什么是工厂模式?
一句话概括:工厂模式就是提供一个创建对象的接口,根据需求创建不同的子类对象
工厂模式分为三种:
简单工厂模式、工厂方法模式、抽象工厂模式
简单工厂模式
一个工厂,多个产品,产品需要有一个虚基类,通过传入参数,生成具体产品对象,并用基类指针指向该对象
c++代码:
#include<iostream>
#include<string>
#include<windows.h>
using namespace std;
enum {
typeA,
typeB
};
class product {
public:
virtual void show() = 0;
};
class productA : public product {
public:
virtual void show()
{
cout << "product A" << endl;
}
};
class productB : public product {
public:
virtual void show()
{
cout << "product B" << endl;
}
};
class Factory {
public:
static product* CreateObject(int type)
{
switch (type)
{
case typeA:
return new productA();
case typeB:
return new productB();
default:
break;
}
}
};
int main()
{
Factory* fy = new Factory();
product* Fa = fy->CreateObject(typeA);
Fa->show();
product* Fb = fy->CreateObject(typeB);
Fb->show();
system("pause");
return 0;
}
工厂方法模式
简单工厂模式会产生一个问题,这违反了设计模式的一个原则,也就是我们所说的开闭原则,就是说我们设计的类可以扩展,但是不能修改,什么意思呢?很明显如果我们此时加入了C类对象我们就需要对工厂方法进行代码的修改,而且每次加入都会影响代码于是工厂方法模式就产生了,多个工厂,多个产品,每个产品对应于一个工厂,此时产品和工厂都是通过虚基类的方式构建
c++代码:
#include<iostream>
#include<string>
#include<windows.h>
using namespace std;
class product {
public:
virtual void show() = 0;
};
class productA : public product{
public:
virtual void show()
{
cout << "product A" << endl;
}
};
class productB : public product{
public:
virtual void show()
{
cout << "product B" << endl;
}
};
class Factory{
public:
virtual product* CreateObject() = 0;
};
class FactoryA : public Factory{
public:
virtual product* CreateObject()
{
return new productA();
}
};
class FactoryB : public Factory{
public:
virtual product* CreateObject()
{
return new productB();
}
};
int main()
{
Factory* fya = new FactoryA();
product* Fa = fya->CreateObject();
Fa->show();
Factory* fyb = new FactoryB();
product* Fb = fyb->CreateObject();
Fb->show();
system("pause");
return 0;
}
抽象工厂模式
多个工厂,多个产品,并且每个产品可以包含多个型号。此时工厂和产品都是通过虚基类的方式构建。每一个工厂类可以生产同一个产品的多个型号。
什么意思?博主打个比方:就是说我们如果生产烟,只能生产好猫牌的香烟。可是我的工厂可能还能生产好狗牌的香烟,那么我们如何让一个工厂同时既能生产好猫牌香烟,又能生产好狗牌香烟呢?此时我们就要使用抽象工厂模式。c++代码如下:
#include<iostream>
#include<string>
#include<windows.h>
using namespace std;
class dog {
public:
virtual void show() = 0;
};
class dogA : public dog{
public:
virtual void show()
{
cout << "dog A" << endl;
}
};
class dogB : public dog{
public:
virtual void show()
{
cout << "dog B" << endl;
}
};
class cat {
public:
virtual void show() = 0;
};
class catA : public cat{
public:
virtual void show()
{
cout << "cat A" << endl;
}
};
class catB : public cat{
public:
virtual void show()
{
cout << "cat B" << endl;
}
};
class Factory{
public:
virtual dog* CreateDog() = 0;
virtual cat* CreateCat() = 0;
};
class FactoryA : public Factory{
public:
virtual dog* CreateDog()
{
return new dogA();
}
virtual cat* CreateCat()
{
return new catA();
}
};
class FactoryB : public Factory{
public:
virtual dog* CreateDog()
{
return new dogB();
}
virtual cat* CreateCat()
{
return new catB();
}
};
int main()
{
Factory* fya = new FactoryA();
dog* da = fya->CreateDog();
da->show();
cat* ca = fya->CreateCat();
ca->show();
cout << endl;
Factory* fyb = new FactoryB();
dog* db = fyb->CreateDog();
db->show();
cat* cb = fyb->CreateCat();
cb->show();
system("pause");
return 0;
}
工厂模式的优缺点
- 优点
1> 一个调用者想要创建对象,只要知道其名称就可以了
2> 屏蔽产品的具体实现,调用者只关心产品的接口 - 缺点
1> 除了工厂方法模式,其他两种模式都违反了开闭原则,制药违反开闭原则就要修改代码
2> 如果使用工厂模式,就需要引入一个工厂类,会增加系统复杂度