工厂方法模式

来源:互联网 发布:欧洲读cs博士知乎 编辑:程序博客网 时间:2024/06/17 20:56

                                                                                             

                   工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不再负责产品的创建,也就是说核心类成为一个抽象工厂角色,只需要负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以是系统在不修改具体工厂角色的情况下引进新的产品。

        工厂方法模式是简单工厂模式的衍生,解决了许多简单工厂模式不能解决的问题。首先它实现了“开——闭”原则,实现了可扩展,还可以应用于产品结果复杂的场合。工厂方法模式对简单工厂模式进行了抽象。有一个抽象的Factory类(可以是抽象类或接口),这个类不再负责具体的产品生产,而是只制定一些规范,具体的生产工作由子类去完成。在此模式中,工厂类和产品经常一一对应,也就是说一个抽象工厂对应一个抽象产品,一个具体工厂对应一个具体产品,这个具体的工厂就负责生产对应的产品。

        简单工厂VS工厂方法:

简单工厂:

class Program    {        staticvoidMain(string[] args)        {            LeiFeng student1 = newUndergraduate();           student1.BuyRice();            LeiFeng student2 = newUndergraduate();           student2.Sweep();            LeiFeng student3 = newUndergraduate();           student3.Wash();        }        classLeiFeng        {            publicvoidSweep()            {               Console.WriteLine("扫地");            }            publicvoidWash()            {               Console.WriteLine("洗衣");            }            publicvoidBuyRice()            {               Console.WriteLine("买米");            }        }        classUndergraduate : LeiFeng        { }        classVolunteer : LeiFeng        { }        classSimpleFactory        {            publicstaticLeiFengCreateLeiFeng(string type)            {               LeiFeng result = null;               switch (type)               {                   case "学雷锋的大学生":                        result =newUndergraduate();                        break;                   case "社区志愿者":                        result =newVolunteer();                        break;               }                returnresult;            }        }         }


 



 

   工厂方法模式

    

class Program    {        staticvoidMain(string[] args)        {            IFactory factory = newUndergraduateFactory();             LeiFeng student =factory.CreateLeiFeng();           student.BuyRice();        }        classLeiFeng        {            publicvoidSweep()            {                Console.WriteLine("扫地");            }            publicvoidWash()            {               Console.WriteLine("洗衣");            }            publicvoidBuyRice()            {               Console.WriteLine("买米");            }        }        classUndergraduate : LeiFeng        { }        classVolunteer : LeiFeng        { }              interfaceIFactory        {            LeiFeng CreateLeiFeng();        }        classUndergraduateFactory : IFactory        {            publicLeiFeng CreateLeiFeng()            {               return newUndergraduate();            }        }        classVolunteerFactory : IFactory        {            publicLeiFeng CreateLeiFeng()            {               return newVolunteer();            }        }}


 



 

       工厂方法模式角色有:抽象工厂、具体工厂、抽象产品、具体产品。其中,抽象工厂是工厂方法模式的核心,与应用程序无关;具体工厂是实现抽象工厂接口的具体工厂类,包含与应用程序密切相关的逻辑,并且受到应用程序调用来创建产品对象;抽象产品是工厂方法模式创建的对象的超类型,也就是产品对象的共同父类或接口;具体产品实现了抽象产品所定义的接口。

      工厂方法模式的适应场景:

     1.    对于一个产品,调用者清楚的知道应该使用哪个具体工厂服务,实例化该具体工厂,生产出具体的产品来。

     2.    只需要一种产品,而不需要知道究竟是哪个工厂生产的。

                                             
0 0
原创粉丝点击