Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6552373
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: C#/.net

2020-04-18 18:36:22

今天,开始和大家分享软件开发中的23种设计模式,和大家一起进步。在疫情到处肆虐的时候,我们净下心提升点我们的能力,以确保我们在激烈的竞争中保留自己的一个生存的机会。
工厂顾名思义就是创建产品,根据产品是具体产品还是具体工厂可分为简单工厂模式和工厂方法模式,根据工厂的抽象程度可分为工厂方法模式和抽象工厂模式。该模式用于封装和管理对象的创建,是一种创建型模式。本文从一个具体的例子逐步深入分析,来体会三种工厂模式的应用场景和利弊。

1. 简单工厂模式

简单工厂模式对对象创建管理方式最为简单,因为其仅仅简单的对不同类对象的创建进行了一层薄薄的封装。该模式通过向工厂传递类型来指定要创建的对象,其UML类图如下:

下面通过使用C#语言来实现简单工厂模式。目标是要生产各类手机。
首先,我们定义一个生产手机的标准接口(相当于AbstractProduct),仅有一个制造方法。如下:

点击(此处)折叠或打开

  1.     public interface IPhone
  2.     {
  3.         ///
  4.         /// Make a Phone.
  5.         ///
  6.         void Make();
  7.     }
在C#的接口定义中,如果不指明接口是public的话,只能在程序集中访问。对于接口中的方法,不用进行访问级别申明,默认是public的。

下面实现华为手机的制造(ProductA),如下:

点击(此处)折叠或打开

  1. public class HuaWeiPhone : IPhone
  2.     {
  3.         public HuaWeiPhone()
  4.         {
  5.             this.Make();
  6.         }
  7.         #region IPhone 成员

  8.         public void Make()
  9.         {
  10.             Console.WriteLine("我厂生产华为手机。");
  11.         }

  12.         #endregion
  13.     }
C#中对接口的实现以及对类的继承统一使用冒号。不像Java中使用关键字implements或者extends。

再来一个山寨版的苹果(相当于ProductB),如下:

点击(此处)折叠或打开

  1. public class ApplePhone : IPhone
  2.     {
  3.         public ApplePhone()
  4.         {
  5.             this.Make();
  6.         }
  7.         #region IPhone 成员

  8.         public void Make()
  9.         {
  10.             Console.WriteLine("我厂生产苹果手机。不是Iphone。");
  11.         }

  12.         #endregion
  13.     }

主程序

点击(此处)折叠或打开

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             PhoneFactory factory = new PhoneFactory();
  6.             IPhone huaweiPhone = factory.MakePhone("HuaWeiPhone"); // make huawei phone!
  7.             IPhone applePhone = factory.MakePhone("ApplePhone"); // make applephone!
  8.             Console.ReadLine();
  9.         }
  10.     }

2. 工厂方法模式(Factory Method)

和简单工厂模式中工厂负责生产所有产品相比,工厂方法模式将生成具体产品的任务分发给具体的产品工厂,其UML类图如下:
也就是定义一个抽象工厂,其定义了产品的生产接口,但不负责具体的产品,将生产任务交给不同的派生类工厂。这样不用通过指定类型来创建对象了。而是通过指定类型来创建工厂,再由工厂制造产品。

接下来继续使用生产手机的例子来讲解该模式。

其中和产品相关的IPhone接口、HuaWeiPhone类和ApplePhone类的定义不变。

定义IFactory类:生产不同产品的工厂的接口:

点击(此处)折叠或打开

  1.     public interface IFactory
  2.     {
  3.         IPhone MakePhone();
  4.     }
 HuaWeiFactory类:生产华为手机的工厂(ProductAFactory):

点击(此处)折叠或打开

  1. public class HuaWeiFactory:IFactory
  2.     {
  3.         #region IFactory 成员

  4.         public SampleFactory.IPhone MakePhone()
  5.         {
  6.             return new HuaWeiPhone();
  7.         }

  8.         #endregion
  9.     }
AppleFactory类:生产苹果手机的工厂(ProductBFactory):

点击(此处)折叠或打开

  1. public class AppleFactory : IFactory
  2.     {
  3.         #region IFactory 成员

  4.         public SampleFactory.IPhone MakePhone()
  5.         {
  6.             return new ApplePhone();
  7.         }

  8.         #endregion
  9.     }
主程序:

点击(此处)折叠或打开

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             IFactory huaweiFactory = new HuaWeiFactory();
  6.             IFactory appleFactory = new AppleFactory();
  7.             huaweiFactory.MakePhone(); // make huawei phone!
  8.             appleFactory.MakePhone(); // make apple phone!
  9.             Console.ReadLine();
  10.         }
  11.     }

3. 抽象工厂模式(Abstract Factory)

上面两种模式不管工厂怎么拆分抽象,都只是针对一类产品Phone(AbstractProduct),如果要生成另一种产品PC,应该怎么表示呢?

最简单的方式是把2中介绍的工厂方法模式完全复制一份,不过这次生产的是PC。但同时也就意味着我们要完全复制和修改Phone生产管理的所有代码,显然这是一个笨办法,并不利于扩展和维护。

抽象工厂模式通过在AbstarctFactory中增加创建产品的接口,并在具体子工厂中实现新加产品的创建,当然前提是子工厂支持生产该产品。否则继承的这个工厂可以什么也不干。

其UML类图如下:


从上面类图结构中可以清楚的看到如何在工厂方法模式中通过增加新产品接口来实现产品的增加的。

接下来我们继续通过华为和苹果产品生产的例子来解释该模式。

PC接口:定义PC产品的接口(IPCComputer)

点击(此处)折叠或打开

  1. public interface IPCComputer
  2.     {
  3.         void Make();
  4.     }
HuaWeiPC类:定义华为电脑产品(HuaweiPC)

点击(此处)折叠或打开

  1. public class HuaWeiPCComputer : IPCComputer
  2.     {
  3.         public HuaWeiPCComputer()
  4.         {
  5.             this.Make();
  6.         }

  7.         #region IPCComputer 成员

  8.         public void Make()
  9.         {
  10.             Console.WriteLine("生产华为笔记本电脑!");
  11.         }

  12.         #endregion
  13.     }
ApplePC类:定义苹果电脑产品(MAC)

点击(此处)折叠或打开

  1. public class ApplePCComputer : IPCComputer
  2.     {
  3.         public ApplePCComputer()
  4.         {
  5.             this.Make();
  6.         }

  7.         #region IPCComputer 成员

  8.         public void Make()
  9.         {
  10.             Console.WriteLine("生产苹果笔记本电脑,不是IMac哦。");
  11.         }

  12.         #endregion
  13.     }
下面需要修改工厂相关的类的定义:
IMultiProductFactory类:增加PC产品制造接口

点击(此处)折叠或打开

  1. public interface IMultiProductFactory
  2.     {
  3.         IPhone MakePhone();
  4.         IPCComputer MakePCComputer();
  5.     }
HuaWeiMultiProductFactory类:增加华为PC的制造(ProductFactory1)

点击(此处)折叠或打开

  1. public class HuaWeiMultiProductFactory : IMultiProductFactory
  2.     {
  3.         #region IMultiProductFactory 成员

  4.         public SampleFactory.IPhone MakePhone()
  5.         {
  6.             return new HuaWeiPhone();
  7.         }

  8.         public IPCComputer MakePCComputer()
  9.         {
  10.             return new HuaWeiPCComputer();
  11.         }

  12.         #endregion
  13.     }
AppleMultiProductFactory类:增加苹果PC的制造(ProductFactory2)

点击(此处)折叠或打开

  1. public class AppleMultiProductFactory : IMultiProductFactory
  2.     {
  3.         #region IMultiProductFactory 成员

  4.         public SampleFactory.IPhone MakePhone()
  5.         {
  6.             return new ApplePhone();
  7.         }

  8.         public IPCComputer MakePCComputer()
  9.         {
  10.             return new ApplePCComputer();
  11.         }

  12.         #endregion
  13.     }
主程序:

点击(此处)折叠或打开

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             IMultiProductFactory huaweiFactory = new HuaWeiMultiProductFactory();
  6.             IMultiProductFactory appleFactory = new AppleMultiProductFactory();
  7.             huaweiFactory.MakePhone(); // make huawei phone!
  8.             huaweiFactory.MakePCComputer(); // make huawei PC!
  9.             appleFactory.MakePhone(); // make apple phone!
  10.             appleFactory.MakePCComputer(); // make apple PC!
  11.             Console.ReadLine();
  12.         }
  13.     }

总结:

上面介绍的三种工厂模式有各自的应用场景,实际应用时能解决问题满足需求即可,可灵活变通,无所谓高级与低级。
其中简单工厂方法实现同种产品的各种实现,根据名称来确定生产哪类产品。
工厂方法将同种产品的各种实现都由各自的工厂来实现。在使用的时候由各种工厂来产生各自的产品。
而抽象工厂主要实现多类产品的生产。即每个工厂可生产不同的产品。

在实际的编程中,你可以根据你的实际情况来确定使用哪种工厂模式。
附件将我的UML和源码加载上。
FactoryPattern.rar













阅读(1021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~