总体来说设计模式分为三大类:
创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
参考
https://www.csdn.net/gather_2a/MtzaYgwsNTEtYmxvZwO0O0OO0O0O.html
https://blog.csdn.net/taiyang1987912/article/category/2859245/2?
别人总结23种模式
#单例模式
确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。
#include
using namespace std;
class Singleton
{
private: //构造和成员变量 访问权限为private
Singleton()
{
}
static Singleton* instance;
public:
static Singleton* getInstance()
{
if (instance == NULL)
{
instance = new Singleton();
}
return instance;
}
void say()
{
cout << "我是唯一的" << endl;
}
};
/*********************************************************
静态变量初始化格式: <数据类型><类名>::<静态数据成员名>=<值>
前面不加static,以免与一般静态变量或对象相混淆
静态变量需在<类外>&&<实现>中初始化,不能在<.h>初始化
*********************************************************/
Singleton* Singleton::instance = 0;
int main()
{
Singleton * singleton = Singleton::getInstance();
singleton->say();
system("pause");
return 0;
}
单线程时这样写只有一个实例,而多线程时可能会有多个,需要用锁Lock() 和~Lock()
#工厂模式
##简单工厂(并不属于23种模式)
产品号定义如下:
enum PhoneType {
HuaweiType,
XiaomiType
};
产品如下:
//abstract product
class Phone {
public:
virtual void show() = 0;
};
//concrete product HuaWei
class HuaWeiPhone : public Phone {
public:
void show() {
std::cout << "HuaWeiPhone" << endl;
}
};
//concrete product XiaoMi
class XiaoMiPhone : public Phone {
public:
void show() {
std::cout << "XiaoMiPhone" << endl;
}
};
工厂如下:
//concrete factory
class PhoneFactory {
public:
Phone* CreatePhone(PhoneType type) {
if (HuaweiType == type) {
return new HuaWeiPhone();
} else if (XiaomiType == type) {
return new XiaoMiPhone();
}
}
};
main如下:
int main(int argc, char* argv[]) {
PhoneFactory factory;
Phone* phone = factory.CreatePhone(HuaweiType);
phone->show();
phone = factory.CreatePhone(XiaomiType);
phone->show();
system("pause");
return 0;
}
##工厂方法
产品同上
工厂发生变化:
//abstract factory
class Factory {
public:
virtual Phone* CreatePhone() = 0;
};
//concrete factory HuaWei
class HuaWeiFactory : public Factory {
public:
Phone* CreatePhone() {
return new HuaWeiPhone();
}
};
//concrete factory XiaoMi
class XiaoMiFactory : public Factory {
public:
Phone* CreatePhone() {
return new XiaoMiPhone();
}
};
main:
int main(int argc, char* argv[]) {
HuaWeiFactory huaWeiFactory;
Phone* phone = huaWeiFactory.CreatePhone();
phone->show();
XiaoMiFactory xiaoMiFactory;
phone = xiaoMiFactory.CreatePhone();
phone->show();
system("pause");
return 0;
}
只生产一个产品“phone”,可以加具体产品VIvoPhone,IPhone,增加具体工厂VivoFactory,IPhoneFactory,都是属于工厂方法
##抽象工厂
新增加产品watch:
//abstract product
class Watch {
public:
virtual void show() = 0;
};
//concrete product HuaWei
class HuaWeiWatch : public Watch {
public:
void show() {
std::cout << "HuaWeiWatch" << endl;
}
};
//concrete product XiaoMi
class XiaoMiWatch : public Watch {
public:
void show() {
std::cout << "XiaoMiWatch" << endl;
}
};
工厂如下:
//abstract factory
class Factory {
public:
virtual Phone* CreatePhone() = 0;
virtual Watch* CreateWatch() = 0;
};
//concrete factory HuaWei
class HuaWeiFactory : public Factory {
public:
Phone* CreatePhone() {
return new HuaWeiPhone();
}
Watch* CreateWatch() {
return new HuaWeiWatch();
}
};
//concrete factory XiaoMi
class XiaoMiFactory : public Factory {
public:
Phone* CreatePhone() {
return new XiaoMiPhone();
}
Watch* CreateWatch() {
return new XiaoMiWatch();
}
};
main如下:
int main(int argc, char* argv[]) {
HuaWeiFactory huaWeiFactory;
Phone* phone = huaWeiFactory.CreatePhone();
phone->show();
Watch* watch = huaWeiFactory.CreateWatch();
watch->show();
XiaoMiFactory xiaoMiFactory;
phone = xiaoMiFactory.CreatePhone();
phone->show();
watch = xiaoMiFactory.CreateWatch();
watch->show();
system("pause");
return 0;
}
总结:
简单工厂:
产品类型:enum,string,int等定义都行,反正表示一个产品号
工厂类有一个生产方法:根据输入产品号,函数体里根据产品号new对应的产品返回
工厂模式:
抽象工厂模式: