- 简单工厂:不是设计模式,是用一工厂函数创建对象,加一层封装
- 工厂模式:对象的分类是一维的(如多个品牌的设备),每个分类有自己的工厂,增加新类型就多加一个类
- 抽象工厂模式:对象的分类是二维及以上的(如设备有好多种,每种设备还是多个品牌),一个分类用类实现,一个分类用函数实现。增加类型就加函数和类即可。
先放代码吧,有空再整理具体的
#include<iostream>
#include<string>
#include <vector>
#include <algorithm>
using namespace std;
enum MouseType {
enum_DellMouse,
enum_HpMouse
};
///
class Mouse{
public:
Mouse(){
cout << "Mouse Create" << endl;
}
};
class DellMouse:public Mouse{
public:
DellMouse(){
cout << "DellMouse Create" << endl;
}
};
class HpMouse:public Mouse{
public:
HpMouse(){
cout << "HpMouse Create" << endl;
}
};
///
class KeyBoard{
public:
KeyBoard(){
cout << "KeyBoard Create" << endl;
}
};
class DellKeyBoard:public KeyBoard{
public:
DellKeyBoard(){
cout << "DellKeyBoard Create" << endl;
}
};
class HpKeyBoard:public KeyBoard{
public:
HpKeyBoard(){
cout << "HpKeyBoard Create" << endl;
}
};
///
class Monitor{
public:
Monitor(){
cout << "Monitor Create" << endl;
}
};
class DellMonitor:public Monitor{
public:
DellMonitor(){
cout << "DellMonitor Create" << endl;
}
};
class HpMonitor:public Monitor{
public:
HpMonitor(){
cout << "HpMonitor Create" << endl;
}
};
class AsusMonitor:public Monitor{
public:
AsusMonitor(){
cout << "AsusMonitor Create" << endl;
}
};
///
// 简单工厂
class MouseFactory_simple: public Mouse{
public:
MouseFactory_simple(){}
Mouse* createMouse(MouseType name){
switch (name){
case MouseType::enum_DellMouse:
return new DellMouse();
break;
case MouseType::enum_HpMouse:
return new HpMouse();
break;
}
return new Mouse();
}
};
// 工厂模式
class MouseFactory:public Mouse{
public:
MouseFactory(){}
virtual Mouse* createMouse() = 0;
};
class DellMouseFactory:public MouseFactory{
public:
DellMouseFactory(){}
Mouse* createMouse(){
return new DellMouse();
}
};
class HpMouseFactory:public MouseFactory{
public:
HpMouseFactory(){}
Mouse* createMouse(){
return new HpMouse();
}
};
// 抽象工厂模式
class DevicesFactory_abstract:public Mouse, public KeyBoard{
public:
DevicesFactory_abstract(){}
virtual Mouse* createMouse() = 0;
virtual KeyBoard* createKeyBoard() = 0;
virtual Monitor* createMonitor() = 0;
};
class HpDevicesFactory_abstract:public DevicesFactory_abstract{
public:
HpDevicesFactory_abstract(){}
Mouse* createMouse(){
return new HpMouse();
}
KeyBoard* createKeyBoard(){
return new HpKeyBoard();
}
Monitor* createMonitor(){
return new HpMonitor();
}
};
class DellDevicesFactory_abstract:public DevicesFactory_abstract{
public:
DellDevicesFactory_abstract(){}
Mouse* createMouse(){
return new DellMouse();
}
KeyBoard* createKeyBoard(){
return new DellKeyBoard();
}
Monitor* createMonitor(){
return new DellMonitor();
}
};
class AsusDevicesFactory_abstract:public DevicesFactory_abstract{
public:
AsusDevicesFactory_abstract(){}
Mouse* createMouse(){
return nullptr;
}
KeyBoard* createKeyBoard(){
return nullptr;
}
Monitor* createMonitor(){
return new AsusMonitor();
}
};
// 对于抽象工厂来说如果增加一个牌子就加一个类,如果增加一个设备则加一个函数
int main()
{
/* 简单工厂
MouseFactory_simple* mouseFactory = new MouseFactory_simple();
Mouse* newDellMouse = mouseFactory->createMouse(MouseType::enum_HpMouse);
Mouse* newHpMouse = mouseFactory->createMouse(MouseType::enum_HpMouse );
*/
/* 工厂模式
DellMouseFactory dellMouseFactory;
Mouse* dellMouse =dellMouseFactory.createMouse();
HpMouseFactory hpMouseFactory;
Mouse* hpMouse =hpMouseFactory.createMouse();
*/
// 抽象工厂模式
HpDevicesFactory_abstract hpDevicesFactory_abstract;
DellDevicesFactory_abstract dellDevicesFactory_abstract;
AsusDevicesFactory_abstract asusDevicesFactory_abstract;
hpDevicesFactory_abstract.createMouse();
hpDevicesFactory_abstract.createKeyBoard();
hpDevicesFactory_abstract.createMonitor();
dellDevicesFactory_abstract.createMouse();
dellDevicesFactory_abstract.createKeyBoard();
dellDevicesFactory_abstract.createMonitor();
asusDevicesFactory_abstract.createMouse();
asusDevicesFactory_abstract.createKeyBoard();
asusDevicesFactory_abstract.createMonitor();
return 0;
}