initialize repository of design pattern template

This commit is contained in:
LU
2025-08-09 08:41:14 +08:00
commit 1727e31b9d
32 changed files with 2149 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
#pragma once
#include "common.hpp"
class Shape // ÐÎ×´½Ó¿Ú
{
public:
virtual void draw() = 0;
};
class Rectangle :public Shape
{
public:
void draw();
};
class Square :public Shape
{
public:
void draw();
};
class Circle :public Shape
{
public:
void draw();
};
class Color // ÑÕÉ«½Ó¿Ú
{
public:
virtual void fill() = 0;
};
class Red :public Color
{
public:
void fill();
};
class Green :public Color
{
public:
void fill();
};
class Yellow :public Color
{
public:
void fill();
};
class AbstractFactory
{
public:
virtual Color* getColor(string color) = 0;
virtual Shape* getShape(string shape) = 0;
};
class ShapeFactory :public AbstractFactory
{
public:
Shape* getShape(string shape);
Color* getColor(string color);
};
class ColorFactory :public AbstractFactory
{
public:
Shape* getShape(string shape);
Color* getColor(string color);
};
class FactoryProducer
{
public:
AbstractFactory* getFactory(string choice);
};
+74
View File
@@ -0,0 +1,74 @@
#pragma once
#include "common.hpp"
class Manager;
class PersonA // Requester 请求(内容)
{
public:
PersonA(string name, int dayoff, string reason) :
m_Name(name), m_Dayoff(dayoff), m_Reason(reason) {
m_pLeader = nullptr;
};
int getDayoff();
string getName();
string getReason();
void setLeader(Manager* pLeader);
void Request();
private:
int m_Dayoff;
string m_Name;
string m_Reason;
Manager* m_pLeader;
};
class Manager // Resonsible 责任人抽象类
{
public:
Manager(string name, string title) :m_Name(name), m_Title(title)
{
m_pNextHandle = nullptr;
};
string getName();
string getTitle();
void setNextHander(Manager* p_NextHandler);
virtual void handleRequest(PersonA* pPerson) = 0;
protected:
string m_Name;
string m_Title;
Manager* m_pNextHandle;
};
class Supervisor :public Manager // 主管
{
public:
Supervisor(string name, string title) :Manager(name, title) {};
void handleRequest(PersonA* pPerson);
};
class DepartmentManager :public Manager
{
public:
DepartmentManager(string name, string title) :Manager(name, title) {};
void handleRequest(PersonA* pPerson);
};
class CEO :public Manager
{
public:
CEO(string name, string title) :Manager(name, title) {};
void handleRequest(PersonA* pPerson);
};
class Administrator :public Manager
{
public:
Administrator(string name, string title) :Manager(name, title) {};
void handleRequest(PersonA* pPerson);
};
+102
View File
@@ -0,0 +1,102 @@
#pragma once
#include "common.hpp"
class ComputerComponent
{
public:
ComputerComponent(string name) :m_name(name) {}
virtual void showInfo(string indent = "") = 0;
virtual bool isComposite();
virtual void startup(string indent = "");
virtual void shutdown(string indent = "");
protected:
string m_name;
};
class CPU :public ComputerComponent
{
public:
CPU(string name) :ComputerComponent(name) {}
void showInfo(string indent);
};
class MemoryCard :public ComputerComponent
{
public:
MemoryCard(string name) :ComputerComponent(name) {}
void showInfo(string indent);
};
class HardDisk :public ComputerComponent
{
public:
HardDisk(string name) :ComputerComponent(name) {}
void showInfo(string indent);
};
class GraphicsCard :public ComputerComponent
{
public:
GraphicsCard(string name) :ComputerComponent(name) {}
void showInfo(string indent);
};
class Battery :public ComputerComponent
{
public:
Battery(string name) :ComputerComponent(name) {}
void showInfo(string indent);
};
class Fan :public ComputerComponent
{
public:
Fan(string name) :ComputerComponent(name) {}
void showInfo(string indent);
};
class Displayer :public ComputerComponent
{
public:
Displayer(string name) :ComputerComponent(name) {}
void showInfo(string indent);
};
class ComputerCompsite :public ComputerComponent
{
public:
ComputerCompsite(string name) :ComputerComponent(name) {};
void showInfo(string indent);
bool isComposite();
void addComponent(ComputerComponent* component);
void removeComponent(ComputerComponent* component);
void startup(string indent);
void shutdown(string indent);
private:
vector<ComputerComponent*> m_components; // ×é³É³É·Ö
};
class Mainboard :public ComputerCompsite
{
public:
Mainboard(string name) :ComputerCompsite(name) {};
void showInfo(string indent);
};
class ComputerCase :public ComputerCompsite
{
public:
ComputerCase(string name) :ComputerCompsite(name) {};
void showInfo(string indent);
};
class Computer :public ComputerCompsite
{
public:
Computer(string name) :ComputerCompsite(name) {};
void showInfo(string indent);
};
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <memory>
#include <queue>
#include <string>
using namespace std;
+96
View File
@@ -0,0 +1,96 @@
#pragma once
#include "common.hpp"
class Person
{
public:
virtual void wear();
protected:
string m_Name;
};
class Engineer :public Person
{
public:
Engineer(string name, string skill) :m_Name(name), m_Skill(skill) {};
string getSkill();
virtual void wear();
private:
string m_Skill;
string m_Name;
};
class Teacher :public Person
{
public:
Teacher(string name, string title) :m_Name(name), m_Title(title) {};
string getTitle();
virtual void wear();
private:
string m_Name;
string m_Title;
};
class ClothingDecorator :public Person
{
public:
~ClothingDecorator();
virtual void wear();
virtual void decorate() = 0;
protected:
Person* m_pPerson; // ¸¸ÀàÖ¸Õë
};
class CasualPantDecorator :public ClothingDecorator
{
public:
CasualPantDecorator(Person* pPerson) { this->m_pPerson = pPerson; };
virtual void decorate();
};
class BeltDecorator :public ClothingDecorator
{
public:
BeltDecorator(Person* pPerson) { this->m_pPerson = pPerson; };
virtual void decorate();
};
class LeatherShoesDecorator :public ClothingDecorator
{
public:
LeatherShoesDecorator(Person* pPerson) { this->m_pPerson = pPerson; };
virtual void decorate();
};
class KnittedSweaterDecorator :public ClothingDecorator
{
public:
KnittedSweaterDecorator(Person* pPerson) { this->m_pPerson = pPerson; };
virtual void decorate();
};
class WhiteShirtDecorator :public ClothingDecorator
{
public:
WhiteShirtDecorator(Person* pPerson) { this->m_pPerson = pPerson; };
virtual void decorate();
};
class GlassesDecorator :public ClothingDecorator
{
public:
GlassesDecorator(Person* pPerson) { this->m_pPerson = pPerson; };
virtual void decorate();
};
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include "observe.hpp" // 观察者/监视模式
#include "state.hpp" // 状态模式
#include "decorate.hpp" // 装饰器模式
#include "singleton.hpp" // 单例模式
#include "prototype.hpp" // 原型模式(深拷贝)
#include "chain_of_responsibility.hpp" // 责任链模式
#include "proxy.hpp" // 代理模式
#include "facade.hpp" // 外观模式
#include "iterator.hpp" // 迭代器模式
#include "combination.hpp" // 组合模式
#include "strategy.hpp" // 策略模式
#include "simple_factory.hpp" // 简单工厂模式
#include "abstract_factory.hpp" // 抽象工厂模式
void RunObserveMode(void);
void RunStateMode(void);
void RunDecorateMode(void);
void RunSingletonMode(void);
void RunPrototypeMode(void);
void RunChainOfResponsibilityMode(void);
void RunProxyMode(void);
void RunFacadeMode(void);
void RunIteratorMode(void);
void RunCombination(void);
void RunStrategyMode(void);
void RunSimpleFactoryMode(void);
void RunAbstractFactoryMode(void);
+37
View File
@@ -0,0 +1,37 @@
#pragma once
#include "common.hpp"
class ZIPModel
{
public:
void compress(string srcFilePath, string dstFilePath);
void decompress(string srcFilePath, string dstFilePath);
};
class RARModel
{
public:
void compress(string srcFilePath, string dstFilePath);
void decompress(string srcFilePath, string dstFilePath);
};
class ZModel
{
public:
void compress(string srcFilePath, string dstFilePath);
void decompress(string srcFilePath, string dstFilePath);
};
class CompressionFacade
{
public:
CompressionFacade(ZIPModel* pZipModel = nullptr, RARModel* pRarModel = nullptr, ZModel* pZModel = nullptr);
~CompressionFacade();
void compress(string srcFilePath, string dstFilePath, string type);
void decompress(string srcFilePath, string dstFilePath);
private:
ZIPModel* m_pZipModel;
RARModel* m_pRarModel;
ZModel* m_pZModel;
};
+152
View File
@@ -0,0 +1,152 @@
#pragma once
#include "common.hpp"
class NumeralSystem;
class Customer
{
public:
Customer(string name, string clinics = "None") :m_name(name), m_clinics(clinics)
{
m_num = 0;
}
string getName();
void Regist(NumeralSystem* system);
void setNum(int num);
int getNum();
void setClinic(string clinic);
string getClinic();
private:
string m_name;
int m_num;
string m_clinics;
};
class NumeralIterator
{
public:
NumeralIterator(vector<Customer*> data, int curIdx = -1) :m_data(data), m_curIdx(curIdx) {};
bool next();
Customer* current();
private:
vector<Customer*> m_data;
int m_curIdx;
};
class NumeralSystem
{
public:
static vector<string> m_clinics;
NumeralSystem(string name) : m_name(name)
{
m_curNum = 0;
}
void pushCustomer(Customer* pCustomer);
NumeralIterator getIterator();
private:
vector<Customer*> m_pCustomers;
int m_curNum;
string m_name;
};
// ======================== 迭代器模式(来源:网络) ====================
class Iterator
{
public:
Iterator() {};
virtual ~Iterator() {};
virtual string toBegin() = 0;
virtual string toNext() = 0;
virtual string getCurrent() = 0;
virtual bool isEnd() = 0;
};
class Aggregate
{
public:
virtual int Count() = 0;
virtual void Push(const string& strValue) = 0;
virtual string Pop(const int nIndex) = 0;
virtual Iterator* CreateIterator() = 0;
};
class ConcreteIterator : public Iterator
{
public:
ConcreteIterator(Aggregate* pAggregate) :m_nCurrent(0), Iterator()
{
m_Aggregate = pAggregate;
}
string toBegin()
{
return m_Aggregate->Pop(0);
}
string toNext()
{
string strRet;
m_nCurrent++;
if (m_nCurrent < m_Aggregate->Count())
{
strRet = m_Aggregate->Pop(m_nCurrent);
}
return strRet;
}
string getCurrent()
{
return m_Aggregate->Pop(m_nCurrent);
}
bool isEnd()
{
return ((m_nCurrent >= m_Aggregate->Count()) ? true : false);
}
private:
Aggregate* m_Aggregate;
int m_nCurrent;
};
class ConcreteAggregate : public Aggregate
{
public:
ConcreteAggregate() :m_pIterator(NULL)
{
m_vecItems.clear();
}
~ConcreteAggregate()
{
if (NULL != m_pIterator)
{
delete m_pIterator;
m_pIterator = NULL;
}
}
Iterator* CreateIterator()
{
if (NULL == m_pIterator)
{
m_pIterator = new ConcreteIterator(this);
}
return m_pIterator;
}
void Push(const string& strValue)
{
m_vecItems.push_back(strValue);
}
string Pop(const int nIndex)
{
string strRet;
if (nIndex < m_vecItems.size())
{
strRet = m_vecItems[nIndex];
}
return strRet;
}
private:
vector<string> m_vecItems;
Iterator* m_pIterator;
};
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "common.hpp"
class WaterHeater;
class Observer // 观察者的抽象基类
{
public:
virtual void Update(WaterHeater* waterHeater) const = 0;
};
class WashingMode :public Observer // 观察者类
{
public:
virtual void Update(WaterHeater* waterHeater) const;
};
class DrinkingMode :public Observer // 观察者类
{
public:
virtual void Update(WaterHeater* waterHeater) const;
};
class WaterHeater // 被观察的类
{
public:
WaterHeater(int temperature = 25) :m_temperature(temperature) {};
int getTempetature();
void setTemperature(int temperature);
virtual void HandleObserver(); // 通知观察者
bool RegisterObserver(Observer* inObserver); // 添加监听(观察)者
bool UnRegisterObserver(Observer* inObserver); // 删除监听(观察)者
protected:
int m_temperature;
vector<Observer*> m_Observers;
};
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include "common.hpp"
class Prototype // C++ 本身的拷贝函数为浅拷贝,原型模式为深拷贝
{
public:
Prototype() {}
~Prototype() {}
virtual Prototype* Clone() = 0;
};
class ConcretePrototype :public Prototype
{
public:
ConcretePrototype() {}
~ConcretePrototype() {}
virtual Prototype* Clone();
private:
ConcretePrototype(const ConcretePrototype&); //
};
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include "common.hpp"
class Subject // 主题的抽象基类
{
public:
Subject(string name) :m_Name(name) {};
string getName();
virtual void request() = 0;
private:
string m_Name;
};
class RealSubject :public Subject // 主体主题类
{
public:
RealSubject(string name) :Subject(name) {};
void request();
};
class ProxySubject :public Subject // 代理主题类
{
public:
ProxySubject(string name, RealSubject* subject = nullptr) :Subject(name), m_pSubject(subject) {};
void request();
void preRequest();
void afterRequest();
private:
RealSubject* m_pSubject;
};
+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include "common.hpp"
typedef enum // ť­ąĘŔŕĐÍ
{
PenTypeLine = 1,
PenTypeRect = 2,
PenTypeEllipse = 3
}PenType;
class Pen
{
public:
Pen(string name);
virtual PenType getType() = 0;
string getName();
private:
string m_name;
};
class LinePen :public Pen
{
public:
LinePen(string name) :Pen(name) {}
PenType getType();
};
class RectanglePen :public Pen
{
public:
RectanglePen(string name) :Pen(name) {}
PenType getType();
};
class EllipsePen :public Pen
{
public:
EllipsePen(string name) :Pen(name) {}
PenType getType();
};
class PenFactory
{
public:
PenFactory() {};
Pen* CreatePen(PenType pentype);
private:
map<PenType, Pen*> m_PenProduct;
};
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include "common.hpp"
class SingletonA // 懒汉式,线程不安全,空间换时间
{
private:
SingletonA() {} // 构造函数私有化保证对象不会在类外被创建
SingletonA(SingletonA&) = delete;
SingletonA& operator=(const SingletonA&) = delete;
static SingletonA* m_pInstance;
public:
static SingletonA* getInstance();
};
class SingletonB // 饿汉式,线程安全,时间换空间
{
private:
SingletonB() {}
SingletonB(SingletonB&) = delete;
SingletonB& operator=(const SingletonB&) = delete;
public:
static SingletonB* getInstance();
};
+74
View File
@@ -0,0 +1,74 @@
#pragma once
#include "common.hpp"
class State;
class Context // 上下文环境类,负责具体状态的切换
{
public:
Context();
~Context();
void addState(State* state);
bool changeState(State* state);
State* getState();
void setStateInfo(int stateInfo);
int getStateInfo();
private:
State* p_curState;
vector<State*> p_states;
int m_stateInfo;
};
class Water;
class State
{
public:
State(string name) :m_name(name) {};
virtual void behavior(Water* p_water) const = 0;
virtual bool isMatch(int stateInfo) const = 0;
string getName();
private:
string m_name;
};
class SolidState :public State
{
public:
SolidState(string name) :State(name) {};
void behavior(Water* p_water) const;
bool isMatch(int stateInfo) const;
};
class Liquidtate :public State
{
public:
Liquidtate(string name) :State(name) {};
void behavior(Water* p_water) const;
bool isMatch(int stateInfo) const;
};
class GaseousState :public State
{
public:
GaseousState(string name) :State(name) {};
void behavior(Water* p_water) const;
bool isMatch(int stateInfo) const;
};
class Water :public Context
{
public:
Water();
int getTemperature();
void setTemperature(int temperature);
void riseTemperature(int step);
void reduceTemperature(int step);
void behavior();
};
+49
View File
@@ -0,0 +1,49 @@
#pragma once
#include "common.hpp"
class StrategyPerson
{
public:
StrategyPerson(string name, int age, double weight, double height);
void showMyself();
string m_name;
int m_age;
double m_weight;
double m_height;
};
// ²ßÂԵijéÏó
class ICompare
{
public:
virtual bool comparable(StrategyPerson* person1, StrategyPerson* person2) = 0;
};
// ¾ßÌåµÄ²ßÂÔ
class CompareByAge :public ICompare
{
public:
bool comparable(StrategyPerson* person1, StrategyPerson* person2);
};
class CompareByHeight :public ICompare
{
public:
bool comparable(StrategyPerson* person1, StrategyPerson* person2);
};
class CompareByWeight :public ICompare
{
public:
bool comparable(StrategyPerson* person1, StrategyPerson* person2);
};
class SortPerson
{
public:
SortPerson(ICompare* pICompare);
void sort(vector<StrategyPerson>& personList);
private:
ICompare* m_CompareStrategy;
};