简单工厂是一种实例化对象的方式,通过工厂对象制作你想要的对象。
简单工厂体现了面向对象封装、继承、多态的三大基本特点,通过父类指针指向子类对象,动态的重写子类的函数。
先来看一下一个范例:
#include <iostream>
#include <cstring>
using namespace std;
class Fruit
{
public:
virtual void getFruit() = 0;
};
class Banana : public Fruit
{
public:
void getFruit()
{
cout << "香蕉" << endl;
}
};
class Pear : public Fruit
{
public:
void getFruit()
{
cout << "梨" << endl;
}
};
class Factory
{
public:
static Fruit* create(char *name)
{
Fruit* oper = NULL;
if(strcmp(name,"pear") == 0)
oper = new Pear();
else if(strcmp(name,"banana") == 0)
oper = new Banana();
else
return NULL;
return oper;
}
};
int main()
{
Fruit* pear = Factory::create("pear");
if(pear == NULL)
cout << "创建pear失败" << endl;
pear->getFruit();
Fruit* banana = Factory::create("banana");
if(banana == NULL)
cout << "创建banana失败" << endl;
banana->getFruit();
return 0;
}
在简单工厂中有一个易忘的知识点,static:
- 静态成员函数中不能调用非静态成员变量。
- 非静态成员函数中科院调用静态成员变量,因为静态成员变量属于类本身,在类对象产生之前已经存在,所以非静态成员函数中可以调用静态成员变量。
- 静态成员变量使用之前必须初始化。
熟悉了简单工厂模式,利用简单工厂模式设计一个四则运算计算器。
#include <iostream>
#include <string>
#include <exception>
using namespace std;
class Operation
{
public:
Operation(){}
Operation(double a,double b):_numberA(a),_numberB(b){}
double getNumberA()
{
return _numberA;
}
double getNumberB()
{
return _numberB;
}
void setNumberA(double a)
{
_numberA = a;
}
void setNumberB(double b)
{
_numberB = b;
}
virtual double getResult() = 0;
private:
double _numberA;
double _numberB;
};
class Add : public Operation
{
public:
Add(){}
Add(double a,double b):Operation(a,b){}
double getResult()
{
double result = 0;
result = getNumberA() + getNumberB();
return result;
}
};
class Sub : public Operation
{
public:
Sub(){}
Sub(double a,double b):Operation(a,b){}
double getResult()
{
double result = 0;
result = getNumberA() - getNumberB();
return result;
}
};
class Mul : public Operation
{
public:
Mul(){}
Mul(double a,double b):Operation(a,b){}
double getResult()
{
double result = 0;
result = getNumberA() * getNumberB();
return result;
}
};
class Dev : public Operation
{
public:
Dev(){}
Dev(double a,double b):Operation(a,b){}
double getResult()
{
double result = 0;
if(getNumberB() == 0)
{
return -1.23702e-34;
}
else
{
result = getNumberA() / getNumberB();
return result;
}
}
};
class Factory
{
public:
static Operation* createOperation(char operate)
{
Operation* oper = NULL;
switch(operate)
{
case '+':
oper = new Add();
break;
case '-':
oper = new Sub();
break;
case '*':
oper = new Mul();
break;
case '/':
oper = new Dev();
break;
}
return oper;
}
};
int main()
{
try
{
double a,b,result = 0;
char operate;
cout << "请输入第一个数字:";
cin >> a;
cout << "请输入符号:";
cin >> operate;
cout << "请输入第二个数字:";
cin >> b;
cout << "得到的结果:";
Operation* oper = Factory::createOperation(operate);
oper->setNumberA(a);
oper->setNumberB(b);
result = oper->getResult();
if(result <= -1.23702e-34)
cout << "分母不能为零,请重新输入" << endl;
else
cout << result << endl;
}
catch(exception ex)
{
cout << "您输入的有错,请重新输入。";
}
return 0;
}
优点:生产与消费分开,使得耦合度降低。
缺点:生产类扩展会影响整个系统,逻辑过于复杂时,不利于扩展。
适用于生产类的创建少,消费类只需传入参数,不需要了解生产逻辑。