C#设计模式之简单工厂

导读:
  接触过设计模式的人大多把GoF奉为经典,设计模式被分为创建型、结构型和行为型三大类。简单工厂和抽像工厂都属于创建型,以下是我对简单工厂模式的理解。
  简单工厂又叫做静态工厂方法模式。就是由一个工厂类根据传入的参量决定创建出哪一种产品类的实例。一般涉及到三种角色:
  工厂类:担任这个角色的是工厂方法模式的核心,含有与应用紧密相关的商业逻辑。工厂类在客户端的直接调用下创建产品对象,它往往由一个具体的类实现。
  抽象产品角色:担任这个角色的类是由工厂方法模式所创建的对象的父类,或她们共同拥有的接口。一般由接口或抽象类实现。
  具体产品角色:工厂方法模式所创建的任何对象都是这个角色的实例,由具体类实现。
  就如我们去买手机一样,手机店就是工厂,手机本身的定义是一个抽象产品的角色,我们来到手机店,告诉服务员我们要买的手机型号,这里服务员扮演着一个工厂里的create方法,他根据我们所说的型号拿出我们需要的具体的手机实例。
  简单工厂模式优缺点:模式的核心是工厂类,这个类负责产品的创建,而客户端可以免去产品创建的责任,这实现了责任的分割。但由于工厂类集中了所有产品创建逻辑的,如果不能正常工作的话会对系统造成很大的影响。如果增加新产品必须修改工厂角色的源码。
  下面是一个具体的关于简单工厂的实例。
  以园丁种植水果为例讨论该模式的具体实现:
  Fruit 水果接口,规定水果具有的一些共同特性
  Apple 苹果类 派生自Fruit接口
  Strawberry 草莓类 派生自Fruit接口
  FruitGardener 园丁类 负责草莓与苹果的创建工作。
  当Client要创建水果(苹果或草莓对象)的时候调用园丁类的factory方法创建:
  代码如下:
  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.......

本文转自
http://max01.blog.163.com/blog/static/19812237200742410241379/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值