Factory 定义一个接口,客户可以使用这个接口创建一个对象.同时,我们还可以控制对那个类进行实例化
客户代码
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class MixerPlanner : MachinePlanner
7 {
8 public MixerPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18
19 }
20}
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class MixerPlanner : MachinePlanner
7 {
8 public MixerPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18
19 }
20}
一个类层次:
接口定义
1using System;
2
3namespace Gof.Test.Factory
4{
5 public interface IAcount
6 {
7 string UserName{get;set;}
8 }
9}
1using System;
2
3namespace Gof.Test.Factory
4{
5 public interface IAcount
6 {
7 string UserName{get;set;}
8 }
9}
具体类1
1using System;
2
3namespace Gof.Test.Factory
4{
5 public class AcountMyself:IAcount
6 {
7 public AcountMyself(string myselft)
8 {
9 _name = myselft;
10 }
11 private string _name;
12
13 IAcount 成员#region IAcount 成员
14
15 public string UserName
16 {
17 get
18 {
19 return _name;
20 }
21 set
22 {
23 _name = value;
24 }
25 }
26
27 #endregion
28 }
29}
1using System;
2
3namespace Gof.Test.Factory
4{
5 public class AcountMyself:IAcount
6 {
7 public AcountMyself(string myselft)
8 {
9 _name = myselft;
10 }
11 private string _name;
12
13 IAcount 成员#region IAcount 成员
14
15 public string UserName
16 {
17 get
18 {
19 return _name;
20 }
21 set
22 {
23 _name = value;
24 }
25 }
26
27 #endregion
28 }
29}
具体类2
1using System;
2
3namespace Gof.Test.Factory
4{
5 /**//// <summary>
6 /// AcountYourself 的摘要说明。
7 /// </summary>
8 public class AcountYourself:IAcount
9 {
10 public AcountYourself(string yourself)
11 {
12 _name = yourself;
13 }
14
15 private string _name;
16
17 IAcount 成员#region IAcount 成员
18
19 public string UserName
20 {
21 get
22 {
23 return _name;
24 }
25 set
26 {
27 _name = value;
28 }
29 }
30
31 #endregion
32 }
33}
1using System;
2
3namespace Gof.Test.Factory
4{
5 /**//// <summary>
6 /// AcountYourself 的摘要说明。
7 /// </summary>
8 public class AcountYourself:IAcount
9 {
10 public AcountYourself(string yourself)
11 {
12 _name = yourself;
13 }
14
15 private string _name;
16
17 IAcount 成员#region IAcount 成员
18
19 public string UserName
20 {
21 get
22 {
23 return _name;
24 }
25 set
26 {
27 _name = value;
28 }
29 }
30
31 #endregion
32 }
33}
工厂方法
1using System;
2
3namespace Gof.Test.Factory
4{
5 public class AcountFactory
6 {
7 public AcountFactory()
8 {
9 }
10 public static bool IsAfternoon()
11 {
12 DateTime d = DateTime.Now;
13 if(d.Hour > 12)
14 {
15 return true;
16 }
17 else
18 {
19 return false;
20 }
21 }
22 public static IAcount CreateAcount()
23 {
24 if(IsAfternoon())
25 {
26 return new AcountMyself("Myself");
27 }
28 else
29 {
30 return new AcountYourself("Yourself");
31 }
32 }
33 }
34}
1using System;
2
3namespace Gof.Test.Factory
4{
5 public class AcountFactory
6 {
7 public AcountFactory()
8 {
9 }
10 public static bool IsAfternoon()
11 {
12 DateTime d = DateTime.Now;
13 if(d.Hour > 12)
14 {
15 return true;
16 }
17 else
18 {
19 return false;
20 }
21 }
22 public static IAcount CreateAcount()
23 {
24 if(IsAfternoon())
25 {
26 return new AcountMyself("Myself");
27 }
28 else
29 {
30 return new AcountYourself("Yourself");
31 }
32 }
33 }
34}
客户代码
1 IAcount acount = AcountFactory.CreateAcount();
2 Console.WriteLine(acount.UserName);
3 Console.ReadLine();
双类层次结构:1 IAcount acount = AcountFactory.CreateAcount();
2 Console.WriteLine(acount.UserName);
3 Console.ReadLine();
1.Machine类层次结构;定义不同的类型的机器。
基类Machine
1using System;
2using Gof.Test.Factory.Plan;
3
4namespace Gof.Test.Factory.Machines
5{
6 public class Machine
7 {
8 public Machine()
9 {
10 }
11 public bool HasDone
12 {
13 get
14 {
15 return _hasDone;
16 }
17 set
18 {
19 _hasDone = value;
20 }
21 }private bool _hasDone;
22
23 public virtual MachinePlanner CreatePlanner()
24 {
25 return new BasicPlanner(this);
26 }
27 }
28}
1using System;
2using Gof.Test.Factory.Plan;
3
4namespace Gof.Test.Factory.Machines
5{
6 public class Machine
7 {
8 public Machine()
9 {
10 }
11 public bool HasDone
12 {
13 get
14 {
15 return _hasDone;
16 }
17 set
18 {
19 _hasDone = value;
20 }
21 }private bool _hasDone;
22
23 public virtual MachinePlanner CreatePlanner()
24 {
25 return new BasicPlanner(this);
26 }
27 }
28}
Mixer
1using System;
2using Gof.Test.Factory.Plan;
3
4
5namespace Gof.Test.Factory.Machines
6{
7 public class Mixer : Machine
8 {
9 public Mixer()
10 {
11 }
12 public override MachinePlanner CreatePlanner()
13 {
14 return new MixerPlanner(this);
15 }
16 }
17}
2.计划类层次结构,定义机器的执行计划。1using System;
2using Gof.Test.Factory.Plan;
3
4
5namespace Gof.Test.Factory.Machines
6{
7 public class Mixer : Machine
8 {
9 public Mixer()
10 {
11 }
12 public override MachinePlanner CreatePlanner()
13 {
14 return new MixerPlanner(this);
15 }
16 }
17}
抽象基类
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public abstract class MachinePlanner
7 {
8 public MachinePlanner(Machine machine)
9 {
10 _machine = machine;
11 }
12 public Machine Machine
13 {
14 get
15 {
16 return _machine;
17 }
18 }
19 private Machine _machine;
20 public abstract bool ContineWork
21 {
22 get;
23 }
24 }
25}
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public abstract class MachinePlanner
7 {
8 public MachinePlanner(Machine machine)
9 {
10 _machine = machine;
11 }
12 public Machine Machine
13 {
14 get
15 {
16 return _machine;
17 }
18 }
19 private Machine _machine;
20 public abstract bool ContineWork
21 {
22 get;
23 }
24 }
25}
基本计划类1
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class BasicPlanner:MachinePlanner
7 {
8 public BasicPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18 }
19}
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class BasicPlanner:MachinePlanner
7 {
8 public BasicPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18 }
19}
基本计划类2
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class MixerPlanner : MachinePlanner
7 {
8 public MixerPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18
19 }
20}
1using System;
2using Gof.Test.Factory.Machines;
3
4namespace Gof.Test.Factory.Plan
5{
6 public class MixerPlanner : MachinePlanner
7 {
8 public MixerPlanner(Machine machine) : base(machine)
9 {
10 }
11 public override bool ContineWork
12 {
13 get
14 {
15 return Machine.HasDone;
16 }
17 }
18
19 }
20}
客户代码
1 Gof.Test.Factory.Machines.Mixer mixture1 = new Gof.Test.Factory.Machines.Mixer();
2 mixture1.HasDone = true;
3 Gof.Test.Factory.Plan.MachinePlanner mixerplanner = mixture1.CreatePlanner();
4 if(mixerplanner.ContineWork)
5 {
6 Console.WriteLine("Mixer can work again");
7 }
The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a defer instantiation to subclasses.
1 Gof.Test.Factory.Machines.Mixer mixture1 = new Gof.Test.Factory.Machines.Mixer();
2 mixture1.HasDone = true;
3 Gof.Test.Factory.Plan.MachinePlanner mixerplanner = mixture1.CreatePlanner();
4 if(mixerplanner.ContineWork)
5 {
6 Console.WriteLine("Mixer can work again");
7 }