【设计】工厂模式--C++源代码(VS2015)

来源:互联网 发布:google adb mac 编辑:程序博客网 时间:2024/06/05 15:48

1. 简单工厂模式--添加新产品需要修改类Factory,违背的类的封装性原则

#include <iostream>

#include <vector>
using namespace std;


class Product
{
public :
virtual void Show() = 0;
};


class ProductA : public Product
{
public :
void Show()
{
cout << "it is Product A" << endl;
}
};


class ProductB : public Product
{
public:
void Show()
{
cout << "it is Product B" << endl;
}
};


class Factory
{
public:
Product *CreateProduct(const char *ch)
{
if (!strcmp(ch, "ProductA"))
{
return new ProductA;
}
else if (!strcmp(ch, "ProductB"))
{
return new ProductB;
}
else
{
cout << "pls input a right product type" << endl;
return NULL;
}
}
};


int main()
{
Factory* Fac = new Factory();
//工厂制造A产品
Product* Pro_A = Fac->CreateProduct("ProductA");
Pro_A->Show();
//工厂制造B产品
Product* Pro_B = Fac->CreateProduct("ProductB");
Pro_B->Show();
system("pause");
return 0;

}


2. 工厂模式--每增加一个新的产品形态就需要增加一个Factory类

#include <iostream>
#include <vector>
using namespace std;


class Product
{
public :
virtual void Show() = 0;
};


class ProductA : public Product
{
public :
void Show()
{
cout << "it is Product A" << endl;
}
};


class ProductB : public Product
{
public:
void Show()
{
cout << "it is Product B" << endl;
}
};


class Factory
{
public:
virtual Product *CreateProduct() = 0;
};


class Factory_A : public Factory
{
public :
Product* CreateProduct()
{
return new ProductA;
}
};


class Factory_B : public Factory
{
public:
Product* CreateProduct()
{
return new ProductB;
}
};


int main()
{
//工厂制造A产品
Factory* Fac_A = new Factory_A();
Product* Pro_A = Fac_A->CreateProduct();
Pro_A->Show();
//工厂制造B产品
Factory* Fac_B = new Factory_B();
Product* Pro_B = Fac_B->CreateProduct();
Pro_B->Show();
system("pause");
return 0;
}


3. 抽象工厂模式--A工厂制造A类产品,B工厂制造B类产品

#include <iostream>
#include <vector>
using namespace std;


class Product_1
{
public :
virtual void Show() = 0;
};


class Product_A1 : public Product_1
{
public :
void Show()
{
cout << "it is Product A1" << endl;
}
};


class Product_B1 : public Product_1
{
public:
void Show()
{
cout << "it is Product B1" << endl;
}
};


class Product_2
{
public:
virtual void Show() = 0;
};


class Product_A2 : public Product_2
{
public:
void Show()
{
cout << "it is Product A2" << endl;
}
};


class Product_B2 : public Product_2
{
public:
void Show()
{
cout << "it is Product B2" << endl;
}
};


class Factory
{
public:
virtual Product_1 *CreateProduct_1() = 0;
virtual Product_2 *CreateProduct_2() = 0;
};


class Factory_A : public Factory
{
public :
Product_1* CreateProduct_1()
{
return new Product_A1;
}
Product_2* CreateProduct_2()
{
return new Product_A2;
}
};


class Factory_B : public Factory
{
public:
Product_1* CreateProduct_1()
{
return new Product_B1;
}
Product_2* CreateProduct_2()
{
return new Product_B2;
}
};


int main()
{
//工厂制造A1产品
Factory* Fac_A1 = new Factory_A();
Product_1* Pro_A1 = Fac_A1->CreateProduct_1();
Pro_A1->Show();


//工厂制造A2产品
Factory* Fac_A2 = new Factory_A();
Product_2* Pro_A2 = Fac_A2->CreateProduct_2();
Pro_A2->Show();


//工厂制造B1产品
Factory* Fac_B1 = new Factory_B();
Product_1* Pro_B1 = Fac_B1->CreateProduct_1();
Pro_B1->Show();


//工厂制造B2产品
Factory* Fac_B2 = new Factory_B();
Product_2* Pro_B2 = Fac_B2->CreateProduct_2();
Pro_B2->Show();
system("pause");
return 0;
}

原创粉丝点击