设计模式简介
面向对象设计原则
- 依赖倒置原则(DIP)
- 高层模块(稳定)不应该依赖于底层模块(变化),二者都应该依赖于抽象(稳定)
- 抽象(稳定)不应该依赖于实现细节(变化),实现细节应该依赖于抽象(稳定)
- 开放封闭原则(OCP)
- 对扩展开放,对更改封闭
- 类模块应该是可扩展的,但是不可修改
- 单一职责原则(SRP)
- 一个类应该仅有一个引起它变化的原因
- 变化的方向隐含着类的责任
- 替换原则(LSP)
- 子类必须能够替换他们的基类(IS-A)
- 继承表达类型抽象
- 接口隔离原则(ISP)
- 不应该强迫客户程序依赖他们不用的方法
- 接口应该小而完备
- 优先使用对象组合,而非类继承
- 类继承通常为“白箱复用”,对象组合通常为“黑箱复用”
- 继承在某种程度上破坏了封装性,子类父类耦合度高
- 而对象组合则只要求被组合的对象具有良好定义的接口,耦合度低
- 封装变化点
- 使用封装来创建对象之间的分界层,让设计者可以在分界层的一侧进行修改,而不会对另一侧产生不良的影响,从而实现层次间的松耦合
- 针对接口编程,而非针对实现编程
- 不将变量类型声明为某个特定的具体类,而是声明为某个接口
- 客户程序无需获知对象的具体类型,只需要知道对象所具有的接口
- 减少系统中各部分的依赖关系,从而实现“高内聚、松耦合”的类型设计方案
重构关键技法
- 静态 ~> 动态
- 早绑定 ~> 晚绑定
- 继承 ~> 组合
- 编译时依赖 ~> 运行时依赖
- 紧耦合 ~> 松耦合
设计模式(23种)
中文名 | 英文名 | 简要说明 | 关键词 |
---|
抽象工厂模式 | Abstract Factory | 提供一个接口,可创建一系列相关或相互依赖的对象,而无需指定它们具体的类 | 生成系列对象 |
构建器模式 | Builder | 将一个复杂类的表示与其构造相分离,使得相同的构建过程能够得出不同的表示 | 复杂对象构造 |
工厂方法模式 | Factory Method | 定义一个创建对象的接口,但由子类决定需要实例化哪个类,工厂方法使得子类实例化的过程推迟 | 动态生成对象 |
原型模式 | Prototype | 用原型实例指定创建对象的类型,并且通过拷贝这个原型来创建新的对象 | 克隆对象 |
单例模式 | Singleton | 保证一个类只有一个实例,并提供一个访问它的全局方法 | 单实例 |
中文名 | 英文名 | 简要说明 | 关键词 |
---|
适配器模式 | Adapter | 将一个类的接口转换成用户希望的另一个接口,它使原本不相容的接口得以协同工作 | 转换接口 |
桥接模式 | Bridge | 将类的抽象部分和它的实现部分分离,使它们可以独立的变化 | 继承树拆分 |
组合模式 | Composite | 将对象组合成树型结构以表示“整体-部分”的层次结构,使用户对单个对象和组合对象的使用具有一致性 | 树型目录结构 |
装饰模式 | Decorator | 动态的给一个对象添加一些额外的职责,它提供了用子类扩展功能的灵活的替代(比派生子类更灵活) | 附加职责 |
外观模式 | Facade | 定义一个高层接口,为子系统中的一组接口提供一个一致的外观,从而简化了孩子系统 | 对外统一接口 |
享元模式 | Flyweight | 提供支持大量细粒度对象共享的有效方法 | 文章共享文字对象 |
代理模式 | Proxy | 为其他对象提供一种代理以控制这个对象的访问 | 代理 |
中文名 | 英文名 | 简要说明 | 关键词 |
---|
职责链模式 | Chain of Responsibility | 通过给多个对象处理请求的机会,减少请求的发送者与接受者之间的耦合。将接收对象链接起来,在链中传递请求,直到有一个对象处理这个请求 | 传递职责 |
命令模式 | Command | 将一个请求封装为一个对象,从而可用不同的请求对客户进行参数化,将请求排队或记录请求日志,支持可撤销操作 | 日志记录、可撤销 |
解释器模式 | Interpreter | 给定一种语言,定义它的文法表示,并定义一个解释器,该解释器用来根据文法表示来解释语言中的句子 | 虚拟机的机制 |
迭代器模式 | Iterator | 提供一种方法来顺序访问一个聚合对象中的各个元素,而不需要暴露该对象的内部表示 | 数据库数据集 |
中介者模式 | Mediator | 用一个中介对象来封装一系列的对象交互。它使各对象不需要显示的相互调用,从而达到低耦合,还可以独立的改变对象间的交互 | 不直接引用 |
备忘录模式 | Memento | 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,从而可以在以后将该对象回复到原先保存的状态 | |
观察者模式 | Observer | 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知并自动更新 | 联动 |
状态模式 | State | 允许一个对象在其内部状态改变时改变它的行为 | 状态变成类 |
策略模式 | Strategy | 定义一系列算法,把它们一个个封装起来,并且使它们之间可互相替换,从而让算法可以独立于使用它的用户而变化 | 多方案切换 |
模板方法模式 | Template Method | 定义一个操作中算法骨架,而将一些步骤延迟到子类中,使得子类可以不改变一个算法的结构即可重新定义算法的某些特定步骤 | |
访问者模式 | Visitor | 表示一个作用于某对象结构中的各元素的操作,使得在不改变各元素的类的前提下定义作用于这些元素的新操作 | |
组件协作
TemplateMethod(模板方法模式)
#include <iostream>
#include <string>
using namespace std;
//lib developer
class Library{
public:
virtual ~Library(){}
void Run(){
Step1();
if (Step2()){
Step3();
}
}
protected:
void Step1(){
cout << "Step1" << endl;
}
void Step3(){
cout << "Step3" << endl;
}
virtual bool Step2() = 0;
};
class App : public Library{
public:
App(){}
virtual ~App(){}
protected:
virtual bool Step2(){
cout << "Step2" << endl;
return true;
}
};
int main(int argc, char* argv[])
{
cout << "template method" << endl;
Library* lib = new App;
lib->Run();
delete lib;
system("pause");
return 0;
}
Strategy(策略模式)
#include <iostream>
#include <string>
using namespace std;
class IStrategy
{
public:
virtual int DoOperation(int num1, int num2) = 0;
};
class AddOperation : public IStrategy
{
public:
virtual int DoOperation(int num1, int num2) override
{
return num1 + num2;
}
};
class SubstractOperation : public IStrategy
{
public:
virtual int DoOperation(int num1, int num2) override
{
return num1 - num2;
}
};
class Context
{
public:
Context(IStrategy* InStrategy)
: Strategy(InStrategy)
{
}
int exec(int num1, int num2)
{
return Strategy->DoOperation(num1, num2);
}
private:
IStrategy* Strategy;
};
int main(int argc, char* argv[])
{
cout << "strategy" << endl;
Context* cxt = new Context(new AddOperation);
cout << "1+2=" << cxt->exec(1, 2) << endl;
cxt = new Context(new SubstractOperation);
cout << "1-2=" << cxt->exec(1, 2) << endl;
system("pause");
return 0;
}
Observer(观察者模式)
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
using namespace std;
class IProgressObject
{
public:
virtual void DoProgress(float value) = 0;
virtual ~IProgressObject(){};
};
class NumNotifier :public IProgressObject
{
public:
virtual void DoProgress(float value){
cout << setiosflags(ios::fixed) << setprecision(1) << value << endl;
}
};
class PointNotifier : public IProgressObject
{
public:
virtual void DoProgress(float value){
cout << "#"<< endl;
}
};
class Reader
{
public:
Reader(const vector<string>& InStrings)
: stringList(InStrings)
{}
void addProgress(IProgressObject* InProgressObject){
progressObjectList.push_back(InProgressObject);
}
void removeProgress(IProgressObject* InProgressObject){
progressObjectList.remove(InProgressObject);
}
void start(){
for (unsigned int i = 0; i < stringList.size(); i++){
float progressValue = (float)(i + 1) / stringList.size();
cout << stringList[i] << endl;
onProgress(progressValue);
}
}
protected:
virtual void onProgress(float value){
for (list<IProgressObject*>::iterator iter = progressObjectList.begin(); iter != progressObjectList.end(); iter++){
(*iter)->DoProgress(value);
}
}
protected:
vector<string> stringList;
list<IProgressObject*> progressObjectList;
};
int main(int agrc, char* argv[])
{
cout << "observer" << endl;
NumNotifier nn;
PointNotifier pn;
vector<string> strings{"A","B","C","D","E","F","G","H","I","J"};
Reader reader(strings);
reader.addProgress(&nn);
reader.addProgress(&pn);
reader.start();
system("pause");
return 0;
}
单一职责
Decorator(装饰模式)
#include <iostream>
#include <string>
using namespace std;
class Stream
{
public:
virtual ~Stream(){};
virtual char Read(int num) = 0;
virtual void Seek(int pos) = 0;
virtual void Write(char data) = 0;
};
class FileStream : public Stream
{
public:
virtual char Read(int num) override
{
//读文件流
return 'r';
}
virtual void Seek(int pos) override
{
//定位文件流
}
virtual void Write(char data) override
{
//写文件流
}
};
class NetworkStream : public Stream
{
public:
virtual char Read(int num) override
{
//读文件流
return 'r';
}
virtual void Seek(int pos) override
{
//定位文件流
}
virtual void Write(char data) override
{
//写文件流
}
};
class DecoratorStream : public Stream
{
protected:
DecoratorStream(Stream* InStream)
:stream(InStream)
{}
Stream* stream;
};
class CryptoStream : public DecoratorStream
{
public:
CryptoStream(Stream* InStream)
: DecoratorStream(InStream)
{}
virtual char Read(int num) override
{
//加密操作
//......
//读文件流
return stream->Read(num);
}
virtual void Seek(int pos) override
{
//加密操作
//......
//定位文件流
stream->Seek(pos);
}
virtual void Write(char data) override
{
//加密操作
//......
//写文件流
stream->Write(data);
}
};
class BufferStream : public DecoratorStream
{
public:
BufferStream(Stream* InStream)
: DecoratorStream(InStream)
{}
virtual char Read(int num) override
{
//缓存操作
//......
//读文件流
return stream->Read(num);
}
virtual void Seek(int pos) override
{
//缓存操作
//......
//定位文件流
stream->Seek(pos);
}
virtual void Write(char data) override
{
//缓存操作
//......
//写文件流
stream->Write(data);
}
};
int main(int argc, char* argv[])
{
cout << "decorator" << endl;
FileStream* fs = new FileStream();
CryptoStream* cs = new CryptoStream(fs);
BufferStream* bs = new BufferStream(cs);
delete bs;
delete cs;
delete fs;
system("pause");
return 0;
}
Bridge(桥接模式)
#include <iostream>
#include <string>
using namespace std;
//轮胎基类
class ITyre
{
public:
virtual void DealSomething() = 0;
};
//子午线轮胎
class RadialTyre : public ITyre
{
public:
virtual void DealSomething() override
{
cout << "RadialTyre" << endl;
}
};
//斜交轮胎
class BiasTyre : public ITyre
{
public:
virtual void DealSomething() override
{
cout << "BiasTyre" << endl;
}
};
//汽车基类
class ICar
{
public:
ICar(ITyre* InTyre)
: Tyre(InTyre)
{}
virtual void DoSomething(){};
protected:
ITyre* Tyre;
};
//轿车
class SedanCar : public ICar
{
public:
SedanCar(ITyre* InTyre)
: ICar(InTyre)
{}
virtual void DoSomething() override
{
cout << "SedanCar" << endl;
Tyre->DealSomething();
}
};
//SUV
class SuvCar : public ICar
{
public:
SuvCar(ITyre* InTyre)
: ICar(InTyre)
{}
virtual void DoSomething() override
{
cout << "SuvCar" << endl;
Tyre->DealSomething();
}
};
int main(int argc, char* argv[])
{
cout << "bridge" << endl;
ICar* Car = new SuvCar(new RadialTyre());
Car->DoSomething();
Car = new SedanCar(new BiasTyre());
Car->DoSomething();
system("pause");
return 0;
}
对象创建
Factory Method(工厂方法)
#include <iostream>
#include <string>
using namespace std;
class IShape
{
public:
virtual ~IShape(){}
virtual void Draw() = 0;
};
class Rectangle : public IShape
{
public:
virtual void Draw() override
{
cout << "rectangle" << endl;
}
};
class Circle : public IShape
{
public:
virtual void Draw() override
{
cout << "circle" << endl;
}
};
class ShapeFactory
{
public:
virtual ~ShapeFactory(){}
virtual IShape* CreateShape() = 0;
};
class RectangleFactory : public ShapeFactory
{
public:
virtual IShape* CreateShape() override
{
return new Rectangle;
}
};
class CircleFactory : public ShapeFactory
{
public:
virtual IShape* CreateShape() override
{
return new Circle;
}
};
class Test
{
public:
Test(ShapeFactory* InFactory)
: Factory(InFactory)
{}
IShape* CreateShape(){
return Factory->CreateShape();
}
private:
ShapeFactory* Factory;
};
int main(int argc, char* argv[])
{
cout << "factory method" << endl;
Test t(new CircleFactory);
IShape* s = t.CreateShape();
s->Draw();
system("pause");
return 0;
}
Abstract Factory(抽象工厂模式)
#include <iostream>
#include <string>
using namespace std;
class IMouse
{
public:
virtual ~IMouse(){}
virtual void Produce() = 0;
};
class LenovoMouse : public IMouse
{
public:
virtual void Produce() override
{
cout << "Lenovo Mouse" << endl;
}
};
class MacMouse : public IMouse
{
public:
virtual void Produce() override
{
cout << "Mac Mouse" << endl;
}
};
class IKeyboard
{
public:
virtual ~IKeyboard(){}
virtual void Produce() = 0;
};
class LenovoKeyboard : public IKeyboard
{
public:
virtual void Produce() override
{
cout << "Lenovo Keyboard" << endl;
}
};
class MacKeyboard : public IKeyboard
{
public:
virtual void Produce() override
{
cout << "Mac Keyboard" << endl;
}
};
class ComputerFactory
{
public:
virtual IMouse* ProduceMouse() = 0;
virtual IKeyboard* ProduceKeyboard() = 0;
};
class LenovoFactory :public ComputerFactory
{
public:
virtual IMouse* ProduceMouse() override
{
return new LenovoMouse;
}
virtual IKeyboard* ProduceKeyboard() override
{
return new LenovoKeyboard;
}
};
class MacFactory :public ComputerFactory
{
public:
virtual IMouse* ProduceMouse() override
{
return new MacMouse;
}
virtual IKeyboard* ProduceKeyboard() override
{
return new MacKeyboard;
}
};
class Test
{
public:
Test(ComputerFactory* InFactory)
: Factory(InFactory)
{}
IMouse* ProduceMouse()
{
return Factory->ProduceMouse();
}
IKeyboard* ProduceKeyboard()
{
return Factory->ProduceKeyboard();
}
private:
ComputerFactory* Factory;
};
int main(int argc, char* argv[])
{
cout << "abstract factory" << endl;
Test lf(new LenovoFactory);
lf.ProduceMouse()->Produce();
lf.ProduceKeyboard()->Produce();
Test mf = new MacFactory;
mf.ProduceMouse()->Produce();
mf.ProduceKeyboard()->Produce();
system("pause");
return 0;
}
Prototype(原型模式)
#include <iostream>
#include <string>
using namespace std;
class IShape
{
public:
virtual ~IShape(){};
virtual void Draw() = 0;
virtual IShape* Clone() = 0;
};
class Rectangle : public IShape
{
public:
virtual void Draw() override
{
cout << "Rectangle" << endl;
}
virtual IShape* Clone() override
{
return new Rectangle(*this);
}
};
class Circle : public IShape
{
public:
virtual void Draw() override
{
cout << "Circle" << endl;
}
virtual IShape* Clone() override
{
return new Circle(*this);
}
};
class Test
{
public:
Test(IShape* InShape)
: Shape(InShape)
{}
IShape* CreateShape()
{
return Shape->Clone();
}
private:
IShape* Shape;
};
int main(int argc, char* argv[])
{
cout << "prototype" << endl;
IShape* prototype = new Circle;
Test t(prototype);
t.CreateShape()->Draw();
system("pause");
return 0;
}
Builder(构建器)
#include <iostream>
#include <string>
using namespace std;
class Computer
{
public:
Computer(){}
virtual ~Computer(){};
void SetMouse(const string& InMouse)
{
mMouse = InMouse;
}
void SetKeyboard(const string& InKeyboard)
{
mKeyboard = InKeyboard;
}
void SetDisplay(const string& InDisplay)
{
mDisplay = InDisplay;
}
void Show()
{
cout << "Mouse:" << mMouse << endl;
cout << "Keyboard:" << mKeyboard << endl;
cout << "Display:" << mDisplay << endl;
}
protected:
string mMouse;
string mKeyboard;
string mDisplay;
};
class Builder
{
public:
Builder(){}
virtual ~Builder(){}
virtual void BuildMouse(const string& InMouse) = 0;
virtual void BuildKeyboard(const string& InKeyboard) = 0;
virtual void BuildDisplay(const string& InDisplay) = 0;
virtual void CreateProduct() = 0;
virtual Computer* GetProduct() = 0;
};
class ComputerBuilder : public Builder
{
public:
ComputerBuilder()
: mComputer(NULL)
{}
virtual void BuildMouse(const string& InMouse) override
{
mComputer->SetMouse(InMouse);
}
virtual void BuildKeyboard(const string& InKeyboard) override
{
mComputer->SetKeyboard(InKeyboard);
}
virtual void BuildDisplay(const string& InDisplay) override
{
mComputer->SetDisplay(InDisplay);
}
virtual void CreateProduct() override
{
mComputer = new Computer();
}
virtual Computer* GetProduct() override
{
return mComputer;
}
private:
Computer* mComputer;
};
class Director
{
public:
Director(Builder* InBuild)
: mBuilder(InBuild)
{}
void Construct(const string& InMouse, const string& InKeyboard, const string& InDisplay)
{
if (!mBuilder) return;
mBuilder->CreateProduct();
mBuilder->BuildMouse(InMouse);
mBuilder->BuildKeyboard(InKeyboard);
mBuilder->BuildDisplay(InDisplay);
}
private:
Builder* mBuilder;
};
int main(int argc, char* argv[])
{
cout << "builder" << endl;
Builder* b = new ComputerBuilder;
Director* d = new Director(b);
d->Construct("sb","jp","xsq");
Computer* c = b->GetProduct();
c->Show();
system("pause");
return 0;
}
对象性能
Singleton(单例模式)
#include <iostream>
#include <string>
using namespace std;
class Singleton
{
public:
static Singleton& GetInstance()
{
static Singleton mInstance;
return mInstance;
}
void Show(){
cout << "singleton instance" << endl;
}
private:
Singleton(){}
Singleton(Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
};
int main(int argc, char* argv[])
{
cout << "singleton" << endl;
Singleton::GetInstance().Show();
system("pause");
return 0;
}
Flyweight(享元模式)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Character
{
public:
virtual ~Character(){}
//外部状态应用
virtual void Display(int InWidth, int InHeight, int InColor) = 0;
//获取内部状态
virtual char GetSymbol() = 0;
protected:
Character(char c)
: symbol(c)
{}
protected:
//内部状态
char symbol;
//外部状态
int width;
int height;
int color;
};
class ConcreteCharacter : public Character
{
public:
ConcreteCharacter(char c)
: Character(c)
{}
virtual void Display(int InWidth, int InHeight, int InColor) override
{
width = InWidth;
height = InHeight;
color = InColor;
cout << width << " " << height << " " << color << endl;
}
virtual char GetSymbol() override
{
return symbol;
}
};
class CharacterFactory
{
public:
CharacterFactory(){};
Character* GetCharacter(char c)
{
for (vector<Character*>::iterator iter = mCharacters.begin(); iter != mCharacters.end(); iter++)
{
if ((*iter)->GetSymbol() == c)
{
return *iter;
}
}
Character* newc = new ConcreteCharacter(c);
mCharacters.push_back(newc);
return newc;
}
vector<Character*>::size_type GetCount()
{
return mCharacters.size();
}
private:
vector<Character*> mCharacters;
};
int main(int argc, char* argv[])
{
cout << "flyweight" << endl;
int param = 0;
string test = "ABBCCCDDDDEF";
CharacterFactory* cf = new CharacterFactory;
for (string::iterator iter = test.begin(); iter != test.end(); iter++)
{
param += 10;
Character* c = cf->GetCharacter(*iter);
cout << c->GetSymbol() << endl;
c->Display(param, param, param);
}
cout << "Object Count = " << cf->GetCount() << endl;
delete cf;
system("pause");
return 0;
}
接口隔离
Facade(外观模式)
#include <iostream>
#include <string>
using namespace std;
enum LawsuitType
{
CRIMINAL,
CIVIL,
ECONOMIC,
ADMINISTRATION
};
class IBaseLawsuit
{
public:
virtual void Receive() = 0;
};
//刑事诉讼
class Criminal : public IBaseLawsuit
{
public:
virtual void Receive() override
{
cout << "Criminal Received" << endl;
}
void CriminalProcess()
{
cout << "Cirminal Process" << endl;
}
};
//民事诉讼
class Civil : public IBaseLawsuit
{
public:
virtual void Receive() override
{
cout << "Civil Received" << endl;
}
void CivilProcess()
{
cout << "Civil Process" << endl;
}
};
//经济诉讼
class Economic : public IBaseLawsuit
{
public:
virtual void Receive() override
{
cout << "Economic Received" << endl;
}
void EconomicProcess()
{
cout << "Economic Process" << endl;
}
};
//行政诉讼
class Administration : public IBaseLawsuit
{
public:
virtual void Receive() override
{
cout << "Administration Received" << endl;
}
void AdministrationProcess()
{
cout << "Administration Process" << endl;
}
};
//律师事务所
class LawFirm
{
public:
LawFirm()
{
mCirminal = new Criminal;
mCivil = new Civil;
mEconomic = new Economic;
mAdmin = new Administration;
}
void Receive(LawsuitType InType)
{
cout << "LawFirm Received" << endl;
switch (InType)
{
case CRIMINAL:
mCirminal->Receive();
mCirminal->CriminalProcess();
break;
case CIVIL:
mCivil->Receive();
mCivil->CivilProcess();
break;
case ECONOMIC:
mEconomic->Receive();
mEconomic->EconomicProcess();
break;
case ADMINISTRATION:
mAdmin->Receive();
mAdmin->AdministrationProcess();
break;
default:
break;
}
}
private:
Criminal* mCirminal;
Civil* mCivil;
Economic* mEconomic;
Administration* mAdmin;
};
int main(int argc, char* argv[])
{
cout << "facade" << endl;
LawFirm lf;
lf.Receive(LawsuitType::CIVIL);
system("pause");
return 0;
}
Proxy(代理模式)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//武者
class IKungfuMaster
{
public:
IKungfuMaster(const string& InId)
: id(InId)
{}
virtual ~IKungfuMaster(){};
virtual void Show() = 0;
virtual void DisplayIdentity() = 0;
protected:
string id;
};
class RealStar : public IKungfuMaster
{
public:
RealStar()
: IKungfuMaster("real star")
{}
virtual void Show() override
{
cout << id << " is acting" << endl;
}
virtual void DisplayIdentity()
{
cout << "i am " << id << endl;
}
};
class StarProxy : public IKungfuMaster
{
public:
StarProxy()
: IKungfuMaster("star proxy")
, rs(new RealStar)
{}
virtual void Show() override
{
rs->Show();
}
virtual void DisplayIdentity()
{
cout << "i am " << id << endl;
}
private:
RealStar* rs;
};
int main(int argc, char* argv[])
{
cout << "proxy" << endl;
IKungfuMaster* master = new StarProxy;
master->Show();
master->DisplayIdentity();
system("pause");
return 0;
}
Mediator(中介者模式)
#include <iostream>
#include <string>
using namespace std;
class IMediator;
//客户
class ICustomer
{
public:
virtual void SetMediator(IMediator* InMediator) = 0;
virtual void SendMessage(const string& InMessage) = 0;
virtual void GetMessage(const string& InMessage) = 0;
protected:
IMediator* mMediator;
};
//中介
class IMediator
{
public:
virtual void SetRenter(ICustomer* InRenter) = 0;
virtual void SetLandlord(ICustomer* InLandlord) = 0;
virtual void PassOnMessage(const string& InMessage, ICustomer* InSender) = 0;
};
//房东
class Landlord : public ICustomer
{
public:
Landlord(){}
virtual void SetMediator(IMediator* InMediator)
{
mMediator = InMediator;
}
virtual void SendMessage(const string& InMessage)
{
if (mMediator)
{
mMediator->PassOnMessage(InMessage, this);
}
}
virtual void GetMessage(const string& message)
{
cout << "landlord received a message:" << message << endl;
}
};
//租客
class Renter : public ICustomer
{
public:
Renter(){}
virtual void SetMediator(IMediator* InMediator) override
{
mMediator = InMediator;
}
virtual void SendMessage(const string& InMessage)
{
if (mMediator)
{
mMediator->PassOnMessage(InMessage, this);
}
}
virtual void GetMessage(const string& InMessage)
{
cout << "renter received a message:" << InMessage << endl;
}
};
//房地产中介
class Realtor : public IMediator
{
public:
Realtor()
: mRenter(nullptr)
, mLandlord(nullptr)
{}
virtual void SetRenter(ICustomer* InRenter)
{
mRenter = InRenter;
}
virtual void SetLandlord(ICustomer* InLandlord)
{
mLandlord = InLandlord;
}
virtual void PassOnMessage(const string& InMessage, ICustomer* InSender)
{
if (InSender == mRenter)
{
mLandlord->GetMessage(InMessage);
}
else if (InSender == mLandlord)
{
mRenter->GetMessage(InMessage);
}
}
protected:
ICustomer* mRenter;
ICustomer* mLandlord;
};
int main(int argc, char* argv[])
{
cout << "mediator" << endl;
//创建对象
Realtor* realtor = new Realtor;
Renter* renter = new Renter;
Landlord* landlord = new Landlord;
//设置对象关系
renter->SetMediator(realtor);
landlord->SetMediator(realtor);
realtor->SetRenter(renter);
realtor->SetLandlord(landlord);
//
renter->SendMessage("can i see the room?");
landlord->SendMessage("yes, of course!");
system("pause");
return 0;
}
Adapter(适配器模式)
#include <iostream>
#include <string>
using namespace std;
class IDataLine
{
public:
IDataLine(string InType)
: Type(InType)
{}
virtual string ConnectorType() = 0;
virtual void Connect() = 0;
protected:
string Type;
};
class DataLineVGA : public IDataLine
{
public:
DataLineVGA()
: IDataLine("VGA")
{}
virtual string ConnectorType() override
{
return Type;
}
virtual void Connect() override
{
cout << "signal transmission" << endl;
}
};
//旧接口
class VGAConnector
{
public :
VGAConnector(IDataLine* InVGA);
virtual void ConnectorType()
{
mDataLine->ConnectorType();
}
virtual void ConnectVGA()
{
mDataLine->Connect();
}
protected:
IDataLine* mDataLine;
};
//新接口
class HDMIConnector
{
public:
virtual void ConnectorType() = 0;
virtual void ConnectHDMI() = 0;
};
class Adapter : public HDMIConnector
{
public:
Adapter(IDataLine* InVGA)
: mDataLineVGA(InVGA)
{}
virtual void ConnectorType()
{
cout << "HDMI" << endl;
}
virtual void ConnectHDMI() override
{
ConnectorType();
mDataLineVGA->Connect();
}
protected:
IDataLine* mDataLineVGA;
};
int main(int argc, char* argv[])
{
cout << "adapter" << endl;
Adapter* a = new Adapter(new DataLineVGA);
a->ConnectHDMI();
delete a;
system("pause");
return 0;
}
状态变化
Memento(备忘录模式)
#include <iostream>
#include <string>
using namespace std;
class Memento
{
friend class Originator;
private:
Memento(const string& InState)
: mState(InState)
{}
~Memento(){}
void SetState(const string& InState)
{
mState = InState;
}
string GetState()
{
return mState;
}
private:
string mState;
};
//负责创建Memento,恢复内部数据
class Originator
{
public:
Originator(){}
Originator(const string& InState)
: mState(InState)
{}
~Originator(){}
Memento* CreateMemento()
{
return new Memento(mState);
}
void RestoreTo(Memento* InMemento)
{
mState = InMemento->GetState();
}
void SetState(const string& InState)
{
mState = InState;
}
void Print()
{
cout << "state:" << mState << endl;
}
private:
string mState;
};
int main(int argc, char* argv[])
{
cout << "memento" << endl;
Originator* o = new Originator("expire");
o->Print();
Memento* m = o->CreateMemento();
o->SetState("new");
o->Print();
o->RestoreTo(m);
o->Print();
system("pause");
return 0;
}
State(状态模式)
#include <iostream>
#include <string>
using namespace std;
class LightState
{
public:
virtual ~LightState(){}
virtual void Action() = 0;
virtual LightState* NextState() = 0;
};
class OpenState : public LightState
{
public:
virtual void Action() override
{
cout << "Open State" << endl;
}
virtual LightState* NextState() override;
static LightState* GetInstance()
{
if (Instance == nullptr)
{
Instance = new OpenState;
}
return Instance;
}
private:
static LightState* Instance;
};
class CloseState : public LightState
{
public:
virtual void Action() override
{
cout << "Close State" << endl;
}
virtual LightState* NextState() override
{
return OpenState::GetInstance();
}
static LightState* GetInstance()
{
if (Instance == nullptr)
{
Instance = new CloseState;
}
return Instance;
}
private:
static LightState* Instance;
};
LightState* OpenState::Instance = nullptr;
LightState* CloseState::Instance = nullptr;
LightState* OpenState::NextState()
{
return CloseState::GetInstance();
}
class Light
{
public:
Light()
: State(OpenState::GetInstance())
{}
void Action()
{
State->Action();
State = State->NextState();
}
private:
LightState* State;
};
int main(int argc, char* argv[])
{
cout << "state" << endl;
Light l;
l.Action();
l.Action();
system("pause");
return 0;
}
数据结构
Composite(组合模式)
#include <iostream>
#include <string>
#include <list>
using namespace std;
class TreeNode
{
public:
TreeNode(const string& InNodeName)
: NodeName(InNodeName)
{}
void Show()
{
cout << "Name:" << NodeName << endl;
for (list<TreeNode*>::iterator iter = Children.begin(); iter != Children.end(); iter++)
{
cout << " " << (*iter)->NodeName << endl;
}
}
void AddNode(TreeNode* InNode)
{
Children.push_back(InNode);
}
void RemoveNode(TreeNode* InNode)
{
Children.remove(InNode);
}
private:
string NodeName;
list<TreeNode*> Children;
};
int main(int argc, char* argv[])
{
cout << "composite" << endl;
TreeNode* n = new TreeNode("RootNode");
TreeNode* cn = nullptr;
for (int i = 0; i < 10; i++)
{
string name = "LeafNode" + to_string(i);
cn = new TreeNode(name);
n->AddNode(cn);
}
n->Show();
system("pause");
return 0;
}
Iterator(迭代器模式)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<typename T>
class Iterator {
public:
virtual ~Iterator() {
}
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() = 0;
virtual bool CurrentItem(T& OutItem) = 0;
};
template<typename T>
class Container
{
public:
virtual ~Container(){}
virtual void AddItem(T InItem) = 0;
virtual Iterator<T>* CreateIterator() = 0;
virtual int GetSize() = 0;
virtual bool GetItem(int Index, T& OutItem) = 0;
};
template<typename T>
class ConcreteIterator : public Iterator<T>
{
public:
ConcreteIterator(Container<T>* InContainer)
: mContainer(InContainer)
, CurIndex(0)
{}
virtual void First() override
{
CurIndex = 0;
}
virtual void Next() override
{
if (CurIndex < mContainer->GetSize())
{
CurIndex++;
}
}
virtual bool IsDone() override
{
if (CurIndex > mContainer->GetSize() - 1)
{
return true;
}
return false;
}
virtual bool CurrentItem(T& OutItem) override
{
return mContainer->GetItem(CurIndex, OutItem);
}
private:
Container<T>* mContainer;
int CurIndex;
};
template<typename T>
class ConcreteContainer : public Container<T>
{
public:
ConcreteContainer()
: mSize(0)
{}
virtual void AddItem(T InItem) override
{
mData.push_back(InItem);
mSize++;
}
virtual Iterator<T>* CreateIterator() override
{
return new ConcreteIterator<T>(this);
}
virtual int GetSize() override
{
return mSize;
}
virtual bool GetItem(int Index, T& OutItem) override
{
if (Index < 0 || Index >= mSize)
{
return false;
}
OutItem = mData[Index];
return true;
}
private:
int mSize;
vector<T> mData;
};
int main(int argc, char* argv[])
{
cout << "iterator" << endl;
Container<int>* c = new ConcreteContainer<int>();
c->AddItem(1);
c->AddItem(2);
c->AddItem(3);
Iterator<int>* iter = c->CreateIterator();
for (iter->First(); !iter->IsDone(); iter->Next())
{
int value = 0;
bool bSuccess = iter->CurrentItem(value);
cout << "value:" << value << endl;
}
system("pause");
return 0;
}
Chain of Chain of Responsibility(责任链模式)
#include <iostream>
#include <string>
using namespace std;
class Request
{
public:
Request()
: mLevel(0)
{}
void SetLevel(int InLevel)
{
mLevel = InLevel;
}
int GetLevel() const
{
return mLevel;
}
private:
int mLevel;
};
class ChainHandler
{
public:
ChainHandler(int InLevel)
: mLevel(InLevel)
, mNextHandler(nullptr)
{}
void Process(const Request& InRequest)
{
if (CanHandleRequest(InRequest))
{
HandleRequest(InRequest);
}
else
{
PassToNextHandler(InRequest);
}
}
void SetNextHandler(ChainHandler* InHandler)
{
mNextHandler = InHandler;
}
int GetLevel() const
{
return mLevel;
}
protected:
virtual bool CanHandleRequest(const Request& InRequest) = 0;
virtual void HandleRequest(const Request& InRequest) = 0;
void PassToNextHandler(const Request& InRequest)
{
if (mNextHandler != nullptr)
{
mNextHandler->HandleRequest(InRequest);
}
}
protected:
int mLevel;
ChainHandler* mNextHandler;
};
class HandlerLevel : public ChainHandler
{
public:
HandlerLevel(int InLevel)
: ChainHandler(InLevel)
{}
protected:
virtual bool CanHandleRequest(const Request& InRequest) override
{
return InRequest.GetLevel() == GetLevel();
}
virtual void HandleRequest(const Request& InRequest) override
{
cout << "Handler Level:" << GetLevel() << endl;
cout << "Request Level:" << InRequest.GetLevel() << endl;
}
};
int main(int argc, char* argv[])
{
cout << "chain of responsibility" << endl;
ChainHandler* ch1 = new HandlerLevel(1);
ChainHandler* ch2 = new HandlerLevel(2);
ChainHandler* ch3 = new HandlerLevel(3);
ch1->SetNextHandler(ch2);
ch2->SetNextHandler(ch3);
Request q;
q.SetLevel(1);
ch1->Process(q);
q.SetLevel(2);
ch1->Process(q);
system("pause");
return 0;
}
行为变化
Command(命令模式)
#include <iostream>
#include <string>
#include <list>
using namespace std;
class ICommand
{
public:
virtual void Execute() = 0;
};
class ConcreteCommand : public ICommand
{
public:
ConcreteCommand(string InArgs)
: mArgs(InArgs)
{}
virtual void Execute() override
{
cout << "Execute, Args:" << mArgs << endl;
}
private:
string mArgs;
};
class ListCommand : public ICommand
{
public:
ListCommand(){}
void AddCommand(ICommand* InCommand)
{
mCommands.push_back(InCommand);
}
virtual void Execute() override
{
while (mCommands.size())
{
ICommand* c = *mCommands.begin();
mCommands.pop_front();
c->Execute();
}
}
private:
list<ICommand*> mCommands;
};
int main(int argc, char* argv[])
{
cout << "command" << endl;
ICommand* c1 = new ConcreteCommand("print1");
ICommand* c2 = new ConcreteCommand("print2");
ListCommand lc;
lc.AddCommand(c1);
lc.AddCommand(c2);
lc.Execute();
system("pause");
return 0;
}
Visitor(访问者模式)
#include <iostream>
#include <string>
using namespace std;
//假设固定一张试卷3个问题
//两个考生做题
class Question1;
class Question2;
class Question3;
class IVisitor
{
public:
virtual ~IVisitor(){};
virtual void Visit(Question1* InQuestion) = 0;
virtual void Visit(Question2* InQuestion) = 0;
virtual void Visit(Question3* InQuestion) = 0;
};
class ZhangSanVisitor : public IVisitor
{
public:
virtual void Visit(Question1* InQuestion) override
{
cout << "ZhangSan Answer Question1" << endl;
}
virtual void Visit(Question2* InQuestion) override
{
cout << "ZhangSan Answer Question2" << endl;
}
virtual void Visit(Question3* InQuestion) override
{
cout << "ZhangSan Answer Question3" << endl;
}
};
class LiSiVisitor : public IVisitor
{
public:
virtual void Visit(Question1* InQuestion) override
{
cout << "LiSi Answer Question1" << endl;
}
virtual void Visit(Question2* InQuestion) override
{
cout << "LiSi Answer Question2" << endl;
}
virtual void Visit(Question3* InQuestion) override
{
cout << "LiSi Answer Question3" << endl;
}
};
class IQuestion
{
public:
virtual ~IQuestion(){}
virtual void Answer(IVisitor* InVisitor) = 0;
};
class Question1 : public IQuestion
{
public:
virtual void Answer(IVisitor* InVisitor) override
{
InVisitor->Visit(this);
}
};
class Question2 : public IQuestion
{
public:
virtual void Answer(IVisitor* InVisitor) override
{
InVisitor->Visit(this);
}
};
class Question3 : public IQuestion
{
public:
virtual void Answer(IVisitor* InVisitor) override
{
InVisitor->Visit(this);
}
};
int main(int argc, char* argv[])
{
cout << "visitor" << endl;
Question1 q1;
Question2 q2;
Question3 q3;
IVisitor* v = new ZhangSanVisitor;
q1.Answer(v);
q2.Answer(v);
q3.Answer(v);
v = new LiSiVisitor;
q1.Answer(v);
q2.Answer(v);
q3.Answer(v);
system("pause");
return 0;
}
领域问题
Interpreter(解析器模式)
#include <iostream>
#include <string>
#include <map>
#include <stack>
using namespace std;
class Expression
{
public:
virtual int Interpreter(map<string, int>& InVar) = 0;
virtual ~Expression(){}
};
//终结符表达式(整数)
class VarExpression : public Expression
{
public:
VarExpression(string InKey)
: mKey(InKey)
{}
virtual int Interpreter(map<string, int>& InVar) override
{
return InVar[mKey];
}
private:
string mKey;
};
//符号表达式
class SymbolExpression : public Expression
{
public:
SymbolExpression(Expression* InLeft, Expression* InRight)
{
mLeft = InLeft;
mRight = InRight;
}
Expression* GetLeft()
{
return mLeft;
}
Expression* GetRight()
{
return mRight;
}
protected:
Expression* mLeft;
Expression* mRight;
};
class AddExpression : public SymbolExpression
{
public:
AddExpression(Expression* InLeft, Expression* InRight)
: SymbolExpression(InLeft, InRight)
{}
virtual int Interpreter(map<string, int>& InVar) override
{
return mLeft->Interpreter(InVar) + mRight->Interpreter(InVar);
}
};
class SubExpression : public SymbolExpression
{
public:
SubExpression(Expression* InLeft, Expression* InRight)
: SymbolExpression(InLeft, InRight)
{}
virtual int Interpreter(map<string, int>& InVar) override
{
return mLeft->Interpreter(InVar) - mRight->Interpreter(InVar);
}
};
class Calculator
{
public:
Calculator(string InExpStr)
{
mExp = nullptr;
stack<Expression*> ExpStack;
Expression* Left = nullptr;
Expression* Right = nullptr;
/*从左到向分析表达式(如:a+b-c),最终的语法树如下:
* -
* / \
* + c
* / \
* a b
*/
for (unsigned int i = 0; i < InExpStr.length(); i++)
{
switch (InExpStr[i])
{
case '+':
{
//从栈顶取出左表达式
Left = ExpStack.top();
ExpStack.pop();
//从表达式中取出+后面的右表达式
Right = new VarExpression(InExpStr.substr(++i, 1));
ExpStack.push(new AddExpression(Left, Right));
break;
}
case '-':
{
//从栈顶取出左表达式
Left = ExpStack.top();
ExpStack.pop();
//从表达式中取出+后面的右表达式
Right = new VarExpression(InExpStr.substr(++i, 1));
ExpStack.push(new SubExpression(Left, Right));
break;
}
default:
ExpStack.push(new VarExpression(InExpStr.substr(i, 1)));
break;
}
}
if (!ExpStack.empty())
{
mExp = ExpStack.top();
ExpStack.pop();
}
}
~Calculator()
{
DelExpTree(mExp);
mExp = nullptr;
}
int Run(map<string, int>& InVar)
{
return (mExp == nullptr) ? 0 : mExp->Interpreter(InVar);
}
private:
void DelExpTree(Expression* InExp)
{
SymbolExpression* branch = dynamic_cast<SymbolExpression*>(InExp);
//叶子结点
if (branch == NULL)
{
delete InExp;
}
else //分支结点
{
//左子树
DelExpTree(branch->GetLeft());
//右子树
DelExpTree(branch->GetRight());
//结点
delete InExp;
}
}
private:
Expression* mExp;
};
int main(int argc, char* argv[])
{
cout << "interpreter" << endl;
string ExpStr = "a+b-c";
map<string, int> var;
var["a"] = 200;
var["b"] = 300;
var["c"] = 100;
Calculator c(ExpStr);
cout << ExpStr << "=" << c.Run(var) << endl;
system("pause");
return 0;
}