c#设计模式--简单工厂模式

来源:互联网 发布:五浪真言知乎 编辑:程序博客网 时间:2024/06/06 01:10

 

简单工厂模式是类的创建模式,又叫做静态工厂方法模式。就是由一个工厂类根据传入的参量决定创建出哪一种产品类的实例。一般涉及到三种角色(如下图):

 

 

     工厂类:担任这个角色的是工厂方法模式的核心,含有与应用紧密相关的商业逻辑。工厂类在客户端的直接调用下创建产品对象,它往往由一个具体的类实现。

 

    抽象产品角色:担任这个角色的类是由工厂方法模式所创建的对象的父类,或她们共同拥有的接口。一般由接口或抽象类实现。

 

     具体产品角色:工厂方法模式所创建的任何对

 

象都是这个角色的实例,由具体类实现。

 

 

 

简单工厂模式优缺点

 

    模式的核心是工厂类,这个类负责产品的创建,而客户端可以免去产品创建的责任,这实现了责任的分割。但由于工厂类集中了所有产品创建逻辑的,如果不能正常工作的话会对系统造成很大的影响。如果增加新产品必须修改工厂角色的源码。

 

 

以园丁种植水果为例讨论该模式的具体实现:

 

    Fruit 水果接口,规定水果具有的一些共同特性

 

    Apple 苹果类 派生自Fruit接口

 

    Strawberry 草莓类 派生自Fruit接口

 

    FruitGardener 园丁类 负责草莓与苹果的创建工作。

 

    当Client要创建水果(苹果或草莓对象)的时候调用园丁类的factory方法创建:UML图如下:

 

 

代码如下:

 

Fruit.cs

 

namespace Simple_Factory

 

{

 

public interface Fruit

 

{   

 

     //生长

 

     void grow();

 

    //收获

 

     void harvest();

 

     //种植

 

     void plant();

 

}

 

}

 

Apple.cs

 

namespace Simple_Factory

 

{

 

public class Apple:Fruit

 

     {

 

         public Apple()

 

         {

 

         }

 

         #region Fruit 成员

 

         public void grow()

 

         {

 

              Console.WriteLine ("Apple is growing.......");

 

         }
public void harvest()
         {

              Console.WriteLine ("Apple is harvesting.......");

         }

    public void plant()

         {

              Console.WriteLine ("Apple is planting.......");

         }

         #endregion

     }

}

Strawberry.cs

namespace Simple_Factory

{

     public class Strawberry:Fruit

     {

         public Strawberry()

         {       

         }

         #region Fruit 成员

         public void grow()

         {

              Console.WriteLine ("Strawberry is growing.......");

         }

         public void harvest()

         {

              Console.WriteLine ("Strawberry is harvesting.......");

         }

         public void plant()

         {

              Console.WriteLine ("Strawberry is planting.......");

         }

         #endregion

     }

}

FruitGardener.cs

namespace Simple_Factory

{

     public class FruitGardener

     {

         //静态工厂方法

         public static Fruit factory(string which)

         {

              if(which.Equals ("Apple"))

              {

                   return new Apple();

              }

              else if(which.Equals ("Strawberry"))

              {
                    return new Strawberry ();
              }

              else

              {

                   return null;

              }   

         }

     }

}

Client.cs

using System;

namespace Simple_Factory

{

     class Client

     {   

         [STAThread]

         static void Main(string[] args)

         {

              Fruit aFruit=FruitGardener.factory ("Apple");//creat apple

              aFruit.grow ();

              aFruit.harvest ();

              aFruit.plant();

              aFruit=FruitGardener.factory ("Strawberry");//creat strawberry

              aFruit.grow ();

              aFruit.harvest ();

              aFruit.plant();

         }

     }

}

输出如下:

Apple is growing.......

Apple is harvesting.......

Apple is planting.......

Strawberry is growing.......

Strawberry is harvesting.......

Strawberry is planting.......
原创粉丝点击