Abstract Factory 为创建一族相互关联或者相互依赖的对象提供支持
GirlFactory
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public abstract class GirlFactory
6 {
7 abstract public Girl Make();
8 }
9}
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public abstract class GirlFactory
6 {
7 abstract public Girl Make();
8 }
9}
PrettyGirlFactory
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public class PrettyGirlFactory : GirlFactory
6 {
7
8 override public Girl Make() {return new ChinessGirl(); }
9
10 }
11}
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public class PrettyGirlFactory : GirlFactory
6 {
7
8 override public Girl Make() {return new ChinessGirl(); }
9
10 }
11}
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public class UglilyGirlFactory :GirlFactory
6 {
7
8 override public Girl Make() { return new JappnessGirl(); }
9 }
10}
Girl
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public abstract class Girl
6 {
7 abstract public void GetName();
8 }
9}
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public abstract class Girl
6 {
7 abstract public void GetName();
8 }
9}
ChinessGirl
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public class ChinessGirl : Girl
6 {
7 override public void GetName() { Console.WriteLine("ChinaGirl");}
8 }
9}
10
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public class ChinessGirl : Girl
6 {
7 override public void GetName() { Console.WriteLine("ChinaGirl");}
8 }
9}
10
JappnessGirl
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public class JappnessGirl : Girl
6 {
7 override public void GetName() { Console.WriteLine("JappnessGirl"); }
8 }
9}
10
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 public class JappnessGirl : Girl
6 {
7 override public void GetName() { Console.WriteLine("JappnessGirl"); }
8 }
9}
10
SimilarGirl
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 class SimilarGirl
6 {
7 private Girl girl;
8 public SimilarGirl( GirlFactory factory )
9 {
10 // create window, attach Links
11 girl = factory.Make();
12 }
13 public void GetName()
14 {
15 girl.GetName();
16 }
17
18 }
19}
20
1using System;
2
3namespace Gof.Test.AbstractFactory
4{
5 class SimilarGirl
6 {
7 private Girl girl;
8 public SimilarGirl( GirlFactory factory )
9 {
10 // create window, attach Links
11 girl = factory.Make();
12 }
13 public void GetName()
14 {
15 girl.GetName();
16 }
17
18 }
19}
20
客户代码
1#if UNIX
2 Gof.Test.AbstractFactory.GirlFactory factory = new Gof.Test.AbstractFactory.UglilyGirlFactory();
3#else
4 Gof.Test.AbstractFactory.GirlFactory factory = new Gof.Test.AbstractFactory.PrettyGirlFactory();
5#endif
6 Gof.Test.AbstractFactory.Girl g = factory.Make();
7 g.GetName();
8 Gof.Test.AbstractFactory.SimilarGirl s = new Gof.Test.AbstractFactory.SimilarGirl(factory);
9 s.GetName();
10 Console.ReadLine();
1#if UNIX
2 Gof.Test.AbstractFactory.GirlFactory factory = new Gof.Test.AbstractFactory.UglilyGirlFactory();
3#else
4 Gof.Test.AbstractFactory.GirlFactory factory = new Gof.Test.AbstractFactory.PrettyGirlFactory();
5#endif
6 Gof.Test.AbstractFactory.Girl g = factory.Make();
7 g.GetName();
8 Gof.Test.AbstractFactory.SimilarGirl s = new Gof.Test.AbstractFactory.SimilarGirl(factory);
9 s.GetName();
10 Console.ReadLine();