标签:style blog http color 使用 os
简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。例如在简单工厂模式中出现的工厂函数:
Operation* FactoryFunction(double left, double right, char op) { switch (op) { case '+': return new Add(left, right); break; case '-': return new Sub(left, right); break; case '*': return new Mul(left, right); break; case '/': return new Div(left, right); break; default: throw runtime_error("Operation invalid!"); break; } }
#include <iostream> using namespace std; // 运算基类 class Operation { public: Operation(const double left, const double right) { lhs = left; rhs = right; } virtual double Calculate() = 0; // 纯虚函数 protected: double lhs; double rhs; }; // 加法运算 class Add : public Operation { public: Add(double left, double right) : Operation(left, right) {} double Calculate() { return lhs + rhs; } }; // 减法运算 class Sub : public Operation { public: Sub(double left, double right) : Operation(left, right) {} double Calculate() { return lhs - rhs; } }; // 乘法运算 class Mul : public Operation { public: Mul(double left, double right) : Operation(left, right) {} double Calculate() { return lhs * rhs; } }; // 除法运算 class Div : public Operation { public: Div(double left, double right) : Operation(left, right) { try { if (right == 0) throw runtime_error("The divisor cannot be 0\n"); } catch (const runtime_error &e) { cout << e.what() << endl; throw; } } double Calculate() { return lhs / rhs; } }; // 抽象工厂 class AbstractFactory { public: virtual Operation* CreateOperation(const int left, const int right) = 0; }; // 根据运算实现出各自的工厂类 class AddFactory : public AbstractFactory { public: Operation* CreateOperation(const int left, const int right) { return new Add(left, right); } }; class SubFactory : public AbstractFactory { public: Operation* CreateOperation(const int left, const int right) { return new Sub(left, right); } }; class MulFactory : public AbstractFactory { public: Operation* CreateOperation(const int left, const int right) { return new Mul(left, right); } }; class DivFactory : public AbstractFactory { public: Operation* CreateOperation(const int left, const int right) { return new Div(left, right); } }; int main() { AbstractFactory *creator = new AddFactory(); Operation *add = creator->CreateOperation(11, 22); cout << add->Calculate() << endl; delete creator; creator = new SubFactory(); Operation *sub = creator->CreateOperation(25, 32); cout << sub->Calculate() << endl; delete creator; creator = new MulFactory(); Operation *mul = creator->CreateOperation(11, 11); cout << mul->Calculate() << endl; delete creator; creator = new DivFactory(); Operation *div = creator->CreateOperation(50, 8); cout << div->Calculate() << endl; delete creator; // 别忘记销毁指针 delete add; delete sub; delete mul; delete div; system("pause"); return 0; }
上面的例子中,定义了一个AbstractFactory抽象工厂类,创建实际对象的工厂类都要继承这个抽象基类并重写具体产生对象的方法CreateOperation。当添加新的运算操作时,不需要修改原有的工厂类,只需要继承AbstractFactory定义一个新的工厂类即可,这就符合了封闭开放原则。
参考:
《大话设计模式》第8章
标签:style blog http color 使用 os
原文地址:http://blog.csdn.net/nestler/article/details/38035839