大话设计模式读书笔记7——工厂方法模式
工厂方法模式
工厂方法模式(Factory Method):定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法是一个类的实例化延迟到其子类。
工厂方法模式UML结构图
工厂方法模式结构代码
/// <summary> /// 声明'Product' 抽象类 /// </summary> abstract class Product { } /// <summary> /// 声明 'ConcreteProductA' 类,继承Product /// </summary> class ConcreteProductA : Product { } /// <summary> /// 声明 'ConcreteProductB 类,继承Product /// </summary> class ConcreteProductB : Product { } /// <summary> /// 声明 'Creator' 抽象类 /// </summary> abstract class Creator { //工厂方法 public abstract Product FactoryMethod(); } /// <summary> /// 声明 'ConcreteCreatorA' 类,继承Creator类 /// </summary> class ConcreteCreatorA : Creator { //实现父类工厂方法返回类ConcreteProductA实例 public override Product FactoryMethod() { return new ConcreteProductA(); } } /// <summary> /// 声明 'ConcreteCreatorB' 类,继承Creator类 /// </summary> class ConcreteCreatorB : Creator { //实现父类工厂方法返回类ConcreteProductB实例 public override Product FactoryMethod() { return new ConcreteProductB(); } } class Program { static void Main(string[] args) { Creator[] creators = new Creator[2]; creators[0] = new ConcreteCreatorA(); creators[1] = new ConcreteCreatorB(); foreach (Creator creator in creators) { Product product = creator.FactoryMethod(); Console.WriteLine("Created {0}",product.GetType().Name); } Console.ReadKey(); } }
执行结果:
工厂模式实例代码
/// <summary> /// 运算类 /// </summary> public class Operation { public double NumA { get; set; } public double NumB { get; set; } public virtual double GetResult() { double result = 0; return result; } } /// <summary> /// 加法类 /// </summary> public class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumA + NumB; return result; } } /// <summary> /// 减法类 /// </summary> public class OperationSub : Operation { public override double GetResult() { double result = 0; result = NumA - NumB; return result; } } /// <summary> /// 工厂接口 /// </summary> public interface IFactory { Operation FactoryMethod(); } /// <summary> /// 加法工厂 /// </summary> public class AddFactory : IFactory { public Operation FactoryMethod() { return new OperationAdd(); } } /// <summary> /// 减法工厂 /// </summary> public class SubFactory : IFactory { public Operation FactoryMethod() { return new OperationSub(); } } class Program { static void Main(string[] args) { IFactory[] fac = new IFactory[2]; fac[0] = new AddFactory(); fac[1] = new SubFactory(); Operation oper; oper = fac[0].FactoryMethod(); oper.NumA=10; oper.NumB=5; Console.WriteLine("Called By {0}", oper.GetType().Name); Console.WriteLine("Result={0}", oper.GetResult().ToString()); oper = fac[1].FactoryMethod(); oper.NumA = 10; oper.NumB = 5; Console.WriteLine("Called By {0}", oper.GetType().Name); Console.WriteLine("Result={0}", oper.GetResult().ToString()); Console.ReadKey(); } }
运行结果:
简单工厂模式和工厂方法模式的区别
简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。
工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。如想要加功能,本来是改工厂类的,而现在是修改客户端。
作者:Rising Sun
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.