哪些網(wǎng)站是用wordpressseo網(wǎng)站優(yōu)化詳解
這三種模式, 都是創(chuàng)建類型的模式, 將對象的創(chuàng)建流程封裝起來供客戶調(diào)用
簡單工廠模式
簡介
: 和策略模式一樣,就是針對不通的參數(shù), 返回不通的實(shí)例而已
問題
: 沒有遵循開閉原則, 如果我們想增加一種類, 那么就要修改工廠的核心代碼,這違反了對修改關(guān)閉
的原則, 于是有了 工廠方法模式
策略模式
和簡單工廠模式
的代碼用例
class Operator
{
public:int first, second;Operator() {};Operator(int a, int b) : first(a), second(b) {};virtual int get_result() = 0;
};class AddOperator : public Operator
{
public:AddOperator() {};AddOperator(int a, int b) : Operator(a, b) {};int get_result() override{return first + second;}
};class MulOperator : public Operator
{
public:MulOperator() {};MulOperator(int a, int b) : Operator(a, b) {};int get_result() override{return first * second;}
};// strategy
int get_result(char _type, int a, int b)
{Operator* o{};if (_type == '+')o = new AddOperator(a, b);if (_type == '*')o = new MulOperator(a, b);return o->get_result();
}// simple factory
class OperatorFactory
{
public:Operator* get_operator(char c){Operator* o{};if (c == '+')o = new AddOperator();if (c == '*')o = new MulOperator();return o;}
};int main()
{//策略模式int a = get_result('+', 1, 2);cout << a << endl;int b = get_result('*', 8, 9);cout << b << endl;// 簡單工廠模式OperatorFactory* op_factory = new OperatorFactory();Operator* o = op_factory->get_operator('+');o->first = 1;o->second = 2;cout << o->get_result() << endl;
}
工廠方法模式
簡介
: 在簡單工廠的基礎(chǔ)上,遵循開閉原則
, 每個(gè)工廠都只產(chǎn)出自己的類, 那么再有新的類要加入的時(shí)候, 我們只需要添加一個(gè)工廠子類 和 目標(biāo)子類就行了
問題
: 工廠只能產(chǎn)出一種目標(biāo)類的實(shí)例, 這樣的話, 系統(tǒng)中類一多, 工廠就會(huì)太多.
`代碼``
class Operator
{
public:int first, second;Operator() {};Operator(int a, int b) : first(a), second(b) {};virtual int get_result() = 0;
};class AddOperator : public Operator
{
public:AddOperator() {};AddOperator(int a, int b) : Operator(a, b) {};int get_result() override{return first + second;}
};class MulOperator : public Operator
{
public:MulOperator() {};MulOperator(int a, int b) : Operator(a, b) {};int get_result() override{return first * second;}
};class Factory
{
public:virtual Operator* get_operator(int a, int b) = 0;
};class AddFactory : public Factory
{
public:Operator* get_operator(int a, int b) override{Operator* op = new AddOperator(a, b);return op;}
};class MulFactory : public Factory
{
public:Operator* get_operator(int a, int b) override{Operator* op = new MulOperator(a, b);return op;}
};int main()
{// 工廠方法模式AddFactory* add_f = new AddFactory();Operator* op = add_f->get_operator(1, 2);cout << op->get_result() << endl;
}
抽象工廠模式
簡介
: 一個(gè)工廠產(chǎn)出一系列互相關(guān)聯(lián)的類的實(shí)例, 實(shí)現(xiàn)產(chǎn)品簇的效果
代碼
class SoftWare
{
public:SoftWare() {};
};class AndroidSoftWare : public SoftWare
{
public:AndroidSoftWare() {};
};class IosSoftWare : public SoftWare
{
public:IosSoftWare() {};
};class Phone
{
public:SoftWare* ware;virtual void set_soft_ware(SoftWare* ware) = 0;
};class XiaoMiPhone : public Phone
{
public:void set_soft_ware(SoftWare* w) override{cout << "xiao mi phone set android soft ware" << endl;ware = w;}
};class IosPhone : public Phone
{
public:void set_soft_ware(SoftWare* w) override{cout << "ios phone set ios soft ware" << endl;ware = w;}
};// 這種情況下, 用抽象工廠模式, 就不會(huì)出錯(cuò), 不會(huì)出現(xiàn)iosphone配上了android soft ware的情況
// 不然在實(shí)際項(xiàng)目中, 配錯(cuò)很可能會(huì)出現(xiàn)嚴(yán)重后果
class AbstractFactory
{
public:virtual Phone* create_phone() = 0;virtual SoftWare* create_soft_ware() = 0;
};class XiaoMiFactory : public AbstractFactory
{Phone* create_phone() override{Phone* p = new XiaoMiPhone();return p;}SoftWare* create_soft_ware() override{SoftWare* sw = new AndroidSoftWare();return sw;}
};class IosFactory : public AbstractFactory
{Phone* create_phone() override{Phone* p = new IosPhone();return p;}SoftWare* create_soft_ware() override{SoftWare* sw = new IosSoftWare();return sw;}
};
int main()
{// 很多情況下, 工廠不止是生產(chǎn)一種類, 而是生產(chǎn)一整套互相關(guān)聯(lián)的類, 這樣一個(gè)工廠, 就是抽象工廠模式AbstractFactory* af = new XiaoMiFactory();Phone* p1 = af->create_phone();SoftWare* sw = af->create_soft_ware();p1->set_soft_ware(sw);af = new IosFactory();Phone* p2 = af->create_phone();SoftWare* sw2 = af->create_soft_ware();p2->set_soft_ware(sw2);
}