常用设计模式总结
一.单例模式(Singleton)
定义
保证一个类只有一个实例,并提供全局访问点。
适用场景
全局配置管理
日志系统
数据库连接池
C++ 示例(线程安全)
cpp
class Singleton {
public:
static Singleton& instance() {
static Singleton inst; // C++11 线程安全
return inst;
}
private:
Singleton() {}
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
};
二.工厂方法(Factory Method)
定义
将"对象的创建"延迟到子类中,由子类决定创建何种对象。
适用场景
子类决定创建逻辑
需要扩展新的产品类型
示例
cpp
class Product {
public:
virtual void show() = 0;
};
class AProduct : public Product {
public:
void show() override {}
};
class Factory {
public:
virtual Product* create() = 0;
};
三.抽象工厂(Abstract Factory)
定义
提供一个接口,用来创建一族相关或互相依赖的对象。
适用场景
跨平台 UI 库(Win/Mac/Linux)
需要保证产品族一致性(Button + TextBox)
示例
cpp
class UIFactory {
public:
virtual Button* createButton() = 0;
virtual TextBox* createTextBox() = 0;
};
与工厂方法对比
| 模式 | 关注点 |
|---|---|
| 工厂方法 | 单一产品的创建 |
| 抽象工厂 | 多个相关产品的创建(产品族) |
四. 代理模式(Proxy)
定义
给对象提供一个代理,控制对原对象的访问。
适用场景
虚拟代理(延迟加载图片)
远程代理(RPC)
权限代理(用户权限检查)
示例
cpp
class Image {
public:
virtual void display() = 0;
};
class RealImage : public Image {
public:
RealImage(string f) { /* load */ }
void display() override {}
};
class ProxyImage : public Image {
RealImage* real = nullptr;
string file;
public:
ProxyImage(string f): file(f) {}
void display() override {
if (!real) real = new RealImage(file); // 延迟加载
real->display();
}
};
五. 装饰器模式(Decorator)
定义
动态地给对象添加额外功能,比继承更灵活。
适用场景
C++ IO 流
图像处理滤镜(叠加处理)
动态增强功能(日志、缓存等)
示例
cpp
class Coffee {
public:
virtual int cost() = 0;
};
class SimpleCoffee : public Coffee {
public:
int cost() override { return 10; }
};
class MilkDecorator : public Coffee {
Coffee* base;
public:
MilkDecorator(Coffee* c): base(c) {}
int cost() override { return base->cost() + 2; }
};
与代理模式的区别
装饰器:增强功能
代理:控制访问
六. 观察者模式(Observer)
定义
对象间一对多依赖,当一个对象改变时自动通知所有观察者。
使用场景
GUI 事件系统(Qt 信号槽)
发布订阅系统
数据变化通知
示例
cpp
class Observer {
public:
virtual void update(int msg) = 0;
};
class Subject {
vector<Observer*> list;
public:
void attach(Observer* o) { list.push_back(o); }
void notify(int m) {
for (auto o : list) o->update(m);
}
};
七. 责任链模式(Chain of Responsibility)
定义
让多个对象有机会处理请求,形成一个链路。
使用场景
日志等级(INFO → WARN → ERROR)
拦截器链(Web 服务器)
权限校验流程
示例
cpp
class Handler {
protected:
Handler* next = nullptr;
public:
void setNext(Handler* n) { next = n; }
virtual void handle(int level) {
if (next) next->handle(level);
}
};
八. 策略模式(Strategy)
定义
将算法独立封装,使算法可以互换。
使用场景
支付策略(微信 / 支付宝)
排序/搜索策略
游戏 AI 选择行为
示例
cpp
class Strategy {
public:
virtual int run(int a, int b) = 0;
};
class Add : public Strategy {
public:
int run(int a, int b) override { return a+b; }
};
class Context {
Strategy* s;
public:
Context(Strategy* s): s(s) {}
int exec(int a, int b) { return s->run(a, b); }
};
九. 适配器模式(Adapter)
定义
将一个类的接口转换成客户需要的另一种接口。
使用场景
旧接口适配新 API
兼容第三方库
示例
cpp
class OldAPI {
public:
int out220() { return 220; }
};
class Adapter {
OldAPI* old;
public:
Adapter(OldAPI* o): old(o) {}
int out5() { return 5; }
};
十. 模板方法模式(Template Method)
定义
定义算法的骨架,将其中某些步骤延迟到子类。
使用场景
处理流程固定,但某步骤可自定义
数据导入导出流程
示例
cpp
class Drink {
public:
void make() {
boil();
brew();
pour();
}
protected:
void boil() { cout << "Boiling\n"; }
virtual void brew() = 0;
void pour() { cout << "Pouring\n"; }
};
十一. 外观模式(Facade)
定义
为复杂子系统提供一个统一的简化接口。
使用场景
一键启动(CPU + Disk + Memory)
简化复杂库(如 FFmpeg)
示例
cpp
class CPU {
public:
void start() {}
};
class Disk {
public:
void read() {}
};
class Computer {
CPU c;
Disk d;
public:
void start() {
c.start();
d.read();
}
}
十二. 建造者模式(Builder)
定义
将复杂对象的构建步骤和最终表示分离。
使用场景
电脑装机
游戏角色构建
复杂对象(JSON、XML)
示例
cpp
class Builder {
public:
virtual void buildCPU() = 0;
virtual void buildGPU() = 0;
};
十三. 命令模式(Command)
定义
将操作封装为命令对象,可支持撤销、排队。
使用场景
撤销/重做
宏命令
任务队列
示例
cpp
class Command {
public:
virtual void execute() = 0;
};
十四. 状态模式(State)
定义
对象行为随着状态的改变而发生变化。
使用场景
游戏角色(移动/跳跃/死亡)
电梯状态机
网络连接状态
示例
cpp
class State {
public:
virtual void handle() = 0;
};