设计模式例程
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
build
|
20
CMakeLists.txt
Normal file
20
CMakeLists.txt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.16.0)
|
||||||
|
|
||||||
|
project(design-pattern)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
|
||||||
|
set(SOURCE_DIR "${PROJECT_SOURCE_DIR}/src")
|
||||||
|
set(INCLUDE_DIR "${PROJECT_SOURCE_DIR}/inc")
|
||||||
|
|
||||||
|
file(GLOB SOURCES "${SOURCE_DIR}/*.cpp")
|
||||||
|
file(GLOB HEADERS "${INCLUDE_DIR}/*.hpp")
|
||||||
|
|
||||||
|
aux_source_directory(${SOURCE_DIR} src)
|
||||||
|
aux_source_directory(${INCLUDE_DIR} inc)
|
||||||
|
|
||||||
|
add_executable(design-pattern ${SOURCES} ${HEADERS} "${PROJECT_SOURCE_DIR}/main.cpp")
|
||||||
|
|
||||||
|
target_include_directories(design-pattern PRIVATE ${SOURCE_DIR} ${INCLUDE_DIR})
|
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;
|
||||||
|
};
|
22
main.cpp
Normal file
22
main.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "common.hpp"
|
||||||
|
#include "design_pattern.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
RunObserveMode();
|
||||||
|
RunStateMode();
|
||||||
|
RunDecorateMode();
|
||||||
|
RunSingletonMode();
|
||||||
|
RunPrototypeMode();
|
||||||
|
RunChainOfResponsibilityMode();
|
||||||
|
RunProxyMode();
|
||||||
|
RunFacadeMode();
|
||||||
|
RunIteratorMode();
|
||||||
|
RunCombination();
|
||||||
|
RunStrategyMode();
|
||||||
|
RunSimpleFactoryMode();
|
||||||
|
RunAbstractFactoryMode();
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
88
src/abstract_factory.cpp
Normal file
88
src/abstract_factory.cpp
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
#include "abstract_factory.hpp"
|
||||||
|
|
||||||
|
void Rectangle::draw()
|
||||||
|
{
|
||||||
|
cout << "Inside Rectangle::draw() method." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Square::draw()
|
||||||
|
{
|
||||||
|
cout << "Inside Square::draw() method." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Circle::draw()
|
||||||
|
{
|
||||||
|
cout << "Inside Circle::draw() method." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Red::fill()
|
||||||
|
{
|
||||||
|
cout << "Inside Red::fill() method." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Green::fill()
|
||||||
|
{
|
||||||
|
cout << "Inside Green::fill() method." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Yellow::fill()
|
||||||
|
{
|
||||||
|
cout << "Inside Yellow::fill() method." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Shape* ShapeFactory::getShape(string shape)
|
||||||
|
{
|
||||||
|
if (shape == "circle")
|
||||||
|
{
|
||||||
|
return new Circle();
|
||||||
|
}
|
||||||
|
else if (shape == "rectangle")
|
||||||
|
{
|
||||||
|
return new Rectangle();
|
||||||
|
}
|
||||||
|
else if (shape == "square")
|
||||||
|
{
|
||||||
|
return new Square();
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Color* ShapeFactory::getColor(string color)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Shape* ColorFactory::getShape(string shape)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Color* ColorFactory::getColor(string color)
|
||||||
|
{
|
||||||
|
if (color == "red")
|
||||||
|
{
|
||||||
|
return new Red();
|
||||||
|
}
|
||||||
|
else if (color == "green")
|
||||||
|
{
|
||||||
|
return new Green();
|
||||||
|
}
|
||||||
|
else if (color == "yellow")
|
||||||
|
{
|
||||||
|
return new Yellow();
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractFactory* FactoryProducer::getFactory(string choice)
|
||||||
|
{
|
||||||
|
if (choice == "shape")
|
||||||
|
{
|
||||||
|
return new ShapeFactory();
|
||||||
|
}
|
||||||
|
else if (choice == "color")
|
||||||
|
{
|
||||||
|
return new ColorFactory();
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
96
src/chain_of_responsibility.cpp
Normal file
96
src/chain_of_responsibility.cpp
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include "chain_of_responsibility.hpp"
|
||||||
|
|
||||||
|
int PersonA::getDayoff()
|
||||||
|
{
|
||||||
|
return m_Dayoff;
|
||||||
|
}
|
||||||
|
|
||||||
|
string PersonA::getName()
|
||||||
|
{
|
||||||
|
return m_Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
string PersonA::getReason()
|
||||||
|
{
|
||||||
|
return m_Reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PersonA::setLeader(Manager* pLeader)
|
||||||
|
{
|
||||||
|
m_pLeader = pLeader;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PersonA::Request()
|
||||||
|
{
|
||||||
|
cout << m_Name << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << m_Dayoff << "<EFBFBD>죬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD>" << m_Reason << endl;
|
||||||
|
if (m_pLeader != nullptr)
|
||||||
|
{
|
||||||
|
m_pLeader->handleRequest(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string Manager::getName()
|
||||||
|
{
|
||||||
|
return m_Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Manager::getTitle()
|
||||||
|
{
|
||||||
|
return m_Title;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Manager::setNextHander(Manager* p_NextHandler)
|
||||||
|
{
|
||||||
|
m_pNextHandle = p_NextHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Supervisor::handleRequest(PersonA* pPerson)
|
||||||
|
{
|
||||||
|
if (pPerson->getDayoff() <= 2)
|
||||||
|
{
|
||||||
|
cout << "ͬ<EFBFBD><EFBFBD>" << pPerson->getName() << "<EFBFBD><EFBFBD><EFBFBD>٣<EFBFBD>ǩ<EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>" << getName() << "(" << getTitle() << ")" << endl;
|
||||||
|
}
|
||||||
|
if (m_pNextHandle != nullptr)
|
||||||
|
{
|
||||||
|
m_pNextHandle->handleRequest(pPerson);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DepartmentManager::handleRequest(PersonA* pPerson)
|
||||||
|
{
|
||||||
|
if (pPerson->getDayoff() <= 5 && pPerson->getDayoff() > 2)
|
||||||
|
{
|
||||||
|
cout << "ͬ<EFBFBD><EFBFBD>" << pPerson->getName() << "<EFBFBD><EFBFBD><EFBFBD>٣<EFBFBD>ǩ<EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>" << getName() << "(" << getTitle() << ")" << endl;
|
||||||
|
}
|
||||||
|
if (m_pNextHandle != nullptr)
|
||||||
|
{
|
||||||
|
m_pNextHandle->handleRequest(pPerson);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CEO::handleRequest(PersonA* pPerson)
|
||||||
|
{
|
||||||
|
if (pPerson->getDayoff() > 5)
|
||||||
|
{
|
||||||
|
cout << "ͬ<EFBFBD><EFBFBD>" << pPerson->getName() << "<EFBFBD><EFBFBD><EFBFBD>٣<EFBFBD>ǩ<EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>" << getName() << "(" << getTitle() << ")" << endl;
|
||||||
|
}
|
||||||
|
if (m_pNextHandle != nullptr)
|
||||||
|
{
|
||||||
|
m_pNextHandle->handleRequest(pPerson);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Administrator::handleRequest(PersonA* pPerson)
|
||||||
|
{
|
||||||
|
|
||||||
|
cout << pPerson->getName() << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>"
|
||||||
|
<< getName() << "(" << getTitle() << ")" << endl;
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
if (m_pNextHandle != nullptr)
|
||||||
|
{
|
||||||
|
m_pNextHandle->handleRequest(pPerson);
|
||||||
|
}
|
||||||
|
}
|
122
src/combination.cpp
Normal file
122
src/combination.cpp
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
#include "combination.hpp"
|
||||||
|
|
||||||
|
bool ComputerComponent::isComposite()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputerComponent::startup(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << m_name << " " << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputerComponent::shutdown(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << m_name << " " << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPU::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "CPU<EFBFBD><EFBFBD>" << indent << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>и<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㡣" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MemoryCard::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "<EFBFBD>ڴ棺" << indent << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>ٶȿ졣" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HardDisk::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "Ӳ<EFBFBD>̣<EFBFBD>" << indent << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô洢<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD>洢<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphicsCard::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "<EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD>" << indent << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><EFBFBD>ټ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Battery::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>" << indent << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Գ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>硣" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Fan::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "<EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD>" << indent << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CPUɢ<EFBFBD>ȡ<EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Displayer::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << indent << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputerCompsite::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << m_name << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>²<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD>" << endl;
|
||||||
|
vector<ComputerComponent*>::iterator iter;
|
||||||
|
for (iter = m_components.begin(); iter != m_components.end(); iter++)
|
||||||
|
{
|
||||||
|
(*iter)->showInfo(indent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ComputerCompsite::isComposite()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputerCompsite::addComponent(ComputerComponent* component)
|
||||||
|
{
|
||||||
|
m_components.push_back(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputerCompsite::removeComponent(ComputerComponent* component)
|
||||||
|
{
|
||||||
|
vector<ComputerComponent*>::iterator iter = find(m_components.begin(), m_components.end(), component);
|
||||||
|
if (iter != m_components.end())
|
||||||
|
{
|
||||||
|
m_components.erase(iter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputerCompsite::startup(string indent)
|
||||||
|
{
|
||||||
|
ComputerComponent::startup(indent);
|
||||||
|
indent += "\t";
|
||||||
|
|
||||||
|
vector<ComputerComponent*>::iterator iter;
|
||||||
|
for (iter = m_components.begin(); iter != m_components.end(); iter++)
|
||||||
|
{
|
||||||
|
(*iter)->startup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputerCompsite::shutdown(string indent)
|
||||||
|
{
|
||||||
|
ComputerComponent::shutdown(indent);
|
||||||
|
indent += "\t";
|
||||||
|
|
||||||
|
vector<ComputerComponent*>::iterator iter;
|
||||||
|
for (iter = m_components.begin(); iter != m_components.end(); iter++)
|
||||||
|
{
|
||||||
|
(*iter)->shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mainboard::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "<EFBFBD><EFBFBD><EFBFBD>壺";
|
||||||
|
ComputerCompsite::showInfo(indent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ComputerCase::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "<EFBFBD><EFBFBD><EFBFBD>䣺";
|
||||||
|
ComputerCompsite::showInfo(indent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Computer::showInfo(string indent)
|
||||||
|
{
|
||||||
|
cout << indent << "<EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>";
|
||||||
|
ComputerCompsite::showInfo(indent);
|
||||||
|
}
|
74
src/decorate.cpp
Normal file
74
src/decorate.cpp
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include "decorate.hpp"
|
||||||
|
|
||||||
|
void Person::wear()
|
||||||
|
{
|
||||||
|
cout << "<EFBFBD><EFBFBD>װ" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Engineer::getSkill()
|
||||||
|
{
|
||||||
|
return m_Skill;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Teacher::getTitle()
|
||||||
|
{
|
||||||
|
return m_Title;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Teacher::wear()
|
||||||
|
{
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << m_Name << m_Title << endl;
|
||||||
|
Person::wear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engineer::wear()
|
||||||
|
{
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << m_Skill << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʦ " << m_Name << " " << endl;
|
||||||
|
Person::wear();
|
||||||
|
}
|
||||||
|
|
||||||
|
ClothingDecorator::~ClothingDecorator()
|
||||||
|
{
|
||||||
|
if (m_pPerson != nullptr)
|
||||||
|
{
|
||||||
|
delete m_pPerson;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClothingDecorator::wear()
|
||||||
|
{
|
||||||
|
m_pPerson->wear();
|
||||||
|
decorate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CasualPantDecorator::decorate()
|
||||||
|
{
|
||||||
|
cout << "һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD>п<EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BeltDecorator::decorate()
|
||||||
|
{
|
||||||
|
cout << "һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ú<EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LeatherShoesDecorator::decorate()
|
||||||
|
{
|
||||||
|
cout << "һ˫<EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƤЬ" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void KnittedSweaterDecorator::decorate()
|
||||||
|
{
|
||||||
|
cout << "һ<EFBFBD><EFBFBD><EFBFBD>Ϻ<EFBFBD>ɫ<EFBFBD><EFBFBD>֯ë<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WhiteShirtDecorator::decorate()
|
||||||
|
{
|
||||||
|
cout << "һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlassesDecorator::decorate()
|
||||||
|
{
|
||||||
|
cout << "һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κڿ<EFBFBD><EFBFBD>۾<EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
314
src/design_pattern.cpp
Normal file
314
src/design_pattern.cpp
Normal file
@ -0,0 +1,314 @@
|
|||||||
|
#include "design_pattern.hpp"
|
||||||
|
|
||||||
|
void RunObserveMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== Observer Mode =================" << endl;
|
||||||
|
|
||||||
|
WaterHeater heater = WaterHeater();
|
||||||
|
|
||||||
|
WashingMode washingObser = WashingMode();
|
||||||
|
DrinkingMode drinkingObser = DrinkingMode();
|
||||||
|
|
||||||
|
heater.RegisterObserver(&washingObser);
|
||||||
|
heater.RegisterObserver(&drinkingObser);
|
||||||
|
|
||||||
|
heater.setTemperature(40);
|
||||||
|
heater.setTemperature(60);
|
||||||
|
heater.setTemperature(100);
|
||||||
|
|
||||||
|
heater.UnRegisterObserver(&washingObser);
|
||||||
|
heater.setTemperature(60);
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void RunStateMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== State Mode =================" << endl;
|
||||||
|
|
||||||
|
Water water = Water();
|
||||||
|
water.behavior();
|
||||||
|
|
||||||
|
water.setTemperature(-4);
|
||||||
|
water.behavior();
|
||||||
|
water.riseTemperature(18);
|
||||||
|
water.behavior();
|
||||||
|
water.riseTemperature(90);
|
||||||
|
water.behavior();
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunDecorateMode()
|
||||||
|
{
|
||||||
|
cout << "=============== Decorate Mode =================" << endl;
|
||||||
|
|
||||||
|
Person* tony = new Engineer("Tony", "<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
|
||||||
|
ClothingDecorator* clothes = new CasualPantDecorator(tony);
|
||||||
|
|
||||||
|
clothes = new BeltDecorator(clothes);
|
||||||
|
clothes = new LeatherShoesDecorator(clothes);
|
||||||
|
clothes = new WhiteShirtDecorator(clothes);
|
||||||
|
clothes = new KnittedSweaterDecorator(clothes);
|
||||||
|
clothes = new GlassesDecorator(clothes); // <20><><EFBFBD><EFBFBD> ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD>д洢<D0B4><E6B4A2><EFBFBD>ϸ<EFBFBD><CFB8><EFBFBD>ָ<EFBFBD><D6B8> ʵ<><CAB5><EFBFBD>˵ݹ<CBB5><DDB9><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
|
||||||
|
clothes->wear();
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
ClothingDecorator* decorateTeacher = new GlassesDecorator(new WhiteShirtDecorator(
|
||||||
|
new LeatherShoesDecorator(new Teacher("Jack", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"))));
|
||||||
|
|
||||||
|
decorateTeacher->wear();
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void RunSingletonMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== Singleton Mode =================" << endl;
|
||||||
|
|
||||||
|
SingletonA* ptr1 = SingletonA::getInstance();
|
||||||
|
SingletonA* ptr2 = SingletonA::getInstance();
|
||||||
|
|
||||||
|
cout << ptr1 << " " << ptr2 << endl;
|
||||||
|
|
||||||
|
SingletonB* ptr3 = SingletonB::getInstance();
|
||||||
|
SingletonB* ptr4 = SingletonB::getInstance();
|
||||||
|
|
||||||
|
cout << ptr3 << " " << ptr4 << endl;
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunPrototypeMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== Prototype Mode =================" << endl;
|
||||||
|
Prototype* ptr = new ConcretePrototype();
|
||||||
|
|
||||||
|
Prototype* copy_ptr = ptr->Clone();
|
||||||
|
|
||||||
|
delete ptr, copy_ptr;
|
||||||
|
cout << endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunChainOfResponsibilityMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== Chain Of Responsibilty Mode =================" << endl;
|
||||||
|
Manager* directorLeader = new Supervisor("Eren", "<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
Manager* departmentManager = new DepartmentManager("Eric", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD>");
|
||||||
|
Manager* ceo = new CEO("Helen", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD>˾CEO");
|
||||||
|
Manager* administrator = new Administrator("Nina", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD>");
|
||||||
|
|
||||||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
directorLeader->setNextHander(departmentManager);
|
||||||
|
departmentManager->setNextHander(ceo);
|
||||||
|
ceo->setNextHander(administrator);
|
||||||
|
|
||||||
|
PersonA sunny("Sunny", 1, "<EFBFBD>μ<EFBFBD>MDCC<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
sunny.setLeader(directorLeader);
|
||||||
|
sunny.Request();
|
||||||
|
|
||||||
|
PersonA tony("Tony", 5, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
tony.setLeader(directorLeader);
|
||||||
|
tony.Request();
|
||||||
|
|
||||||
|
PersonA pony("Pony", 15, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
pony.setLeader(directorLeader);
|
||||||
|
pony.Request();
|
||||||
|
|
||||||
|
|
||||||
|
delete directorLeader, departmentManager, ceo, administrator;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunProxyMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== Proxy Mode =================" << endl;
|
||||||
|
|
||||||
|
RealSubject realObj("RealSubject");
|
||||||
|
ProxySubject proxyObj("ProxySubject", &realObj);
|
||||||
|
proxyObj.request();
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunFacadeMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== Facade Mode =================" << endl;
|
||||||
|
CompressionFacade facade;
|
||||||
|
|
||||||
|
facade.compress("E;\\<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ.md", "E;\\ѹ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ", "zip");
|
||||||
|
facade.decompress("E;\\ѹ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ.zip", "E;\\<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ.md");
|
||||||
|
|
||||||
|
facade.compress("E;\\<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\C++<2B><><EFBFBD><EFBFBD>-<2D><><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>.md", "E;\\ѹ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\C++<2B><><EFBFBD><EFBFBD>-<2D><><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>", "rar");
|
||||||
|
facade.decompress("E;\\ѹ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\C++<2B><><EFBFBD><EFBFBD>-<2D><><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>.rar", "E;\\<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\C++<2B><><EFBFBD><EFBFBD>-<2D><><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>.md");
|
||||||
|
|
||||||
|
facade.compress("E;\\<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\<EFBFBD>ɸ<EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>.md", "E;\\ѹ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\<EFBFBD>ɸ<EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "7z");
|
||||||
|
facade.decompress("E;\\ѹ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\<EFBFBD>ɸ<EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>.7z", "E;\\<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\\<EFBFBD>ɸ<EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>.md");
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunIteratorMode()
|
||||||
|
{
|
||||||
|
cout << "=============== Iterator Mode =================" << endl;
|
||||||
|
NumeralSystem numeralSystem("<EFBFBD>Һ<EFBFBD>̨");
|
||||||
|
|
||||||
|
Customer lily("Lily");
|
||||||
|
lily.Regist(&numeralSystem);
|
||||||
|
|
||||||
|
Customer pony("Pony");
|
||||||
|
pony.Regist(&numeralSystem);
|
||||||
|
|
||||||
|
Customer nick("Nick");
|
||||||
|
nick.Regist(&numeralSystem);
|
||||||
|
|
||||||
|
Customer tony("Tony");
|
||||||
|
tony.Regist(&numeralSystem);
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
NumeralIterator iter = numeralSystem.getIterator(); // <20><><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
while (iter.next())
|
||||||
|
{
|
||||||
|
Customer* pCustomer = iter.current();
|
||||||
|
cout << "<EFBFBD><EFBFBD>һλ<EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>" << pCustomer->getNum() << "<EFBFBD><EFBFBD>" << pCustomer->getName() <<
|
||||||
|
") <20>뵽" << pCustomer->getClinic() << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunCombination(void)
|
||||||
|
{
|
||||||
|
cout << "=============== Combination Mode =================" << endl;
|
||||||
|
|
||||||
|
Mainboard* pmainBoard = new Mainboard("GIGABYTE Z170M M-ATX");
|
||||||
|
pmainBoard->addComponent(new CPU("Inter Core i5-6600K"));
|
||||||
|
pmainBoard->addComponent(new MemoryCard("Kingston Fury DDR4"));
|
||||||
|
pmainBoard->addComponent(new HardDisk("Kingston V300"));
|
||||||
|
pmainBoard->addComponent(new GraphicsCard("Colorful iGame750"));
|
||||||
|
|
||||||
|
ComputerCase* pcomputerCase = new ComputerCase("SAMA MATX");
|
||||||
|
pcomputerCase->addComponent(pmainBoard);
|
||||||
|
pcomputerCase->addComponent(new Battery("Antec VP 450P"));
|
||||||
|
pcomputerCase->addComponent(new Fan("DEEPCOOL 120T"));
|
||||||
|
|
||||||
|
Computer* pcomputer = new Computer("Tony DIY <20><><EFBFBD><EFBFBD>");
|
||||||
|
pcomputer->addComponent(pcomputerCase);
|
||||||
|
pcomputer->addComponent(new Displayer("AOC LV243XIP"));
|
||||||
|
|
||||||
|
pcomputer->showInfo("");
|
||||||
|
cout << endl << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD>";
|
||||||
|
pcomputer->startup("");
|
||||||
|
cout << endl << "<EFBFBD>ػ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD>";
|
||||||
|
pcomputer->shutdown("");
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunStrategyMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== Strategy Mode =================" << endl;
|
||||||
|
vector<StrategyPerson> personList;
|
||||||
|
|
||||||
|
personList.push_back(StrategyPerson("Tony", 2, 54.5, 0.82));
|
||||||
|
personList.push_back(StrategyPerson("Jack", 31, 74.5, 1.80));
|
||||||
|
personList.push_back(StrategyPerson("Nick", 54, 44.5, 1.59));
|
||||||
|
personList.push_back(StrategyPerson("Eric", 23, 62.0, 1.78));
|
||||||
|
personList.push_back(StrategyPerson("Helen",16, 45.7, 1.60));
|
||||||
|
|
||||||
|
SortPerson ageSort(new CompareByAge);
|
||||||
|
ageSort.sort(personList);
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
for (size_t i = 0; i < personList.size(); i++)
|
||||||
|
{
|
||||||
|
personList[i].showMyself();
|
||||||
|
}
|
||||||
|
|
||||||
|
SortPerson weightSort(new CompareByWeight);
|
||||||
|
weightSort.sort(personList);
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD>" << endl;
|
||||||
|
for (size_t i = 0; i < personList.size(); i++)
|
||||||
|
{
|
||||||
|
personList[i].showMyself();
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunSimpleFactoryMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== SimpleFactory Mode =================" << endl;
|
||||||
|
|
||||||
|
PenFactory factory;
|
||||||
|
Pen* pLinePen = factory.CreatePen(PenTypeLine);
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << pLinePen->getName() << " <20>ڴ<EFBFBD><DAB4><EFBFBD>ַ<EFBFBD><D6B7>" << pLinePen << ", <20><><EFBFBD>ͣ<EFBFBD>" << pLinePen->getType() << endl;
|
||||||
|
|
||||||
|
pLinePen = factory.CreatePen(PenTypeRect);
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << pLinePen->getName() << " <20>ڴ<EFBFBD><DAB4><EFBFBD>ַ<EFBFBD><D6B7>" << pLinePen << ", <20><><EFBFBD>ͣ<EFBFBD>" << pLinePen->getType() << endl;
|
||||||
|
|
||||||
|
pLinePen = factory.CreatePen(PenTypeEllipse);
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << pLinePen->getName() << " <20>ڴ<EFBFBD><DAB4><EFBFBD>ַ<EFBFBD><D6B7>" << pLinePen << ", <20><><EFBFBD>ͣ<EFBFBD>" << pLinePen->getType() << endl;
|
||||||
|
|
||||||
|
pLinePen = factory.CreatePen(PenTypeRect);
|
||||||
|
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << pLinePen->getName() << " <20>ڴ<EFBFBD><DAB4><EFBFBD>ַ<EFBFBD><D6B7>" << pLinePen << ", <20><><EFBFBD>ͣ<EFBFBD>" << pLinePen->getType() << endl;
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RunAbstractFactoryMode(void)
|
||||||
|
{
|
||||||
|
cout << "=============== AbstractFactory Mode =================" << endl;
|
||||||
|
AbstractFactory* pShapeFactory = FactoryProducer().getFactory("shape");
|
||||||
|
|
||||||
|
Shape* pShape1 = pShapeFactory->getShape("circle");
|
||||||
|
|
||||||
|
if (pShape1 != nullptr)
|
||||||
|
{
|
||||||
|
pShape1->draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
Shape* pShape2 = pShapeFactory->getShape("square");
|
||||||
|
|
||||||
|
if (pShape2 != nullptr)
|
||||||
|
{
|
||||||
|
pShape2->draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
Shape* pShape3 = pShapeFactory->getShape("rectangle");
|
||||||
|
|
||||||
|
if (pShape3 != nullptr)
|
||||||
|
{
|
||||||
|
pShape3->draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractFactory* pColorFactory = FactoryProducer().getFactory("color");
|
||||||
|
|
||||||
|
Color* pColor1 = pColorFactory->getColor("red");
|
||||||
|
|
||||||
|
if (pColor1 != nullptr)
|
||||||
|
{
|
||||||
|
pColor1->fill();
|
||||||
|
}
|
||||||
|
|
||||||
|
Color* pColor2 = pColorFactory->getColor("green");
|
||||||
|
|
||||||
|
if (pColor2 != nullptr)
|
||||||
|
{
|
||||||
|
pColor2->fill();
|
||||||
|
}
|
||||||
|
|
||||||
|
Color* pColor3 = pColorFactory->getColor("yellow");
|
||||||
|
|
||||||
|
if (pColor3 != nullptr)
|
||||||
|
{
|
||||||
|
pColor3->fill();
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
103
src/facade.cpp
Normal file
103
src/facade.cpp
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
#include "facade.hpp"
|
||||||
|
|
||||||
|
void ZIPModel::compress(string srcFilePath, string dstFilePath)
|
||||||
|
{
|
||||||
|
cout << "ZIPģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD>С<EFBFBD>" << srcFilePath << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD>" << endl;
|
||||||
|
cout << "<EFBFBD>ļ<EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << dstFilePath << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZIPModel::decompress(string srcFilePath, string dstFilePath)
|
||||||
|
{
|
||||||
|
cout << "ZIPģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD>С<EFBFBD>" << srcFilePath << "<EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD>ѹ<EFBFBD><EFBFBD>" << endl;
|
||||||
|
cout << "<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << dstFilePath << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZModel::compress(string srcFilePath, string dstFilePath)
|
||||||
|
{
|
||||||
|
cout << "7Zģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD>С<EFBFBD>" << srcFilePath << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD>" << endl;
|
||||||
|
cout << "<EFBFBD>ļ<EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << dstFilePath << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZModel::decompress(string srcFilePath, string dstFilePath)
|
||||||
|
{
|
||||||
|
cout << "7Zģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD>С<EFBFBD>" << srcFilePath << "<EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD>ѹ<EFBFBD><EFBFBD>" << endl;
|
||||||
|
cout << "<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << dstFilePath << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RARModel::compress(string srcFilePath, string dstFilePath)
|
||||||
|
{
|
||||||
|
cout << "RARģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD>С<EFBFBD>" << srcFilePath << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD>" << endl;
|
||||||
|
cout << "<EFBFBD>ļ<EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << dstFilePath << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RARModel::decompress(string srcFilePath, string dstFilePath)
|
||||||
|
{
|
||||||
|
cout << "RARģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD>С<EFBFBD>" << srcFilePath << "<EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD>ѹ<EFBFBD><EFBFBD>" << endl;
|
||||||
|
cout << "<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << dstFilePath << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
CompressionFacade::CompressionFacade(ZIPModel* pZipModel, RARModel* pRarModel, ZModel* pZModel)
|
||||||
|
{
|
||||||
|
m_pRarModel = pRarModel;
|
||||||
|
m_pZModel = pZModel;
|
||||||
|
m_pZipModel = pZipModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
CompressionFacade::~CompressionFacade()
|
||||||
|
{
|
||||||
|
if (m_pRarModel != nullptr)
|
||||||
|
{
|
||||||
|
delete m_pRarModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_pZModel != nullptr)
|
||||||
|
{
|
||||||
|
delete m_pZModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_pZipModel != nullptr)
|
||||||
|
{
|
||||||
|
delete m_pZipModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CompressionFacade::compress(string srcFilePath, string dstFilePath, string type)
|
||||||
|
{
|
||||||
|
string fullName = dstFilePath + '.' + type;
|
||||||
|
if (type == "zip")
|
||||||
|
{
|
||||||
|
m_pZipModel->compress(srcFilePath, fullName);
|
||||||
|
}
|
||||||
|
else if (type == "rar")
|
||||||
|
{
|
||||||
|
m_pRarModel->compress(srcFilePath, fullName);
|
||||||
|
}
|
||||||
|
else if (type == "7z")
|
||||||
|
{
|
||||||
|
m_pZModel->compress(srcFilePath, fullName);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void CompressionFacade::decompress(string srcFilePath, string dstFilePath)
|
||||||
|
{
|
||||||
|
string::iterator iter = find(srcFilePath.begin(), srcFilePath.end(), '.');
|
||||||
|
string baseName(srcFilePath.begin(), iter);
|
||||||
|
|
||||||
|
string extName(iter, srcFilePath.end());
|
||||||
|
|
||||||
|
if (extName == "zip")
|
||||||
|
{
|
||||||
|
m_pZipModel->decompress(srcFilePath, baseName);
|
||||||
|
}
|
||||||
|
else if (extName == "rar")
|
||||||
|
{
|
||||||
|
m_pRarModel->decompress(srcFilePath, baseName);
|
||||||
|
}
|
||||||
|
else if (extName == "7z")
|
||||||
|
{
|
||||||
|
m_pZModel->decompress(srcFilePath, baseName);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
64
src/iteration.cpp
Normal file
64
src/iteration.cpp
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include "iterator.hpp"
|
||||||
|
|
||||||
|
vector<string> NumeralSystem::m_clinics = vector<string>{ "1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "3<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" };;
|
||||||
|
string Customer::getName()
|
||||||
|
{
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Customer::Regist(NumeralSystem* system)
|
||||||
|
{
|
||||||
|
system->pushCustomer(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Customer::setNum(int num)
|
||||||
|
{
|
||||||
|
m_num = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Customer::getNum()
|
||||||
|
{
|
||||||
|
return m_num;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Customer::setClinic(string clinic)
|
||||||
|
{
|
||||||
|
m_clinics = clinic;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Customer::getClinic()
|
||||||
|
{
|
||||||
|
return m_clinics;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NumeralIterator::next()
|
||||||
|
{
|
||||||
|
if (m_curIdx < (int)m_data.size() - 1)
|
||||||
|
{
|
||||||
|
m_curIdx++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Customer* NumeralIterator::current()
|
||||||
|
{
|
||||||
|
return m_curIdx < m_data.size() ? m_data[m_curIdx] : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NumeralSystem::pushCustomer(Customer* pCustomer)
|
||||||
|
{
|
||||||
|
pCustomer->setNum(m_curNum + 1);
|
||||||
|
string click = NumeralSystem::m_clinics[m_curNum % NumeralSystem::m_clinics.size()];
|
||||||
|
pCustomer->setClinic(click);
|
||||||
|
|
||||||
|
m_curNum++;
|
||||||
|
m_pCustomers.push_back(pCustomer);
|
||||||
|
cout << pCustomer->getName() << " <20><><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << m_name << "<EFBFBD>ɹ<EFBFBD><EFBFBD>Һţ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>" <<
|
||||||
|
pCustomer->getNum() << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵȴ<EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
NumeralIterator NumeralSystem::getIterator()
|
||||||
|
{
|
||||||
|
return NumeralIterator(m_pCustomers);
|
||||||
|
}
|
63
src/observe.cpp
Normal file
63
src/observe.cpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#include "observe.hpp"
|
||||||
|
|
||||||
|
int WaterHeater::getTempetature()
|
||||||
|
{
|
||||||
|
return m_temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WaterHeater::setTemperature(int temperature)
|
||||||
|
{
|
||||||
|
m_temperature = temperature;
|
||||||
|
cout << "<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>ˮ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> " << temperature << " <20><>" << endl;
|
||||||
|
HandleObserver();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WaterHeater::RegisterObserver(Observer* inObserver)
|
||||||
|
{
|
||||||
|
vector<Observer*>::iterator iter = find(m_Observers.begin(), m_Observers.end(), inObserver);
|
||||||
|
if (iter == m_Observers.end())
|
||||||
|
{
|
||||||
|
m_Observers.push_back(inObserver);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WaterHeater::UnRegisterObserver(Observer* inObserver)
|
||||||
|
{
|
||||||
|
vector<Observer*>::iterator iter = find(m_Observers.begin(), m_Observers.end(), inObserver);
|
||||||
|
if (iter != m_Observers.end())
|
||||||
|
{
|
||||||
|
m_Observers.erase(iter);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WaterHeater::HandleObserver()
|
||||||
|
{
|
||||||
|
for (vector<Observer*>::iterator iter = m_Observers.begin(); iter != m_Observers.end(); iter++)
|
||||||
|
{
|
||||||
|
(*iter)->Update(this);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WashingMode::Update(WaterHeater* waterHeater) const
|
||||||
|
{
|
||||||
|
if (waterHeater->getTempetature() >= 50 && waterHeater->getTempetature() < 70)
|
||||||
|
{
|
||||||
|
cout << "ˮ<EFBFBD><EFBFBD><EFBFBD>պã<EFBFBD><EFBFBD>¶<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><EFBFBD><EFBFBD>ˡ<EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DrinkingMode::Update(WaterHeater* waterHeater) const
|
||||||
|
{
|
||||||
|
if (waterHeater->getTempetature() >= 100)
|
||||||
|
{
|
||||||
|
cout << "ˮ<EFBFBD><EFBFBD><EFBFBD>տ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˡ<EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
12
src/prototype.cpp
Normal file
12
src/prototype.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "prototype.hpp"
|
||||||
|
|
||||||
|
ConcretePrototype::ConcretePrototype(const ConcretePrototype& c)
|
||||||
|
{
|
||||||
|
cout << "ConcretePrototype copy construct!" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Prototype* ConcretePrototype::Clone()
|
||||||
|
{
|
||||||
|
return new ConcretePrototype(*this);
|
||||||
|
}
|
||||||
|
|
32
src/proxy.cpp
Normal file
32
src/proxy.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "proxy.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
string Subject::getName()
|
||||||
|
{
|
||||||
|
return m_Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RealSubject::request()
|
||||||
|
{
|
||||||
|
cout << "RealSubject to do something" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProxySubject::request()
|
||||||
|
{
|
||||||
|
preRequest();
|
||||||
|
if (m_pSubject != nullptr)
|
||||||
|
{
|
||||||
|
m_pSubject->request();
|
||||||
|
}
|
||||||
|
afterRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProxySubject::preRequest()
|
||||||
|
{
|
||||||
|
cout << "preRequest" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProxySubject::afterRequest()
|
||||||
|
{
|
||||||
|
cout << "afterRequest" << endl;
|
||||||
|
}
|
50
src/simple_factory.cpp
Normal file
50
src/simple_factory.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include "simple_factory.hpp"
|
||||||
|
|
||||||
|
Pen::Pen(string name)
|
||||||
|
{
|
||||||
|
m_name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Pen::getName()
|
||||||
|
{
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
PenType LinePen::getType()
|
||||||
|
{
|
||||||
|
return PenTypeLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
PenType RectanglePen::getType()
|
||||||
|
{
|
||||||
|
return PenTypeRect;
|
||||||
|
}
|
||||||
|
|
||||||
|
PenType EllipsePen::getType()
|
||||||
|
{
|
||||||
|
return PenTypeEllipse;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pen* PenFactory::CreatePen(PenType pentype)
|
||||||
|
{
|
||||||
|
if (m_PenProduct.find(pentype) == m_PenProduct.end())
|
||||||
|
{
|
||||||
|
if (pentype == PenTypeLine)
|
||||||
|
{
|
||||||
|
m_PenProduct[pentype] = new LinePen("ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
}
|
||||||
|
else if(pentype == PenTypeRect)
|
||||||
|
{
|
||||||
|
m_PenProduct[pentype] = new RectanglePen("<EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
}
|
||||||
|
else if (pentype == PenTypeEllipse)
|
||||||
|
{
|
||||||
|
m_PenProduct[pentype] = new EllipsePen("<EFBFBD><EFBFBD>Բ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m_PenProduct[pentype];
|
||||||
|
}
|
18
src/singleton.cpp
Normal file
18
src/singleton.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include "singleton.hpp"
|
||||||
|
|
||||||
|
SingletonA* SingletonA::m_pInstance = nullptr;
|
||||||
|
|
||||||
|
SingletonA* SingletonA::getInstance()
|
||||||
|
{
|
||||||
|
if (m_pInstance == nullptr)
|
||||||
|
{
|
||||||
|
m_pInstance = new SingletonA();
|
||||||
|
}
|
||||||
|
return m_pInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
SingletonB* SingletonB::getInstance()
|
||||||
|
{
|
||||||
|
static SingletonB Instance;
|
||||||
|
return &Instance;
|
||||||
|
}
|
140
src/state.cpp
Normal file
140
src/state.cpp
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
#include "state.hpp"
|
||||||
|
|
||||||
|
void SolidState::behavior(Water* p_water) const
|
||||||
|
{
|
||||||
|
cout << "<EFBFBD><EFBFBD>ǰ״̬Ϊ<EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>Ϊ" << p_water->getTemperature() << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SolidState::isMatch(int stateInfo) const
|
||||||
|
{
|
||||||
|
return stateInfo <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Liquidtate::behavior(Water* p_water) const
|
||||||
|
{
|
||||||
|
cout << "<EFBFBD><EFBFBD>ǰ״̬ΪҺ̬<EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>Ϊ" << p_water->getTemperature() << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Liquidtate::isMatch(int stateInfo) const
|
||||||
|
{
|
||||||
|
return stateInfo > 0 && stateInfo < 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GaseousState::behavior(Water* p_water) const
|
||||||
|
{
|
||||||
|
cout << "<EFBFBD><EFBFBD>ǰ״̬Ϊ<EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>Ϊ" << p_water->getTemperature() << "<EFBFBD><EFBFBD>" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GaseousState::isMatch(int stateInfo) const
|
||||||
|
{
|
||||||
|
return stateInfo >= 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
Water::Water()
|
||||||
|
{
|
||||||
|
addState(new SolidState("<EFBFBD><EFBFBD>̬"));
|
||||||
|
addState(new Liquidtate("Һ̬"));
|
||||||
|
addState(new GaseousState("<EFBFBD><EFBFBD>̬"));
|
||||||
|
|
||||||
|
setTemperature(25);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Water::getTemperature()
|
||||||
|
{
|
||||||
|
return getStateInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Water::setTemperature(int temperature)
|
||||||
|
{
|
||||||
|
setStateInfo(temperature);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Water::riseTemperature(int step)
|
||||||
|
{
|
||||||
|
setTemperature(getTemperature() + step);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Water::reduceTemperature(int step)
|
||||||
|
{
|
||||||
|
setTemperature(getTemperature() - step);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Water::behavior()
|
||||||
|
{
|
||||||
|
State* p_state = getState();
|
||||||
|
p_state->behavior(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
string State::getName()
|
||||||
|
{
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
Context::Context()
|
||||||
|
{
|
||||||
|
m_stateInfo = 0;
|
||||||
|
p_curState = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Context::~Context()
|
||||||
|
{
|
||||||
|
vector<State*>::iterator iter = p_states.begin();
|
||||||
|
for (; iter != p_states.end(); iter++)
|
||||||
|
{
|
||||||
|
delete (*iter); // <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
||||||
|
}
|
||||||
|
p_states.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Context::addState(State* state)
|
||||||
|
{
|
||||||
|
vector<State*>::iterator iter = find(p_states.begin(), p_states.end(), state);
|
||||||
|
if (iter == p_states.end())
|
||||||
|
{
|
||||||
|
p_states.push_back(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Context::changeState(State* state)
|
||||||
|
{
|
||||||
|
vector<State*>::iterator iter = find(p_states.begin(), p_states.end(), state);
|
||||||
|
if (iter == p_states.end())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p_curState == nullptr)
|
||||||
|
{
|
||||||
|
cout << "<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>Ϊ " << state->getName() << endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "<EFBFBD><EFBFBD> " << p_curState->getName() << " <20><>Ϊ " << state->getName() << endl;
|
||||||
|
}
|
||||||
|
p_curState = state;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
State* Context::getState()
|
||||||
|
{
|
||||||
|
return p_curState;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Context::setStateInfo(int stateInfo)
|
||||||
|
{
|
||||||
|
m_stateInfo = stateInfo;
|
||||||
|
for (vector<State*>::iterator iter = p_states.begin(); iter != p_states.end(); iter++)
|
||||||
|
{
|
||||||
|
if ((*iter)->isMatch(stateInfo))
|
||||||
|
{
|
||||||
|
changeState((*iter));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int Context::getStateInfo()
|
||||||
|
{
|
||||||
|
return m_stateInfo;
|
||||||
|
}
|
47
src/strategy.cpp
Normal file
47
src/strategy.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include "strategy.hpp"
|
||||||
|
|
||||||
|
SortPerson::SortPerson(ICompare* pICompare)
|
||||||
|
{
|
||||||
|
m_CompareStrategy = pICompare;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SortPerson::sort(vector<StrategyPerson>& personList)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < personList.size() - 1; i++) // Bubble sort
|
||||||
|
{
|
||||||
|
for (size_t j = i + 1; j < personList.size(); j++)
|
||||||
|
{
|
||||||
|
if (m_CompareStrategy->comparable(&personList[i], &personList[j]))
|
||||||
|
{
|
||||||
|
StrategyPerson temp = personList[i];
|
||||||
|
personList[i] = personList[j];
|
||||||
|
personList[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CompareByWeight::comparable(StrategyPerson* person1, StrategyPerson* person2)
|
||||||
|
{
|
||||||
|
return person1->m_weight < person2->m_weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CompareByHeight::comparable(StrategyPerson* person1, StrategyPerson* person2)
|
||||||
|
{
|
||||||
|
return person1->m_height < person2->m_height;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CompareByAge::comparable(StrategyPerson* person1, StrategyPerson* person2)
|
||||||
|
{
|
||||||
|
return person1->m_age < person2->m_age;
|
||||||
|
}
|
||||||
|
|
||||||
|
StrategyPerson::StrategyPerson(string name, int age, double weight, double height)
|
||||||
|
{
|
||||||
|
m_name = name, m_age = age, m_weight = weight, m_height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
void StrategyPerson::showMyself()
|
||||||
|
{
|
||||||
|
cout << m_name << " <20><><EFBFBD>䣺" << m_age << "<EFBFBD><EFBFBD>, <20><><EFBFBD>أ<EFBFBD>" << m_weight << "kg, <20><><EFBFBD>ߣ<EFBFBD>" << m_height << "m" << endl;
|
||||||
|
}
|
Reference in New Issue
Block a user