简单工厂

简单工厂模式并不属于23种设计模式,它只是一个入门,比较容易理解,那好,我这个菜鸟也从这里开始入门了。
简单工厂模式是类的创建模式,又叫做静态工厂方法模式。就是由一个工厂类根据传入的参量决定创建出哪一种产品类的实例。

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

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

    具体产品角色:工厂方法模式所创建的任何对象都是这个角色的实例,由具体类实现。

象都是这个角色的实例,由具体类实现。
简单工厂模式优缺点

    模式的核心是工厂类,这个类负责产品的创建,而客户端可以免去产品创建的责任,这实现了责任的分割。但由于工厂类集中了所有产品创建逻辑的,如果不能正常工作的话会对系统造成很大的影响。如果增加新产品必须修改工厂角色的源码。
对于“新系列加新对象”,就我所知,目前还没有完美的做法,只有一些演化的思路,这种变化实在是太剧烈了,因为系统对于新的对象是完全陌生的。
下面这个例子是我在网上找的,但是它有个错误就是使用了接口。在关系表述中,接口表示can-do关系,而抽象类表述的是isa的关系。
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.......
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值