#include<iostream>
#include<memory>
#include <exception>
using namespace std;
class Operation {
public:
Operation() : A(0), B(0) {}
double GetA() const { return A; }
double GetB() const { return B; }
void SetA(double x) { A = x; }
void SetB(double y) { B = y; }
virtual double GetResult() { return 0; }
~Operation() {}
private:
double A, B;
};
class Add : public Operation {
public:
double GetResult()
{
return GetA() + GetB();
}
};
class Sub : public Operation {
public:
double GetResult()
{
return GetA() - GetB();
}
};
class Mul : public Operation {
public:
double GetResult()
{
return GetA() * GetB();
}
};
class Div : public Operation {
public:
double GetResult()
{
if (GetB() == 0)
throw exception("除数不能未能为零");
return GetA() / GetB();
}
};
class SimpleFactory {
public:
static Operation *CreateOperator(char ch) {
Operation *p = nullptr;
switch (ch) {
case '+':
p = new Add();
break;
case '-':
p = new Sub();
break;
case '*':
p = new Mul();
break;
case '/':
p = new Div();
break;
}
return p;
}
};
int main()
{
double A;
double B;
char ch;
cin >> A >> ch >> B;
Operation *op = SimpleFactory::CreateOperator(ch);
op->SetA(A);
op->SetB(B);
try {
cout << op->GetResult() << endl;
}
catch (const exception& exception) {
cout << exception.what() << endl;
}
return 0;
}
优点:
工厂类中包含必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对客户端而言,去除了与具体产品的依赖
缺点:
每次添加一个产品子类都必须在工厂类中添加一个判断分支
- 工厂方法模式
通过修改客户端实现功能的增添
#include<iostream>
#include<memory>
#include <exception>
using namespace std;
class Operation {
public:
Operation() : A(0), B(0) {}
double GetA() const { return A; }
double GetB() const { return B; }
void SetA(double x) { A = x; }
void SetB(double y) { B = y; }
virtual double GetResult() = 0;
~Operation() {}
private:
double A, B;
};
class Add : public Operation {
public:
double GetResult()
{
return GetA() + GetB();
}
};
class Sub : public Operation {
public:
double GetResult()
{
return GetA() - GetB();
}
};
class Mul : public Operation {
public:
double GetResult()
{
return GetA() * GetB();
}
};
class Div : public Operation {
public:
double GetResult()
{
if (GetB() == 0)
throw exception("除数不能未能为零");
return GetA() / GetB();
}
};
class factory {
public:
virtual Operation* createFactory() = 0;
};
class AddFactory : public factory {
public:
Operation* createFactory() {
return new Add();
}
};
class SubFactory : public factory {
public:
Operation * createFactory() {
return new Sub();
}
};
class MulFactory : public factory {
public:
Operation* createFactory() {
return new Mul();
}
};
class DivFactory : public factory {
public:
Operation* createFactory(){
return new Div();
}
};
int main()
{
double A;
double B;
char ch;
cin >> A >> B;
factory *Add = new AddFactory();
Operation* add = Add->createFactory();
add->SetA(A);
add->SetB(B);
try {
cout << add->GetResult() << endl;
}
catch (const exception& exception) {
cout << exception.what() << endl;
}
return 0;
}
- 抽象工厂模式
与工厂模式的区别:抽象工厂模式也就是不仅生产鼠标,同时生产键盘
实例 -> 类 -> 类工厂
实例 -> 类 -> 类工厂 -> 抽象工厂
即,在抽象工厂模式中,增加一个具体的工厂很容易,但是你想在工厂中多生产一种产品,就需要修改很多个类,会违背开闭原则,这种情况下应该使用工厂模式。
参考:
https://www.cnblogs.com/jostree/p/4251756.html
https://www.cnblogs.com/cxjchen/p/3143633.html
http://ichennan.com/2016/08/09/DesignPattern.html
https://www.zhihu.com/question/20367734
http://www.cnblogs.com/carsonzhu/p/5770468.html