initialize repository of design pattern template
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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 << "申请请假" << m_Dayoff << "天,请假事由:" << 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 << "同意" << pPerson->getName() << "请假,签字人:" << getName() << "(" << getTitle() << ")" << endl;
|
||||
}
|
||||
if (m_pNextHandle != nullptr)
|
||||
{
|
||||
m_pNextHandle->handleRequest(pPerson);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DepartmentManager::handleRequest(PersonA* pPerson)
|
||||
{
|
||||
if (pPerson->getDayoff() <= 5 && pPerson->getDayoff() > 2)
|
||||
{
|
||||
cout << "同意" << pPerson->getName() << "请假,签字人:" << getName() << "(" << getTitle() << ")" << endl;
|
||||
}
|
||||
if (m_pNextHandle != nullptr)
|
||||
{
|
||||
m_pNextHandle->handleRequest(pPerson);
|
||||
}
|
||||
}
|
||||
|
||||
void CEO::handleRequest(PersonA* pPerson)
|
||||
{
|
||||
if (pPerson->getDayoff() > 5)
|
||||
{
|
||||
cout << "同意" << pPerson->getName() << "请假,签字人:" << getName() << "(" << getTitle() << ")" << endl;
|
||||
}
|
||||
if (m_pNextHandle != nullptr)
|
||||
{
|
||||
m_pNextHandle->handleRequest(pPerson);
|
||||
}
|
||||
}
|
||||
|
||||
void Administrator::handleRequest(PersonA* pPerson)
|
||||
{
|
||||
|
||||
cout << pPerson->getName() << "的请假申请已审核,情况属实!已备案处理。处理人:"
|
||||
<< getName() << "(" << getTitle() << ")" << endl;
|
||||
|
||||
cout << endl;
|
||||
|
||||
if (m_pNextHandle != nullptr)
|
||||
{
|
||||
m_pNextHandle->handleRequest(pPerson);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
#include "combination.hpp"
|
||||
|
||||
bool ComputerComponent::isComposite()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void ComputerComponent::startup(string indent)
|
||||
{
|
||||
cout << indent << m_name << " " << "准备开始工作" << endl;
|
||||
}
|
||||
|
||||
void ComputerComponent::shutdown(string indent)
|
||||
{
|
||||
cout << indent << m_name << " " << "即将结束工作" << endl;
|
||||
}
|
||||
|
||||
void CPU::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "CPU:" << indent << ",可以进行高速运算。" << endl;
|
||||
}
|
||||
|
||||
void MemoryCard::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "内存:" << indent << ",可以缓存数据,读写速度快。" << endl;
|
||||
}
|
||||
|
||||
void HardDisk::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "硬盘:" << indent << ",可以永久存储数据,存储容量大。" << endl;
|
||||
}
|
||||
|
||||
void GraphicsCard::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "显卡:" << indent << ",可以高速计算和处理图形图像。" << endl;
|
||||
}
|
||||
|
||||
void Battery::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "电源:" << indent << ",可以持续给主板和外接配件供电。" << endl;
|
||||
}
|
||||
|
||||
void Fan::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "风扇:" << indent << ",辅助CPU散热。" << endl;
|
||||
}
|
||||
|
||||
void Displayer::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "显示器:" << indent << ",负责内容的显示。" << endl;
|
||||
}
|
||||
|
||||
void ComputerCompsite::showInfo(string indent)
|
||||
{
|
||||
cout << m_name << ",由以下部分组成:" << 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 << "主板:";
|
||||
ComputerCompsite::showInfo(indent);
|
||||
}
|
||||
|
||||
void ComputerCase::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "机箱:";
|
||||
ComputerCompsite::showInfo(indent);
|
||||
}
|
||||
|
||||
void Computer::showInfo(string indent)
|
||||
{
|
||||
cout << indent << "电脑:";
|
||||
ComputerCompsite::showInfo(indent);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#include "decorate.hpp"
|
||||
|
||||
void Person::wear()
|
||||
{
|
||||
cout << "着装" << endl;
|
||||
}
|
||||
|
||||
string Engineer::getSkill()
|
||||
{
|
||||
return m_Skill;
|
||||
}
|
||||
|
||||
string Teacher::getTitle()
|
||||
{
|
||||
return m_Title;
|
||||
}
|
||||
|
||||
void Teacher::wear()
|
||||
{
|
||||
cout << "我是" << m_Name << m_Title << endl;
|
||||
Person::wear();
|
||||
}
|
||||
|
||||
void Engineer::wear()
|
||||
{
|
||||
cout << "我是" << m_Skill << "工程师 " << 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 << "一件卡其色休闲裤" << endl;
|
||||
}
|
||||
|
||||
void BeltDecorator::decorate()
|
||||
{
|
||||
cout << "一条银色针扣头得黑色腰带" << endl;
|
||||
}
|
||||
|
||||
void LeatherShoesDecorator::decorate()
|
||||
{
|
||||
cout << "一双深色休闲皮鞋" << endl;
|
||||
}
|
||||
|
||||
void KnittedSweaterDecorator::decorate()
|
||||
{
|
||||
cout << "一件紫红色针织毛衣" << endl;
|
||||
}
|
||||
|
||||
void WhiteShirtDecorator::decorate()
|
||||
{
|
||||
cout << "一件白色衬衫" << endl;
|
||||
}
|
||||
|
||||
void GlassesDecorator::decorate()
|
||||
{
|
||||
cout << "一副方形黑框眼镜" << endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -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", "客户端");
|
||||
|
||||
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); // 链表 每个类中存储有上个类指针 实现了递归调用
|
||||
|
||||
clothes->wear();
|
||||
|
||||
cout << endl;
|
||||
|
||||
ClothingDecorator* decorateTeacher = new GlassesDecorator(new WhiteShirtDecorator(
|
||||
new LeatherShoesDecorator(new Teacher("Jack", "教授"))));
|
||||
|
||||
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", "客户端研发部经理");
|
||||
Manager* departmentManager = new DepartmentManager("Eric", "技术研发中心总监");
|
||||
Manager* ceo = new CEO("Helen", "创新文化公司CEO");
|
||||
Manager* administrator = new Administrator("Nina", "行政中心总监");
|
||||
|
||||
// 构建责任链条
|
||||
directorLeader->setNextHander(departmentManager);
|
||||
departmentManager->setNextHander(ceo);
|
||||
ceo->setNextHander(administrator);
|
||||
|
||||
PersonA sunny("Sunny", 1, "参加MDCC大会");
|
||||
sunny.setLeader(directorLeader);
|
||||
sunny.Request();
|
||||
|
||||
PersonA tony("Tony", 5, "家中有紧急事情");
|
||||
tony.setLeader(directorLeader);
|
||||
tony.Request();
|
||||
|
||||
PersonA pony("Pony", 15, "出国深造");
|
||||
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;\\标准文件\\生活中的外观模式.md", "E;\\压缩文件\\生活中的外观模式", "zip");
|
||||
facade.decompress("E;\\压缩文件\\生活中的外观模式.zip", "E;\\标准文件\\生活中的外观模式.md");
|
||||
|
||||
facade.compress("E;\\标准文件\\C++编程-入门到放弃.md", "E;\\压缩文件\\C++编程-入门到放弃", "rar");
|
||||
facade.decompress("E;\\压缩文件\\C++编程-入门到放弃.rar", "E;\\标准文件\\C++编程-入门到放弃.md");
|
||||
|
||||
facade.compress("E;\\标准文件\\可复用的软件结构设计.md", "E;\\压缩文件\\可复用的软件结构设计", "7z");
|
||||
facade.decompress("E;\\压缩文件\\可复用的软件结构设计.7z", "E;\\标准文件\\可复用的软件结构设计.md");
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void RunIteratorMode()
|
||||
{
|
||||
cout << "=============== Iterator Mode =================" << endl;
|
||||
NumeralSystem numeralSystem("挂号台");
|
||||
|
||||
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(); // 类的迭代器
|
||||
while (iter.next())
|
||||
{
|
||||
Customer* pCustomer = iter.current();
|
||||
cout << "下一位病人:" << pCustomer->getNum() << "(" << pCustomer->getName() <<
|
||||
") 请到" << pCustomer->getClinic() << "就诊" << 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 电脑");
|
||||
pcomputer->addComponent(pcomputerCase);
|
||||
pcomputer->addComponent(new Displayer("AOC LV243XIP"));
|
||||
|
||||
pcomputer->showInfo("");
|
||||
cout << endl << "开机过程:";
|
||||
pcomputer->startup("");
|
||||
cout << endl << "关机过程:";
|
||||
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 << "根据年龄进行排序的结果" << endl;
|
||||
for (size_t i = 0; i < personList.size(); i++)
|
||||
{
|
||||
personList[i].showMyself();
|
||||
}
|
||||
|
||||
SortPerson weightSort(new CompareByWeight);
|
||||
weightSort.sort(personList);
|
||||
cout << "根据体重进行排序的结果" << 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 << "创建了" << pLinePen->getName() << " 内存地址:" << pLinePen << ", 类型:" << pLinePen->getType() << endl;
|
||||
|
||||
pLinePen = factory.CreatePen(PenTypeRect);
|
||||
cout << "创建了" << pLinePen->getName() << " 内存地址:" << pLinePen << ", 类型:" << pLinePen->getType() << endl;
|
||||
|
||||
pLinePen = factory.CreatePen(PenTypeEllipse);
|
||||
cout << "创建了" << pLinePen->getName() << " 内存地址:" << pLinePen << ", 类型:" << pLinePen->getType() << endl;
|
||||
|
||||
pLinePen = factory.CreatePen(PenTypeRect);
|
||||
cout << "创建了" << pLinePen->getName() << " 内存地址:" << pLinePen << ", 类型:" << 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
@@ -0,0 +1,103 @@
|
||||
#include "facade.hpp"
|
||||
|
||||
void ZIPModel::compress(string srcFilePath, string dstFilePath)
|
||||
{
|
||||
cout << "ZIP模块正在进行“" << srcFilePath << "”的压缩" << endl;
|
||||
cout << "文件压缩成功,已保存至“" << dstFilePath << "”" << endl;
|
||||
}
|
||||
|
||||
void ZIPModel::decompress(string srcFilePath, string dstFilePath)
|
||||
{
|
||||
cout << "ZIP模块正在进行“" << srcFilePath << "”的解压缩" << endl;
|
||||
cout << "文件解压缩成功,已保存至“" << dstFilePath << "”" << endl;
|
||||
}
|
||||
|
||||
void ZModel::compress(string srcFilePath, string dstFilePath)
|
||||
{
|
||||
cout << "7Z模块正在进行“" << srcFilePath << "”的压缩" << endl;
|
||||
cout << "文件压缩成功,已保存至“" << dstFilePath << "”" << endl;
|
||||
}
|
||||
|
||||
void ZModel::decompress(string srcFilePath, string dstFilePath)
|
||||
{
|
||||
cout << "7Z模块正在进行“" << srcFilePath << "”的解压缩" << endl;
|
||||
cout << "文件解压缩成功,已保存至“" << dstFilePath << "”" << endl;
|
||||
}
|
||||
|
||||
void RARModel::compress(string srcFilePath, string dstFilePath)
|
||||
{
|
||||
cout << "RAR模块正在进行“" << srcFilePath << "”的压缩" << endl;
|
||||
cout << "文件压缩成功,已保存至“" << dstFilePath << "”" << endl;
|
||||
}
|
||||
|
||||
void RARModel::decompress(string srcFilePath, string dstFilePath)
|
||||
{
|
||||
cout << "RAR模块正在进行“" << srcFilePath << "”的解压缩" << endl;
|
||||
cout << "文件解压缩成功,已保存至“" << dstFilePath << "”" << 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;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "iterator.hpp"
|
||||
|
||||
vector<string> NumeralSystem::m_clinics = vector<string>{ "1号诊室", "2号诊室", "3号诊室" };;
|
||||
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() << " 您好!您已在" << m_name << "成功挂号,序号:" <<
|
||||
pCustomer->getNum() << ",请耐心等待" << endl;
|
||||
}
|
||||
|
||||
NumeralIterator NumeralSystem::getIterator()
|
||||
{
|
||||
return NumeralIterator(m_pCustomers);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "observe.hpp"
|
||||
|
||||
int WaterHeater::getTempetature()
|
||||
{
|
||||
return m_temperature;
|
||||
}
|
||||
|
||||
void WaterHeater::setTemperature(int temperature)
|
||||
{
|
||||
m_temperature = temperature;
|
||||
cout << "当前的水温是 " << temperature << " ℃" << 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 << "水已烧好!温度正好,可以用来洗澡了。" << endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void DrinkingMode::Update(WaterHeater* waterHeater) const
|
||||
{
|
||||
if (waterHeater->getTempetature() >= 100)
|
||||
{
|
||||
cout << "水已烧开!可以饮用了。" << endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "prototype.hpp"
|
||||
|
||||
ConcretePrototype::ConcretePrototype(const ConcretePrototype& c)
|
||||
{
|
||||
cout << "ConcretePrototype copy construct!" << endl;
|
||||
}
|
||||
|
||||
Prototype* ConcretePrototype::Clone()
|
||||
{
|
||||
return new ConcretePrototype(*this);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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("ÖąĎßťąĘ");
|
||||
}
|
||||
else if(pentype == PenTypeRect)
|
||||
{
|
||||
m_PenProduct[pentype] = new RectanglePen("žŘĐÎťąĘ");
|
||||
}
|
||||
else if (pentype == PenTypeEllipse)
|
||||
{
|
||||
m_PenProduct[pentype] = new EllipsePen("ÍÖÔ˛ťąĘ");
|
||||
}
|
||||
else
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return m_PenProduct[pentype];
|
||||
}
|
||||
@@ -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
@@ -0,0 +1,140 @@
|
||||
#include "state.hpp"
|
||||
|
||||
void SolidState::behavior(Water* p_water) const
|
||||
{
|
||||
cout << "当前状态为固态,温度为" << p_water->getTemperature() << "℃" << endl;
|
||||
}
|
||||
|
||||
bool SolidState::isMatch(int stateInfo) const
|
||||
{
|
||||
return stateInfo <= 0;
|
||||
}
|
||||
|
||||
void Liquidtate::behavior(Water* p_water) const
|
||||
{
|
||||
cout << "当前状态为液态,温度为" << p_water->getTemperature() << "℃" << endl;
|
||||
}
|
||||
|
||||
bool Liquidtate::isMatch(int stateInfo) const
|
||||
{
|
||||
return stateInfo > 0 && stateInfo < 100;
|
||||
}
|
||||
|
||||
void GaseousState::behavior(Water* p_water) const
|
||||
{
|
||||
cout << "当前状态为气态,温度为" << p_water->getTemperature() << "℃" << endl;
|
||||
}
|
||||
|
||||
bool GaseousState::isMatch(int stateInfo) const
|
||||
{
|
||||
return stateInfo >= 100;
|
||||
}
|
||||
|
||||
Water::Water()
|
||||
{
|
||||
addState(new SolidState("固态"));
|
||||
addState(new Liquidtate("液态"));
|
||||
addState(new GaseousState("气态"));
|
||||
|
||||
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); // 释放内存
|
||||
}
|
||||
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 << "初始化为 " << state->getName() << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "由 " << p_curState->getName() << " 变为 " << 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;
|
||||
}
|
||||
@@ -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 << " ÄêÁ䣺" << m_age << "Ëê, ÌåÖØ£º" << m_weight << "kg, Éí¸ß£º" << m_height << "m" << endl;
|
||||
}
|
||||
Reference in New Issue
Block a user