Factory 定义一个接口,客户可以使用这个接口创建一个对象.同时,我们还可以控制对那个类进行实例化

 客户代码
客户代码1
 using System;
using System;2
 using Gof.Test.Factory.Machines;
using Gof.Test.Factory.Machines;3

4
 namespace Gof.Test.Factory.Plan
namespace Gof.Test.Factory.Plan5


 {
{6
 public class MixerPlanner : MachinePlanner
    public class MixerPlanner : MachinePlanner7

 
     {
{8
 public MixerPlanner(Machine machine) : base(machine)
        public MixerPlanner(Machine machine) : base(machine)9

 
         {
{10
 }
        }11
 public override bool ContineWork
        public override bool ContineWork12

 
         {
{13
 get
            get14

 
             {
{15
 return Machine.HasDone;
                return Machine.HasDone;16
 }
            }17
 }
        }18

19
 }
    }20
 }
}一个类层次:

 接口定义
接口定义1
 using System;
using System;2

3
 namespace Gof.Test.Factory
namespace Gof.Test.Factory4


 {
{5
 public interface IAcount
    public interface IAcount6

 
     {
{7

 string UserName
        string UserName {get;set;}
{get;set;}8
 }
    }9
 }
}
 具体类1
具体类11
 using System;
using System;2

3
 namespace Gof.Test.Factory
namespace Gof.Test.Factory4


 {
{5
 public class AcountMyself:IAcount
    public class AcountMyself:IAcount6

 
     {
{7
 public AcountMyself(string myselft)
        public AcountMyself(string myselft)8

 
         {
{9
 _name = myselft;
            _name = myselft;10
 }
        }11
 private string _name;
        private string _name;12

13

 IAcount 成员#region IAcount 成员
        IAcount 成员#region IAcount 成员14

15
 public string UserName
        public string UserName16

 
         {
{17
 get
            get18

 
             {
{19
 return _name;
                return _name;20
 }
            }21
 set
            set22

 
             {
{23
 _name = value;
                _name = value;24
 }
            }25
 }
        }26

27
 #endregion
        #endregion28
 }
    }29
 }
}
 具体类2
具体类21
 using System;
using System;2

3
 namespace Gof.Test.Factory
namespace Gof.Test.Factory4


 {
{5

 /**//// <summary>
    /**//// <summary>6
 /// AcountYourself 的摘要说明。
    /// AcountYourself 的摘要说明。7
 /// </summary>
    /// </summary>8
 public class AcountYourself:IAcount
    public class AcountYourself:IAcount9

 
     {
{10
 public AcountYourself(string yourself)
        public AcountYourself(string yourself)11

 
         {
{12
 _name = yourself;
            _name = yourself;13
 }
        }14

15
 private string _name;
        private string _name;16

17

 IAcount 成员#region IAcount 成员
        IAcount 成员#region IAcount 成员18

19
 public string UserName
        public string UserName20

 
         {
{21
 get
            get22

 
             {
{23
 return _name;
                return _name;24
 }
            }25
 set
            set26

 
             {
{27
 _name = value;
                _name = value;28
 }
            }29
 }
        }30

31
 #endregion
        #endregion32
 }
    }33
 }
}
 工厂方法
工厂方法1
 using System;
using System;2

3
 namespace Gof.Test.Factory
namespace Gof.Test.Factory4


 {
{5
 public class AcountFactory
    public class AcountFactory6

 
     {
{7
 public AcountFactory()
        public AcountFactory()8

 
         {
{9
 }
        }10
 public static bool IsAfternoon()
        public static bool IsAfternoon()11

 
         {
{12
 DateTime d = DateTime.Now;
            DateTime d = DateTime.Now;13
 if(d.Hour > 12)
            if(d.Hour > 12)14

 
             {
{15
 return true;
                return true;16
 }
            }17
 else
            else18

 
             {
{19
 return false;
                return false;20
 }
            }21
 }
        }22
 public static IAcount CreateAcount()
        public static IAcount CreateAcount()23

 
         {
{24
 if(IsAfternoon())
            if(IsAfternoon())25

 
             {
{26
 return new AcountMyself("Myself");
                return new AcountMyself("Myself");27
 }
            }28
 else
            else29

 
             {
{30
 return new AcountYourself("Yourself");
                return new AcountYourself("Yourself");31
 }
            }32
 }
        }33
 }
    }34
 }
}
 客户代码
客户代码1
 IAcount acount = AcountFactory.CreateAcount();
            IAcount acount = AcountFactory.CreateAcount();2
 Console.WriteLine(acount.UserName);
            Console.WriteLine(acount.UserName);3
 Console.ReadLine();
            Console.ReadLine();1.Machine类层次结构;定义不同的类型的机器。

 基类Machine
基类Machine1
 using System;
using System;2
 using Gof.Test.Factory.Plan;
using Gof.Test.Factory.Plan;3

4
 namespace Gof.Test.Factory.Machines
namespace Gof.Test.Factory.Machines5


 {
{6
 public class Machine
    public class Machine7

 
     {
{8
 public Machine()
        public Machine()9

 
         {
{10
 }
        }11
 public bool HasDone
        public bool HasDone12

 
         {
{13
 get
            get14

 
             {
{15
 return _hasDone;
                return _hasDone;16
 }
            }17
 set
            set18

 
             {
{19
 _hasDone = value;
                _hasDone = value;20
 }
            }21
 }private bool _hasDone;
        }private bool _hasDone;22

23
 public virtual MachinePlanner CreatePlanner()
        public virtual MachinePlanner CreatePlanner()24

 
         {
{25
 return new BasicPlanner(this);
            return new BasicPlanner(this);26
 }
        }27
 }
    }28
 }
}
 Mixer
Mixer1
 using System;
using System;2
 using Gof.Test.Factory.Plan;
using Gof.Test.Factory.Plan;3

4

5
 namespace Gof.Test.Factory.Machines
namespace Gof.Test.Factory.Machines6


 {
{7
 public class Mixer : Machine
    public class Mixer : Machine8

 
     {
{9
 public Mixer()
        public Mixer()10

 
         {
{11
 }
        }12
 public override MachinePlanner CreatePlanner()
        public override MachinePlanner CreatePlanner()13

 
         {
{14
 return new MixerPlanner(this);
            return new MixerPlanner(this);15
 }
        }16
 }
    }17
 }
}
 抽象基类
抽象基类1
 using System;
using System;2
 using Gof.Test.Factory.Machines;
using Gof.Test.Factory.Machines;3

4
 namespace Gof.Test.Factory.Plan
namespace Gof.Test.Factory.Plan5


 {
{6
 public abstract class MachinePlanner
    public abstract class MachinePlanner7

 
     {
{8
 public MachinePlanner(Machine machine)
        public MachinePlanner(Machine machine)9

 
         {
{10
 _machine = machine;
            _machine = machine;11
 }
        }12
 public Machine Machine
        public Machine Machine13

 
         {
{14
 get
            get15

 
             {
{16
 return _machine;
                return _machine;17
 }
            }18
 }
        }19
 private Machine _machine;
        private Machine _machine;20
 public abstract bool ContineWork
        public abstract bool ContineWork21

 
         {
{22
 get;
           get;23
 }
        }24
 }
    }25
 }
}
 基本计划类1
基本计划类11
 using System;
using System;2
 using Gof.Test.Factory.Machines;
using Gof.Test.Factory.Machines;3

4
 namespace Gof.Test.Factory.Plan
namespace Gof.Test.Factory.Plan5


 {
{6
 public class BasicPlanner:MachinePlanner
    public class BasicPlanner:MachinePlanner7

 
     {
{8
 public BasicPlanner(Machine machine) : base(machine)
        public BasicPlanner(Machine machine) : base(machine)9

 
         {
{10
 }
        }11
 public override bool ContineWork
        public override bool ContineWork12

 
         {
{13
 get
            get14

 
             {
{15
 return Machine.HasDone;
                return Machine.HasDone;16
 }
            }17
 }
        }18
 }
    }19
 }
}
 基本计划类2
基本计划类21
 using System;
using System;2
 using Gof.Test.Factory.Machines;
using Gof.Test.Factory.Machines;3

4
 namespace Gof.Test.Factory.Plan
namespace Gof.Test.Factory.Plan5


 {
{6
 public class MixerPlanner : MachinePlanner
    public class MixerPlanner : MachinePlanner7

 
     {
{8
 public MixerPlanner(Machine machine) : base(machine)
        public MixerPlanner(Machine machine) : base(machine)9

 
         {
{10
 }
        }11
 public override bool ContineWork
        public override bool ContineWork12

 
         {
{13
 get
            get14

 
             {
{15
 return Machine.HasDone;
                return Machine.HasDone;16
 }
            }17
 }
        }18

19
 }
    }20
 }
}
 客户代码
客户代码1
 Gof.Test.Factory.Machines.Mixer mixture1 = new Gof.Test.Factory.Machines.Mixer();
            Gof.Test.Factory.Machines.Mixer mixture1 = new Gof.Test.Factory.Machines.Mixer();2
 mixture1.HasDone = true;
            mixture1.HasDone = true;3
 Gof.Test.Factory.Plan.MachinePlanner mixerplanner = mixture1.CreatePlanner();
            Gof.Test.Factory.Plan.MachinePlanner mixerplanner = mixture1.CreatePlanner();4
 if(mixerplanner.ContineWork)
            if(mixerplanner.ContineWork)5

 
             {
{6
 Console.WriteLine("Mixer can work again");
                Console.WriteLine("Mixer can work again");7
 }
            }.jpg) 
  
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号