简单工厂模式解决的问题是如何去实例化一个合适的对象
简单工厂模式的核心思想就是:有一个专门的类来负责创建实例的过程
1)还没有工厂时代:假如还没有工业革命,如果一个客户要一款宝马车,一般的做法是客户去创建一款宝马车,然后拿来用。
2)简单工厂模式:后来出现工业革命。用户不用去创建宝马车。因为客户有一个工厂来帮他创建宝马.想要什么车,这个工厂就可以建。比如想要320i系列车。工厂就创建这个系列的车。即工厂可以创建产品。
3)工厂方法模式时代:为了满足客户,宝马车系列越来越多,如320i,523i,30li等系列一个工厂无法创建所有的宝马系列。于是由单独分出来多个具体的工厂。每个具体工厂创建一种系列。即具体工厂类只能创建一个具体产品。但是宝马工厂还是个抽象。你需要指定某个具体的工厂才能生产车出来。
4)抽象工厂模式时代:随着客户的要求越来越高,宝马车必须配置空调。而且这空调必须对应给系列车才能使用。于是这个工厂开始生产宝马车和需要的空调。
最终是客户只要对宝马的销售员说:我要523i空调车,销售员就直接给他523i空调车了。而不用自己去创建523i空调车宝马车.
#include<iostream>
using namespace std;
class Product
{
public:
virtual void show() = 0;
};
class ProductA :public Product
{
public:
void show()
{
cout << "product A" << endl;
}
};
class ProductB :public Product
{
public:
void show()
{
cout << "product B" << endl;
}
};
class Factory
{
public:
virtual Product *Create() = 0;
};
class FactoryA:public Factory
{
public:
Product *Create()
{
return new ProductA();
}
};
class FactoryB :public Factory
{
public:
Product *Create()
{
return new ProductB();
}
};
int main()
{
Factory *factoryA = new FactoryA();
Product *productA = new ProductA();
productA->show();
Factory *factoryB = new FactoryB();
Product *productB = new ProductB();
productB->show();
}
工厂方法模式:
#include <iostream>
using namespace std;
class Product
{
public:
virtual void Show() = 0;
};
class ProductA : public Product
{
public:
void Show()
{
cout<< "I'm ProductA"<<endl;
}
};
class ProductB : public Product
{
public:
void Show()
{
cout<< "I'm ProductB"<<endl;
}
};
class Factory
{
public:
virtual Product *CreateProduct() = 0;
};
class FactoryA : public Factory
{
public:
Product *CreateProduct()
{
return new ProductA ();
}
};
class FactoryB : public Factory
{
public:
Product *CreateProduct()
{
return new ProductB ();
}
};
int main(int argc , char *argv [])
{
Factory *factoryA = new FactoryA ();
Product *productA = factoryA->CreateProduct();
productA->Show();
Factory *factoryB = new FactoryB ();
Product *productB = factoryB->CreateProduct();
productB->Show();
if (factoryA != NULL)
{
delete factoryA;
factoryA = NULL;
}
if (productA != NULL)
{
delete productA;
productA = NULL;
}
if (factoryB != NULL)
{
delete factoryB;
factoryB = NULL;
}
if (productB != NULL)
{
delete productB;
productB = NULL;
}
return 0;
}
抽象工厂方法:
#include <iostream>
using namespace std;
// Product A
class ProductA
{
public:
virtual void Show() = 0;
};
class ProductA1 : public ProductA
{
public:
void Show()
{
cout<<"I'm ProductA1"<<endl;
}
};
class ProductA2 : public ProductA
{
public:
void Show()
{
cout<<"I'm ProductA2"<<endl;
}
};
// Product B
class ProductB
{
public:
virtual void Show() = 0;
};
class ProductB1 : public ProductB
{
public:
void Show()
{
cout<<"I'm ProductB1"<<endl;
}
};
class ProductB2 : public ProductB
{
public:
void Show()
{
cout<<"I'm ProductB2"<<endl;
}
};
// Factory
class Factory
{
public:
virtual ProductA *CreateProductA() = 0;
virtual ProductB *CreateProductB() = 0;
};
class Factory1 : public Factory
{
public:
ProductA *CreateProductA()
{
return new ProductA1();
}
ProductB *CreateProductB()
{
return new ProductB1();
}
};
class Factory2 : public Factory
{
ProductA *CreateProductA()
{
return new ProductA2();
}
ProductB *CreateProductB()
{
return new ProductB2();
}
};
int main(int argc, char *argv[])
{
Factory *factoryObj1 = new Factory1();
ProductA *productObjA1 = factoryObj1->CreateProductA();
ProductB *productObjB1 = factoryObj1->CreateProductB();
productObjA1->Show();
productObjB1->Show();
Factory *factoryObj2 = new Factory2();
ProductA *productObjA2 = factoryObj2->CreateProductA();
ProductB *productObjB2 = factoryObj2->CreateProductB();
productObjA2->Show();
productObjB2->Show();
if (factoryObj1 != NULL)
{
delete factoryObj1;
factoryObj1 = NULL;
}
if (productObjA1 != NULL)
{
delete productObjA1;
productObjA1= NULL;
}
if (productObjB1 != NULL)
{
delete productObjB1;
productObjB1 = NULL;
}
if (factoryObj2 != NULL)
{
delete factoryObj2;
factoryObj2 = NULL;
}
if (productObjA2 != NULL)
{
delete productObjA2;
productObjA2 = NULL;
}
if (productObjB2 != NULL)
{
delete productObjB2;
productObjB2 = NULL;
}
}