现在的位置: 首页 > 综合 > 正文

简单工厂模式2

2013年06月18日 ⁄ 综合 ⁄ 共 3185字 ⁄ 字号 评论关闭
设计模式C#描述之——简单工厂模式
摘自:http://www.codesky.net/article/doc/200505/2005051975216396.htm

前言:设计模式是软件开发领域的精髓之一。学好设计模式是目前每一个开发人员的必修课。目前关于设计模式的书很多,其中比较好的有GOF那本的中译本,但并不很适合初学者。还有一本是《JAVA与模式》,比较适合初学者使用,在此强烈推荐。但这本书的不足之处是一些地方讲的过于繁琐,很多地方只须简单说明一下即可,却大费笔墨,使得书籍很厚,看起来费力。而且是用JAVA描述的,这使得一些只懂C#的人无从下手。我是一个.net的拥护者,为了看这本书我特意看了些JAVA的书,感觉JAVA在书籍的多样性方面比 .net好很多,而且不少书籍的质量很高。可能是现在JAVA已经比较成熟的原因吧。为了方便.net的爱好者学习设计模式,在此把我学习《JAVA与模式》这本书的学习笔记发出来,并用C#语言重新描述,希望能对初学者有所帮助。
其实设计模式也并不是什么高深的理论,个人认为并不是象一些人所说的“没写过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.......

抱歉!评论已关闭.