标签:style blog http strong io 2014 art ar
《简单工厂模式》 工厂类中通过传入的参数实例化出相应的对象oper = OperationFactory.CreateOperate("+")添加新的子类时,工厂类的switch语句中也要添加一个分支,所以简单工厂模式并不符合开闭原则
计算器例子的结构图
我们已经知道了简单工厂模式中,工厂类与其中的分支耦合,所以我们根据依赖倒转原则,把工厂类抽象为一个接口,这个接口有个方法创建抽象产品,然后所有的要生产具体类的工厂来实现这个接口。最后一个简单的工厂类,就变成了一个工厂抽象接口和具体生成对象的工厂。
工厂方法模式
定义:一个接口的方法用来创建抽象产品,所有的要生产具体的工厂去实现这个接口。
结构图如下:
工厂方法模式中运算器的例子结构图
运算器例子代码如下:
运算类
public class Operation { private double _numberA =0; private double _numberB =0; public double NumberA { get { return _numberA;} set( _numerA = value;) } public double NumberB { get { return _numberB;} set( _numerB = value;) } public victual double GerResult() { double result = 0; return result; } }四个运算子类
class OperationAdd :Operation { public override double GetResult() { double result = 0 ; result = _numberA + numberB; return result; } } class OperationSub:Operation { public override double GetResult() { double result = 0; result = numberA - numberB; return result; } } class OperationMul : Operation { public override double GetResult() { double result = 0; result = numberA * numberB; return result; } } class OperationDiv:Operation { public override double GetResult() { double result = 0; if(numberB==0) throw new Exception("除数不能为0。"); result = numberA /numberB; return result; } }
interface IFactory { Operation CreateOperation(); }四个具体工厂子类
class AddFactory:IFactory { public Operation CreateOperation() { return new OperationAdd(); } } class SubFactory:IFactory { public Operation CreateOperation() { return new OperationSub(); } } class MulFactory:IFactory { public Operation CreateOperation() { return new OperationMul(); } } class DivFactory:IFactory { public Operation CreateOperation() { return new OperationDiv(); } }客户端
IFactory operFactory = new AddFactory(); Operation oper = operFactory.CreateOperation(); oper.numberA = 1; oper.numberB = 2; double result = oper.GetResult();
工厂方法模式,完全符合开闭原则,当添加新的运算符(开根)时,添加Operation类和IFactory类的相应的子类即可实现系统的扩展。
标签:style blog http strong io 2014 art ar
原文地址:http://blog.csdn.net/u010928364/article/details/38341789