1、简单工厂模式
简单工厂模式的特点是只有一个工厂,根据输入的条件不同,实例化不同的类。
#include <iostream>
class AbstractProduct{
public:
virtual void doSomething() = 0;
};
class ProductA : public AbstractProduct{
public:
virtual void doSomething(){
std::cout << "ProductA doSomething" << std::endl;
}
};
class ProductB : public AbstractProduct{
public:
virtual void doSomething(){
std::cout << "ProductB doSomething" << std::endl;
}
};
class Factory {
public:
static AbstractProduct* createProduct(int type){
AbstractProduct* product = nullptr;
switch (type) {
case 1:
product = new ProductA();
break;
case 2:
product = new ProductB();
break;
default:
product = new ProductA();
break;
}
return product;
}
};
int main() {
std::cout << "create product by 1" << std::endl;
AbstractProduct* product = Factory::createProduct(1);
product->doSomething();
delete product;
product = nullptr;
std::cout << "create product by 2" << std::endl;
product = Factory::createProduct(2);
product->doSomething();
delete product;
product = nullptr;
std::cout << "create product by 3" << std::endl;
product = Factory::createProduct(3);
product->doSomething();
delete product;
product = nullptr;
return 0;
}
输出:
create product by 1
ProductA doSomething
create product by 2
ProductB doSomething
create product by 3
ProductA doSomething
2、工厂方法模式
工厂方法模式的特点是定义一个创建对象的接口,然后通过子类确定实例化哪一个类的对象。(将类的实例化延迟到子类)
#include <iostream>
class AbstractProduct{
public:
virtual void doSomething() = 0;
};
class ProductA : public AbstractProduct{
public:
virtual void doSomething(){
std::cout << "ProductA doSomething" << std::endl;
}
};
class ProductB : public AbstractProduct{
public:
virtual void doSomething(){
std::cout << "ProductB doSomething" << std::endl;
}
};
class AbstarctFactory{
public:
virtual AbstractProduct* createProduct() = 0;
};
class FactoryA : public AbstarctFactory{
public:
virtual AbstractProduct* createProduct(){
return new ProductA();
}
};
class FactoryB : public AbstarctFactory{
public:
virtual AbstractProduct* createProduct(){
return new ProductB();
}
};
int main() {
AbstarctFactory* factory = new FactoryA();
AbstractProduct* product = factory->createProduct();
product->doSomething();
delete product;
product = nullptr;
delete factory;
factory = nullptr;
factory = new FactoryB();
product = factory->createProduct();
product->doSomething();
delete product;
product = nullptr;
delete factory;
factory = nullptr;
return 0;
}
输出:
ProductA doSomething
ProductB doSomething
3、抽象工厂模式
抽象工厂模式的特点是它是为创建一组相关或相互依赖的对象提供一个接口,而不用关系实现类。可以看做是工厂方法模式的升级版本。
#include <iostream>
class AbstractProductA{
public:
virtual void doSomething() = 0;
};
class ProductA1 : public AbstractProductA{
public:
virtual void doSomething(){
std::cout << "ProductA1 doSomething" << std::endl;
}
};
class ProductA2 : public AbstractProductA{
public:
virtual void doSomething(){
std::cout << "ProductA2 doSomething" << std::endl;
}
};
class AbstractProductB{
public:
virtual void doSomething2() = 0;
};
class ProductB1 : public AbstractProductB{
public:
virtual void doSomething2(){
std::cout << "ProductB1 doSomething2" << std::endl;
}
};
class ProductB2 : public AbstractProductB{
public:
virtual void doSomething2(){
std::cout << "ProductB2 doSomething2" << std::endl;
}
};
class AbstractFactory{
public:
virtual AbstractProductA* createProductA() = 0;
virtual AbstractProductB* createProductB() = 0;
};
class FactoryA : public AbstractFactory{
public:
virtual AbstractProductA* createProductA(){
return new ProductA1();
}
virtual AbstractProductB* createProductB(){
return new ProductB1();
}
};
class FactoryB : public AbstractFactory{
public:
virtual AbstractProductA* createProductA(){
return new ProductA2();
}
virtual AbstractProductB* createProductB(){
return new ProductB2();
}
};
int main() {
AbstractFactory* factoryA = new FactoryA();
AbstractFactory* factoryB = new FactoryB();
AbstractProductA* a1 = factoryA->createProductA();
AbstractProductA* a2 = factoryB->createProductA();
AbstractProductB* b1 = factoryA->createProductB();
AbstractProductB* b2 = factoryB->createProductB();
a1->doSomething();
a2->doSomething();
b1->doSomething2();
b2->doSomething2();
delete a1;
delete a2;
delete b1;
delete b2;
delete factoryA;
delete factoryB;
a1 = nullptr;
a2 = nullptr;
b1 = nullptr;
b2 = nullptr;
factoryA = nullptr;
factoryB = nullptr;
return 0;
}
输出:
ProductA1 doSomething
ProductA2 doSomething
ProductB1 doSomething2
ProductB2 doSomething2
|