设计模式(5)--工厂模式
2016-11-03 00:15 sylar_liang 阅读(243) 评论(1) 编辑 收藏 举报//5.工厂模式 //ver1 //回顾简单工厂模式 class OperationFactory { public: static Operation createOperation(char chOper) { Operation * op = NULL; switch(chOper) { case '+': op = new OperationAdd(); break; case '-': op = new OperationSub(); break; default: break; } return *op; } }; void main11() { Operation oper; oper = OperationFactory::createOperation('+'); oper.SetNumberA(1.1); oper.SetNumberB(2.2); double result = oper.GetResult(); } //工厂模式: //工厂接口 class Operation { public: Operation(){} ~Operation(){} protected: double _NumberA; double _NumberB; public: void SetNumberA(double A) { _NumberA = A; } double GetNumberA() { return _NumberA; } void SetNumberB(double B) { _NumberB = B; } double GetNumberB() { return _NumberB; } virtual double GetResult() { double result = 0; return result; } }; class OperationAdd : public Operation { public: virtual double GetResult() { double result = _NumberA + _NumberB; return result; } }; class OperationSub : public Operation { public: virtual double GetResult() { double result = _NumberA - _NumberB; return result; } }; class IFactory { public: virtual Operation CreateOperation() { Operation * po = NULL; return *po; } }; class AddFactory : public IFactory { public: virtual Operation CreateOperation() { Operation * po = new OperationAdd(); return *po; } }; class SubFactory : public IFactory { public: virtual Operation CreateOperation() { Operation * po = new OperationSub(); return *po; } }; void main1() { IFactory * pif = new AddFactory(); Operation po = pif->CreateOperation(); po.SetNumberA(1.1); po.SetNumberB(2.2); double result = po.GetResult(); }
//简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,
//对于客户端来说,去除了与具体产品的依赖。
//工厂模式,定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
//工厂模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在,也就是说,工厂方式把
//简单工厂的内部逻辑判断移到了客户端代码来进行。你想要加功能,本来是改工厂类,现在是改客户端。
//5.工厂模式 //ver2 class LeiFeng { public: void Sweep() { //打扫 } void Wash() { //洗衣 } }; //学习雷锋大学生 class Undergraduate : public LeiFeng { }; //志愿者 class Volunteer : public LeiFeng { }; //简单雷锋工厂 class SimpleFactory { public: static LeiFeng CreateLeiFeng(char cType) { LeiFeng * plf = NULL; switch (cType) { case '1': //大学生 plf = new Undergraduate(); break; case '2': //志愿者 plf = new Volunteer(); break; default: break; } return *plf; } }; void main21() { LeiFeng stuA = SimpleFactory::CreateLeiFeng('1'); stuA.Sweep(); LeiFeng volA = SimpleFactory::CreateLeiFeng('2'); volA.Wash(); } //雷锋工厂 class IFactory { public: //IFactory(){} LeiFeng CreateLeiFeng() { LeiFeng * plf = new LeiFeng(); return *plf; } }; //大学生工厂 class UndergraduateFactory : public IFactory { public: //UndergraduateFactory(){} LeiFeng CreateLeiFeng() { LeiFeng *plf = new Undergraduate(); return *plf; } }; //志愿者工厂 class VolunteerFactory : public IFactory { public: //VolunteerFactory(){} LeiFeng CreateLeiFeng() { LeiFeng * plf = new Volunteer(); return *plf; } }; void main22() { IFactory * pf = new UndergraduateFactory(); LeiFeng stu = pf->CreateLeiFeng(); stu.Sweep(); stu.Wash(); }
//工厂模式客服了简单工厂违背开放-封闭原则的缺点,又保持了封装对象创建过程的优点。
//缺点:每加一个产品,就需要加一个产品工厂的类,增加了额外的开发量。