设计模式例程
This commit is contained in:
79
inc/abstract_factory.hpp
Normal file
79
inc/abstract_factory.hpp
Normal file
@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class Shape // <20><>״<EFBFBD>ӿ<EFBFBD>
|
||||
{
|
||||
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 // <20><>ɫ<EFBFBD>ӿ<EFBFBD>
|
||||
{
|
||||
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
inc/chain_of_responsibility.hpp
Normal file
74
inc/chain_of_responsibility.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class Manager;
|
||||
|
||||
class PersonA // Requester <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
|
||||
{
|
||||
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 <20><><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
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 // <20><><EFBFBD><EFBFBD>
|
||||
{
|
||||
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
inc/combination.hpp
Normal file
102
inc/combination.hpp
Normal 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; // <20><><EFBFBD>ɳɷ<C9B3>
|
||||
};
|
||||
|
||||
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
inc/common.hpp
Normal file
18
inc/common.hpp
Normal 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
inc/decorate.hpp
Normal file
96
inc/decorate.hpp
Normal 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; // <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
||||
|
||||
};
|
||||
|
||||
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
inc/design_pattern.hpp
Normal file
29
inc/design_pattern.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "observe.hpp" // <20>۲<EFBFBD><DBB2><EFBFBD>/<2F><><EFBFBD><EFBFBD>ģʽ
|
||||
#include "state.hpp" // ״̬ģʽ
|
||||
#include "decorate.hpp" // װ<><D7B0><EFBFBD><EFBFBD>ģʽ
|
||||
#include "singleton.hpp" // <20><><EFBFBD><EFBFBD>ģʽ
|
||||
#include "prototype.hpp" // ԭ<><D4AD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EEBFBD><EFBFBD><EFBFBD>
|
||||
#include "chain_of_responsibility.hpp" // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
||||
#include "proxy.hpp" // <20><><EFBFBD><EFBFBD>ģʽ
|
||||
#include "facade.hpp" // <20><><EFBFBD><EFBFBD>ģʽ
|
||||
#include "iterator.hpp" // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
||||
#include "combination.hpp" // <20><><EFBFBD><EFBFBD>ģʽ
|
||||
#include "strategy.hpp" // <20><><EFBFBD><EFBFBD>ģʽ
|
||||
#include "simple_factory.hpp" // <20><EFBFBD><F2B5A5B9><EFBFBD>ģʽ
|
||||
#include "abstract_factory.hpp" // <20><><EFBFBD><EFBFBD>ģʽ
|
||||
|
||||
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
inc/facade.hpp
Normal file
37
inc/facade.hpp
Normal 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
inc/iterator.hpp
Normal file
152
inc/iterator.hpp
Normal 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;
|
||||
};
|
||||
|
||||
// ======================== <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>磩 ====================
|
||||
|
||||
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
inc/observe.hpp
Normal file
39
inc/observe.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class WaterHeater;
|
||||
|
||||
class Observer // <20>۲<EFBFBD><DBB2>ߵij<DFB5><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
public:
|
||||
virtual void Update(WaterHeater* waterHeater) const = 0;
|
||||
};
|
||||
|
||||
class WashingMode :public Observer // <20>۲<EFBFBD><DBB2><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
public:
|
||||
virtual void Update(WaterHeater* waterHeater) const;
|
||||
};
|
||||
|
||||
class DrinkingMode :public Observer // <20>۲<EFBFBD><DBB2><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
public:
|
||||
virtual void Update(WaterHeater* waterHeater) const;
|
||||
};
|
||||
|
||||
class WaterHeater // <20><><EFBFBD>۲<EFBFBD><DBB2><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
public:
|
||||
WaterHeater(int temperature = 25) :m_temperature(temperature) {};
|
||||
|
||||
int getTempetature();
|
||||
void setTemperature(int temperature);
|
||||
|
||||
virtual void HandleObserver(); // ֪ͨ<CDA8>۲<EFBFBD><DBB2><EFBFBD>
|
||||
bool RegisterObserver(Observer* inObserver); // <20><><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۲죩<DBB2><ECA3A9>
|
||||
bool UnRegisterObserver(Observer* inObserver); // ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۲죩<DBB2><ECA3A9>
|
||||
|
||||
protected:
|
||||
int m_temperature;
|
||||
vector<Observer*> m_Observers;
|
||||
};
|
22
inc/prototype.hpp
Normal file
22
inc/prototype.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class Prototype // C++ <20><><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊdz<CEAA><C7B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>ģʽΪ<CABD><EFBFBD><EEBFBD>
|
||||
{
|
||||
public:
|
||||
Prototype() {}
|
||||
~Prototype() {}
|
||||
|
||||
virtual Prototype* Clone() = 0;
|
||||
};
|
||||
|
||||
class ConcretePrototype :public Prototype
|
||||
{
|
||||
public:
|
||||
ConcretePrototype() {}
|
||||
~ConcretePrototype() {}
|
||||
|
||||
virtual Prototype* Clone();
|
||||
private:
|
||||
ConcretePrototype(const ConcretePrototype&); //
|
||||
};
|
33
inc/proxy.hpp
Normal file
33
inc/proxy.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class Subject // <20><><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
public:
|
||||
Subject(string name) :m_Name(name) {};
|
||||
string getName();
|
||||
virtual void request() = 0;
|
||||
|
||||
private:
|
||||
string m_Name;
|
||||
};
|
||||
|
||||
class RealSubject :public Subject // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
public:
|
||||
RealSubject(string name) :Subject(name) {};
|
||||
void request();
|
||||
|
||||
};
|
||||
|
||||
class ProxySubject :public Subject // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
public:
|
||||
ProxySubject(string name, RealSubject* subject = nullptr) :Subject(name), m_pSubject(subject) {};
|
||||
void request();
|
||||
void preRequest();
|
||||
void afterRequest();
|
||||
|
||||
private:
|
||||
RealSubject* m_pSubject;
|
||||
};
|
50
inc/simple_factory.hpp
Normal file
50
inc/simple_factory.hpp
Normal file
@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
typedef enum // <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
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
inc/singleton.hpp
Normal file
29
inc/singleton.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class SingletonA // <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>̲߳<DFB3><CCB2><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD>ռ任ʱ<E4BBBB><CAB1>
|
||||
{
|
||||
private:
|
||||
SingletonA() {} // <20><><EFBFBD>캯<EFBFBD><ECBAAF>˽<EFBFBD>л<EFBFBD><D0BB><EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><F3B2BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⱻ<EFBFBD><E2B1BB><EFBFBD><EFBFBD>
|
||||
|
||||
SingletonA(SingletonA&) = delete;
|
||||
SingletonA& operator=(const SingletonA&) = delete;
|
||||
|
||||
static SingletonA* m_pInstance;
|
||||
public:
|
||||
static SingletonA* getInstance();
|
||||
|
||||
};
|
||||
|
||||
class SingletonB // <20><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB>ʱ<EFBFBD>任<EFBFBD>ռ<EFBFBD>
|
||||
{
|
||||
private:
|
||||
SingletonB() {}
|
||||
SingletonB(SingletonB&) = delete;
|
||||
SingletonB& operator=(const SingletonB&) = delete;
|
||||
public:
|
||||
static SingletonB* getInstance();
|
||||
|
||||
};
|
||||
|
||||
|
74
inc/state.hpp
Normal file
74
inc/state.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class State;
|
||||
class Context // <20><><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>࣬<EFBFBD><E0A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD>л<EFBFBD>
|
||||
{
|
||||
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
inc/strategy.hpp
Normal file
49
inc/strategy.hpp
Normal 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;
|
||||
};
|
||||
|
||||
// <20><><EFBFBD>Եij<D4B5><C4B3><EFBFBD>
|
||||
class ICompare
|
||||
{
|
||||
public:
|
||||
virtual bool comparable(StrategyPerson* person1, StrategyPerson* person2) = 0;
|
||||
};
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
|
||||
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;
|
||||
};
|
Reference in New Issue
Block a user