#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 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; }