1.简单工厂模式
简单工厂模式的缺点,把矛盾集中在简单工厂类中,使工厂类高耦合,而且进行修改和添加的时候也会比较麻烦。简单工厂模式的优点,结构更加清晰,使代码容易维护和分工明确,代码如下:
namespace DesignMode
{
public class SimpleFactory
{
public static TrafficInterface CreateInstance(TrafficType type) {
switch (type)
{
case TrafficType.Car:
return new Car();
case TrafficType.Train:
return new Train();
case TrafficType.Airplane:
return new Airplane();
default:
return null;
}
}
public enum TrafficType {
Car,
Train,
Airplane
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/a8938a1f2aed8a15b70821fc8e68ece8.png)
2.工厂模式+配置文件
工程模式加上配置文件的好处,就是代码管理起来更加的容易,在茫茫的代码堆中需要修改的时候直接去找配置文件就可以,虽然性能上有消耗是必然的,代码如下:
using System.Configuration;
using System;
namespace DesignMode
{
public class ConfigureFactory
{
private static string type = ConfigurationManager.AppSettings["TrafficType"];//读取配置文件
public static TrafficInterface CreateInstance() {
switch ((TrafficType)Enum.Parse(typeof(TrafficType),type))
{
case TrafficType.Car:
return new Car();
case TrafficType.Train:
return new Train();
case TrafficType.Airplane:
return new Airplane();
default:
return null;
}
}
public enum TrafficType {
Car,
Train,
Airplane
}
}
}
解决方案下App.config配置里添加的Key:
<appSettings>
<add key="TranfficType" value="Train"/>
</appSettings>
3.工厂模式+反射机制
这个反射机制不用选择开关,因为反射允许我们在编译与执行时,可以载入程序代码的内部信息,反射成本很高,性能消耗更大,但是代码也更加容易维护,代码如下:
using System.Configuration;
using System;
namespace DesignMode
{
public class ReflectFactory
{
private static string reflect = ConfigurationManager.AppSettings["TrafficReflect"];//读取配置文件
public static TrafficInterface CreateInstance() {
string assemblyName = reflect.Split(',')[0];
string typeName = reflect.Split(',')[1];
return (TrafficInterface)(Activator.CreateInstance(assemblyName, typeName).Unwrap());//反射去创建对象
}
}
}
解决方案下App.config配置里添加的Key:
<appSettings>
<add key="TranfficReflect" value="DesignMode,DesignMode.Train"/>
</appSettings>
4.工厂方法
类创建单独提供专属工厂类,这个有什么好处呢?就像笔者上面注释的,这个类创建前拓展一些办法,也就是开闭原则,对于拓展开放修改封闭,代码如下:
namespace DesignMode
{
public class CarFactory:OnlyFactoryInterface
{
public TrafficInterface CreateInstance() {
//TODO 在创建对象前拓展一些办法
return new Car();
}
}
public interface OnlyFactoryInterface
{
TrafficInterface CreateInstance();
}
}
5.抽象工厂
这个抽象工厂在项目修改或者添加的时候比较繁琐,但是当有相同模块需要开发的时候,代码就可以完全重复使用,并且工厂模式都符合开闭原则。代码如下所示:
using System;
namespace DesignMode
{
public interface IFactoryInterface
{
TrafficInterface CreaateFirst();
TrafficInterface CreateSecond();
TrafficInterface CreateThird();
TrafficInterface CreateFourth();
}
class TrafficFactory : IFactoryInterface
{
public TrafficInterface CreaateFirst()
{
return new Car();
}
public TrafficInterface CreateSecond()
{
return new Train();
}
public TrafficInterface CreateThird()
{
return new Airplane();
}
public TrafficInterface CreateFourth()
{
throw new NotImplementedException();
}
}
}
总结:
工厂模式的优缺点:
优点: 1、一个调用者想创建一个对象,只要知道其名称就可以了。 2、扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。 3、屏蔽产品的具体实现,调用者只关心产品的接口。
缺点:每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事。
工厂模式适用场景(不仅限于以下场景):
1、日志记录器:记录可能记录到本地硬盘、系统事件、远程服务器等,用户可以选择记录日志到什么地方。
2、数据库访问,当用户不知道最后系统采用哪一类数据库,以及数据库可能有变化时。
3、设计一个连接服务器的框架,需要三个协议,"POP3"、"IMAP"、"HTTP",可以把这三个作为产品类,共同实现一个接口。