工厂方法(factory method)

动机(Motivation)

在软件系统中,经常面临着“某个对象”的创建工作;由需求的变化,这个对象经常面临着剧烈的变化,但是它却拥有比较稳定的接口。
如何应对这种变化?如何提供一种“封装机制”来隔离出“这个易变对象”的变化,从而保持系统中“其他依赖该对象的对象”不随着需求改变而改变?


意图(Intent)

定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使得一个类的实例化延迟到子类。


Factory Method模式的几个要点

(1) Factory Method模式主要用于隔离类对象的使用者和具体类型之间的耦合关系。对一个经常变化的具体类型,紧耦合关系会导致软件的脆弱。
(2) Factory Method模式通过向对象的手法,将所要创建的具体对象工作延迟到子类,从而实现一种扩展(而非更改)的策略,较好地解决了这种
紧耦合关系。
(3) Factory Method模式解决“单个对象”的需求变化,Abstract Factory 模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。

  1 namespace 工厂方法
  2 {
  3     public enum HumanColor {White,Black,Yellow}
  4 
  5     public abstract class Human
  6     {
  7         public abstract void GetHumanColor();
  8         public abstract void Speak();
  9     }
 10 }
 11 
 12 namespace 工厂方法
 13 {
 14     public class WhiteHuman:Human
 15     {
 16         public override void GetHumanColor()
 17         {
 18             Console.WriteLine("人种颜色为:{0}",HumanColor.White);
 19         }
 20 
 21         public override void Speak()
 22         {
 23             Console.WriteLine("我是白人,我骄傲!");
 24         }
 25     }
 26 }
 27 
 28 namespace 工厂方法
 29 {
 30     public class BlackHuman:Human
 31     {
 32         public override void GetHumanColor()
 33         {
 34             Console.WriteLine("人种颜色为:{0}", HumanColor.Black);
 35         }
 36 
 37         public override void Speak()
 38         {
 39             Console.WriteLine("我是黑人,我骄傲!");
 40         }
 41     }
 42 }
 43 
 44 namespace 工厂方法
 45 {
 46     public class YellowHuman:Human
 47     {
 48         public override void GetHumanColor()
 49         {
 50             Console.WriteLine("人种颜色为:{0}", HumanColor.Yellow);
 51         }
 52 
 53         public override void Speak()
 54         {
 55             Console.WriteLine("我是黄种人,我骄傲!");
 56         }
 57     }
 58 }
 59 
 60 namespace 工厂方法
 61 {
 62     public abstract class AbstractFactory 
 63     {
 64         public abstract Human CreateHuman();
 65     }
 66 }
 67 
 68 namespace 工厂方法
 69 {
 70     public class WhiteHumanFactory : AbstractFactory
 71     {
 72         public override Human CreateHuman()
 73         {
 74             return new WhiteHuman();
 75         }
 76     }
 77 }
 78 
 79 namespace 工厂方法
 80 {
 81     public class BlackHumanFactory : AbstractFactory
 82     {
 83         public override Human CreateHuman()
 84         {
 85             return new BlackHuman();
 86         }
 87     }
 88 }
 89 
 90 namespace 工厂方法
 91 {
 92     public class YellowHumanFactory : AbstractFactory
 93     {
 94         public override Human CreateHuman()
 95         {
 96             return new YellowHuman();
 97         }
 98     }
 99 }
100 
101 namespace 工厂方法
102 {
103     class Program
104     {
105         static void Main(string[] args)
106         {
107             AbstractFactory factory1 = new WhiteHumanFactory();
108             Human human1 = factory1.CreateHuman();
109             human1.GetHumanColor();
110             human1.Speak();
111 
112             AbstractFactory factory2 = new BlackHumanFactory();
113             Human human2 = factory2.CreateHuman();
114             human2.GetHumanColor();
115             human2.Speak();
116 
117             AbstractFactory factory3 = new YellowHumanFactory();
118             Human human3 = factory3.CreateHuman();
119             human3.GetHumanColor();
120             human3.Speak();
121 
122             Console.ReadKey();
123         }
124     }
125 }
原文地址:https://www.cnblogs.com/zhxlsuyu/p/3514026.html