From 9e09e3e8061836331eb65e762c78c2a7cf2bf5da Mon Sep 17 00:00:00 2001 From: klVIllaGerG <805094070@qq.com> Date: Wed, 27 Dec 2023 16:50:06 +0800 Subject: [PATCH 1/6] =?UTF-8?q?gyy=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gyy上传RAII、Facade、Compose设计模式 --- AbstractFactory/AbstractFactory.cpp | 38 --------- AbstractFactory/AbstractFactory.h | 113 -------------------------- Adapter/Adapter.cpp | 36 --------- Adapter/Adapter.h | 121 ---------------------------- Bridge/Bridge.cpp | 103 ----------------------- Bridge/Bridge.h | 109 ------------------------- Builder/Builder.cpp | 59 -------------- Builder/Builder.h | 62 -------------- Command/Command.cpp | 38 --------- Command/Command.h | 68 ---------------- Compose/Compose.cpp | 43 ++++++++++ Compose/Compose.h | 74 +++++++++++++++++ Facade/Facade.cpp | 109 +++++++++++++++++++++++++ Facade/Facade.h | 35 ++++++++ FactoryMethod/FactoryMethod.cpp | 1 - FactoryMethod/FactoryMethod.h | 1 - Flyweight/Flyweight.h | 54 ------------- Flyweight/Flyweigth.cpp | 33 -------- FrontController/FrontController.cpp | 18 +++-- Proxy/Proxy.cpp | 38 --------- Proxy/Proxy.h | 112 ------------------------- RAII/RAII.cpp | 17 ++++ RAII/RAII.h | 34 ++++++++ README.md | 2 - Strategy/Strategy.cpp | 53 ------------ Strategy/Strategy.h | 109 ------------------------- main.cpp | 45 ----------- utils/cct_tools.cpp | 48 ----------- utils/cct_tools.h | 46 ----------- 29 files changed, 325 insertions(+), 1294 deletions(-) delete mode 100644 AbstractFactory/AbstractFactory.cpp delete mode 100644 AbstractFactory/AbstractFactory.h delete mode 100644 Adapter/Adapter.cpp delete mode 100644 Adapter/Adapter.h delete mode 100644 Bridge/Bridge.cpp delete mode 100644 Bridge/Bridge.h delete mode 100644 Builder/Builder.cpp delete mode 100644 Builder/Builder.h delete mode 100644 Command/Command.cpp delete mode 100644 Command/Command.h create mode 100644 Compose/Compose.cpp create mode 100644 Compose/Compose.h create mode 100644 Facade/Facade.cpp create mode 100644 Facade/Facade.h delete mode 100644 FactoryMethod/FactoryMethod.cpp delete mode 100644 FactoryMethod/FactoryMethod.h delete mode 100644 Flyweight/Flyweight.h delete mode 100644 Flyweight/Flyweigth.cpp delete mode 100644 Proxy/Proxy.cpp delete mode 100644 Proxy/Proxy.h create mode 100644 RAII/RAII.cpp create mode 100644 RAII/RAII.h delete mode 100644 README.md delete mode 100644 Strategy/Strategy.cpp delete mode 100644 Strategy/Strategy.h delete mode 100644 main.cpp delete mode 100644 utils/cct_tools.cpp delete mode 100644 utils/cct_tools.h diff --git a/AbstractFactory/AbstractFactory.cpp b/AbstractFactory/AbstractFactory.cpp deleted file mode 100644 index b995a61..0000000 --- a/AbstractFactory/AbstractFactory.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "AbstractFactory.h" - -using namespace std; - -void testAbstractFactory() { - // 代理模式示例 - IBouquet* deliveryProxy = new DeliveryProxy(); - deliveryProxy->deliver("曼彻斯特-维多利亚街"); - - // 抽象工厂模式示例 - IBouquetFactory* highEndFactory = new HighEndBouquetFactory(); - IBouquet* highEndBouquet = highEndFactory->createBouquet(); - highEndBouquet->deliver("曼彻斯特-维多利亚街-曼彻斯特大教堂"); - - IBouquetFactory* economyFactory = new EconomyBouquetFactory(); - IBouquet* economyBouquet = economyFactory->createBouquet(); - economyBouquet->deliver("曼彻斯特-维多利亚街-曼彻斯特大学"); - - delete deliveryProxy; - delete highEndFactory; - delete economyFactory; - delete highEndBouquet; - delete economyBouquet; - -} - -/* -int main() -{ - testAbstractFactory(); -}*/ diff --git a/AbstractFactory/AbstractFactory.h b/AbstractFactory/AbstractFactory.h deleted file mode 100644 index 0b05714..0000000 --- a/AbstractFactory/AbstractFactory.h +++ /dev/null @@ -1,113 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -// 花束接口 -class IBouquet { -public: - virtual void deliver(std::string address) = 0; -}; - -// 花店类 -class FlowerShop : public IBouquet { -public: - void deliver(std::string address) override; -}; - -// 送货代理类 -class DeliveryProxy : public IBouquet { -private: - FlowerShop* flowerShop; -public: - DeliveryProxy(); - void deliver(std::string address) override; -}; - -// 抽象工厂模式 - -// 前置声明 -class HighEndBouquet; -class EconomyBouquet; - -// 花束工厂接口 -class IBouquetFactory { -public: - virtual IBouquet* createBouquet() = 0; -}; - -// 高端花束类 -class HighEndBouquet : public IBouquet { -public: - void deliver(std::string address) override; -}; - -// 经济型花束类 -class EconomyBouquet : public IBouquet { -public: - void deliver(std::string address) override; -}; - -// 高端花束工厂 -class HighEndBouquetFactory : public IBouquetFactory { -public: - IBouquet* createBouquet() override; -private: - std::string Hf[2]; -}; - -// 经济型花束工厂 -class EconomyBouquetFactory : public IBouquetFactory { -public: - IBouquet* createBouquet() override; -private: - std::string Ef[2]; -}; - -void FlowerShop::deliver(std::string address) { - cout << "花店将花束送到地址:" << address << endl; -} - -DeliveryProxy::DeliveryProxy() { - flowerShop = new FlowerShop(); -} - -void DeliveryProxy::deliver(std::string address) { - cout << "代理收到送货请求,将协调外部快递公司来完成送货任务。" << endl; - flowerShop->deliver(address); -} - -void HighEndBouquet::deliver(std::string address) { - cout << "高端花束将花束送到地址:" << address << endl; -} - -void EconomyBouquet::deliver(std::string address) { - cout << "经济型花束将花束送到地址:" << address << endl; -} - -IBouquet* HighEndBouquetFactory::createBouquet() { - cout << "创建高端花束。" << endl; - // 实际创建高端花束的代码 - Hf[0] = "弗洛伊德玫瑰"; - Hf[1] = "岚黛"; - std::cout << Hf[0] << ' ' << Hf[1] << std::endl; - return new HighEndBouquet(); -} - -IBouquet* EconomyBouquetFactory::createBouquet() { - cout << "创建经济型花束。" << endl; - // 实际创建经济型花束的代码 - Ef[0] = "波斯菊"; - Ef[1] = "向日葵"; - std::cout << Ef[0] << ' ' << Ef[1] << std::endl; - return new EconomyBouquet(); -} - -void testAbstractFactory(); diff --git a/Adapter/Adapter.cpp b/Adapter/Adapter.cpp deleted file mode 100644 index d9be929..0000000 --- a/Adapter/Adapter.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include "Adapter.h" -using namespace std; - -void testAdaptor() -{ - std::cout << "ϵͳӲͬĻܹӦ̻ȡϢͨģʽͳһͬĽӿ" - << "ԱڻϵͳзشչʾЩϢ" << std::endl - << std::endl; - - // ʹһܹӦ̵ݣ̬ü۸ơɫ - FlowerSupplier1 supplier1; - double price1 = 20.0; // ûܼ۸ - std::string name1 = "õ"; - std::string description1 = "1ṩ"; - std::string color1 = "ɫ"; - FlowerInfo* adapter1 = new FlowerSupplierAdapter1(supplier1, price1, name1, description1, color1); - std::cout << "Flower Name: " << adapter1->getName() << ", Price: " << adapter1->getPrice() - << ", Description: " << adapter1->getDescription() << ", Color: " << adapter1->getColor() << std::endl; - - // ʹڶܹӦ̵ݣ̬ü۸ơɫ - FlowerSupplier2 supplier2; - double price2 = 25.0; // ûܼ۸ - std::string name2 = "ٺ"; - std::string description2 = "2ṩ"; - std::string color2 = "ɫ"; - FlowerInfo* adapter2 = new FlowerSupplierAdapter2(supplier2, price2, name2, description2, color2); - std::cout << "Flower Name: " << adapter2->getName() << ", Price: " << adapter2->getPrice() - << ", Description: " << adapter2->getDescription() << ", Color: " << adapter2->getColor() << std::endl; - - delete adapter1; - delete adapter2; -} - - - diff --git a/Adapter/Adapter.h b/Adapter/Adapter.h deleted file mode 100644 index cdaeb3a..0000000 --- a/Adapter/Adapter.h +++ /dev/null @@ -1,121 +0,0 @@ -#include -using std::string; -using std::cout; -using std::endl; - -// Ŀӿ -class FlowerInfo { -public: - virtual std::string getName() const = 0; - virtual double getPrice() const = 0; - virtual std::string getDescription() const = 0; - virtual std::string getColor() const = 0; -}; - -// һܹӦ̵ԭʼӿ -class FlowerSupplier1 { -public: - std::string getFlowerName() const { - return "Supplier1 Flower"; - } - - double getFlowerPrice() const { - return 0.0; // ۸ĬΪ0.0۸̬ - } - - std::string getFlowerDescription() const { - return "Beautiful flower from Supplier1"; - } - - std::string getFlowerColor() const { - return "Red"; - } -}; - -// һܹӦ̵ -class FlowerSupplierAdapter1 : public FlowerInfo { -private: - FlowerSupplier1 source; - double price; - std::string name; - std::string description; - std::string color; - -public: - FlowerSupplierAdapter1(const FlowerSupplier1& supplier, double p, const std::string& n, - const std::string& desc, const std::string& col) - : source(supplier), price(p), name(n), description(desc), color(col) {} - - std::string getName() const override { - return name.empty() ? source.getFlowerName() : name; - } - - double getPrice() const override { - return price; - } - - std::string getDescription() const override { - return description.empty() ? source.getFlowerDescription() : description; - } - - std::string getColor() const override { - return color.empty() ? source.getFlowerColor() : color; - } -}; - -// ڶܹӦ̵ԭʼӿ -class FlowerSupplier2 { -public: - std::string fetchFlower() const { - return "Supplier2 Flower"; - } - - double getCost() const { - return 0.0; // ۸ĬΪ0.0۸̬ - } - - std::string getFlowerDetails() const { - return "Supplier2 provides high-quality flowers"; - } - - std::string getFlowerColor() const { - return "Blue"; - } -}; - -// ڶܹӦ̵ -class FlowerSupplierAdapter2 : public FlowerInfo { -private: - FlowerSupplier2 source; - double price; - std::string name; - std::string description; - std::string color; - -public: - FlowerSupplierAdapter2(const FlowerSupplier2& supplier, double p, const std::string& n, - const std::string& desc, const std::string& col) - : source(supplier), price(p), name(n), description(desc), color(col) {} - - std::string getName() const override { - return name.empty() ? source.fetchFlower() : name; - } - - double getPrice() const override { - return price; - } - - std::string getDescription() const override { - return description.empty() ? source.getFlowerDetails() : description; - } - - std::string getColor() const override { - return color.empty() ? source.getFlowerColor() : color; - } -}; - -void testAdaptor(); - - - - diff --git a/Bridge/Bridge.cpp b/Bridge/Bridge.cpp deleted file mode 100644 index ccc3c07..0000000 --- a/Bridge/Bridge.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include -#include "Bridge.h" - - -//ɫľʵ -void PinkAndWhite::setattribute() -{ - colour = "ɫϵ۰ɫɫϵ"; -} - -void RedAndYellow::setattribute() -{ - colour = "ɫϵɫϵ"; -} - -//װʵľʵ -void Plastic::setattribute() -{ - Material = "װʣʹ"; -} - -void Paper::setattribute() -{ - Material = "װʣʹðװֽ"; -} - -//ľʵ -void carnation::setattribute() -{ - category = "ģʹÿܰ"; -} - -void tulip::setattribute() -{ - category = "ģʹ"; -} - -//ͨʵ -void ToMom::setattribute() -{ - implementor1->setattribute(); - implementor2->setattribute(); - implementor3->setattribute(); -} - -void ToMom::print() -{ - cout << "*һλû˸ĸ׵Ļ*" << endl; - cout << implementor1->colour << "." << endl; - cout << implementor2->Material<< endl;//װ - cout << implementor3->category << "." << endl; -} - -//ͨʵ -void ToFriends::setattribute() -{ - implementor1->setattribute(); - implementor2->setattribute(); - implementor3->setattribute(); -} - -void ToFriends::print() -{ - cout << "*һλû˸ѵĻ*" << endl; - cout << implementor1->colour << "." << endl; - cout << implementor2->Material<< endl;//װ - cout << implementor3->category << "." << endl; -} - - - -int testBridge() -{ - Implementor* PinkWhite = new PinkAndWhite; - Implementor* RedYellow = new RedAndYellow; - Implementor* usePlastic = new Plastic; - Implementor* usePaper = new Paper; - Implementor* useCarnation = new carnation; - Implementor* useTulip = new tulip; - - bouquet* mom = new ToMom(PinkWhite, usePaper, useCarnation); - mom->setattribute(); - mom->print(); - cout << endl; - - - bouquet* friends = new ToFriends(RedYellow, usePlastic, useTulip); - friends->setattribute(); - friends->print(); - cout << endl; - delete friends; - delete mom; - delete PinkWhite; - delete RedYellow; - delete usePaper; - delete usePlastic; - delete useCarnation; - delete useTulip; - - cout << endl; - - return 0; -} \ No newline at end of file diff --git a/Bridge/Bridge.h b/Bridge/Bridge.h deleted file mode 100644 index ae14d7a..0000000 --- a/Bridge/Bridge.h +++ /dev/null @@ -1,109 +0,0 @@ -#pragma once -#include - -using namespace std; - -//ΪŽӽӿڵʵ -class Implementor -{ -public: - virtual ~Implementor() {} - - string colour; //ɫ - string Material;//װ - string category; // - - virtual void setattribute() = 0; -}; - -//ɫľʵ -class PinkAndWhite : public Implementor -{ - //۰ -public: - ~PinkAndWhite() {} - void setattribute(); -}; - -class RedAndYellow : public Implementor -{ - // -public: - ~RedAndYellow() {} - void setattribute(); -}; - -//װʵľʵ -class Plastic : public Implementor -{ -public: - ~Plastic() {} - void setattribute(); -}; - -class Paper : public Implementor -{ -public: - ~Paper() {} - void setattribute(); -}; - -//ľʵ -class carnation : public Implementor//ܰ -{ -public: - ~carnation() {} - void setattribute(); -}; - -class tulip : public Implementor// -{ -public: - ~tulip() {} - void setattribute(); -}; - - -// -class bouquet -{ -public: - Implementor* implementor1; - Implementor* implementor2; - Implementor* implementor3; - - bouquet(Implementor* impl, Implementor* imp2, Implementor* imp3) : implementor1(impl), implementor2(imp2), implementor3(imp3) {} - - virtual ~bouquet() {} - - virtual void setattribute() = 0; - virtual void print() = 0; -}; - -//չĽӿڣĸףѵIJͬ -class ToMom : public bouquet -{ -public: - ~ToMom() {} - - ToMom(Implementor* impl, Implementor* imp2, Implementor* imp3) : bouquet(impl, imp2, imp3) {} - - void setattribute(); - - void print(); - -}; - -class ToFriends : public bouquet -{ -public: - ~ToFriends() {} - - ToFriends(Implementor* impl, Implementor* imp2, Implementor* imp3) : bouquet(impl, imp2, imp3) {} - - void setattribute(); - - void print(); - -}; -int testBridge(); \ No newline at end of file diff --git a/Builder/Builder.cpp b/Builder/Builder.cpp deleted file mode 100644 index 204d81b..0000000 --- a/Builder/Builder.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "Builder.h" -#include - -void Order::setFlowerType(const std::string& type) { flowerType = type; } -void Order::setQuantity(int qty) { quantity = qty; } -void Order::setStatus(const std::string& status) { this->status = status; } -std::string Order::getInfo() { - return "Flower: " + flowerType + ", Quantity: " + std::to_string(quantity) + ", Status: " + status; -} - -std::shared_ptr OrderBuilder::getOrder() { - return order; -} - -void RoseOrderBuilder::buildFlowerType() { order->setFlowerType("õ"); } -void RoseOrderBuilder::buildQuantity() { order->setQuantity(12); } -void RoseOrderBuilder::buildStatus() { order->setStatus("ڽ"); } - -void LilyOrderBuilder::buildFlowerType() { order->setFlowerType("ٺ"); } -void LilyOrderBuilder::buildQuantity() { order->setQuantity(8); } -void LilyOrderBuilder::buildStatus() { order->setStatus("Ѿ"); } - -Director::~Director() { - if (builder) { - delete builder; - } -} - -void Director::setBuilder(OrderBuilder* b) { - if (builder) { - delete builder; - } - builder = b; -} - -std::shared_ptr Director::construct() { - builder->buildFlowerType(); - builder->buildQuantity(); - builder->buildStatus(); - return builder->getOrder(); -} -void testBuilder() { - Director director; - - // õ廨 - director.setBuilder(new RoseOrderBuilder()); - director.construct(); - std::shared_ptr roseOrder = director.construct(); - std::cout << "һõ廨" << std::endl; - std::cout << roseOrder->getInfo() << std::endl << std::endl; - - // ٺϻ - director.setBuilder(new LilyOrderBuilder()); - director.construct(); - std::shared_ptr lilyOrder = director.construct(); - std::cout << "һʰٺϻ" << std::endl; - std::cout << lilyOrder->getInfo() << std::endl << std::endl; - -} \ No newline at end of file diff --git a/Builder/Builder.h b/Builder/Builder.h deleted file mode 100644 index 63f8ab3..0000000 --- a/Builder/Builder.h +++ /dev/null @@ -1,62 +0,0 @@ -#include -#include - -// Ϣ -class Order { -private: - std::string flowerType; - int quantity; - std::string status; - -public: - void setFlowerType(const std::string& type); - void setQuantity(int qty); - void setStatus(const std::string& status); - std::string getInfo(); -}; - -// ijӿ -class OrderBuilder { -protected: - std::shared_ptr order; - -public: - OrderBuilder() : order(std::make_shared()) {} - virtual ~OrderBuilder() {} - - std::shared_ptr getOrder(); - - virtual void buildFlowerType() = 0; - virtual void buildQuantity() = 0; - virtual void buildStatus() = 0; -}; - -// Ķ -class RoseOrderBuilder : public OrderBuilder { -public: - void buildFlowerType() override; - void buildQuantity() override; - void buildStatus() override; -}; - -class LilyOrderBuilder : public OrderBuilder { -public: - void buildFlowerType() override; - void buildQuantity() override; - void buildStatus() override; -}; - -// ָ࣬嶩Ľ -class Director { -private: - OrderBuilder* builder; - -public: - Director() : builder(nullptr) {} - ~Director(); - - void setBuilder(OrderBuilder* b); - std::shared_ptr construct(); -}; - -void testBuilder(); diff --git a/Command/Command.cpp b/Command/Command.cpp deleted file mode 100644 index 85ab083..0000000 --- a/Command/Command.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "Command.h" -#include -#include -#include -#include -using namespace std; - -void testCommand() { - // - OrderReceiver receiver; - - // ʵ - FlowerShop shop; - - // õ - auto order1 = std::make_shared(receiver, "Rose Bouquet"); - shop.setCommand(order1); - - // ִ - shop.submitOrder(); - Sleep(500); - - // ڶ - auto order2 = std::make_shared(receiver, "Lily Bouquet"); - shop.setCommand(order2); - - // ִ - shop.submitOrder(); - Sleep(500); - - // ڶ - shop.undoOrder(); - Sleep(500); - - // Ҫٴγһ - shop.undoOrder(); - Sleep(500); -} diff --git a/Command/Command.h b/Command/Command.h deleted file mode 100644 index 4053420..0000000 --- a/Command/Command.h +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include -#include -using namespace std; - -// ӿ -class Command { -public: - virtual ~Command() {} - virtual void execute() = 0; - virtual void undo() = 0; -}; - -// ߣľ߼ -class OrderReceiver { -public: - void processOrder(const std::string &order) { - std::cout << ": " << order << std::endl; - } -}; - -// µ -class PlaceOrderCommand : public Command { -private: - OrderReceiver &receiver; - std::string order; -public: - PlaceOrderCommand(OrderReceiver &receiver, const std::string &order) - : receiver(receiver), order(order) {} - void execute() override { - cout << "Ҫ: " << order << endl; - receiver.processOrder(order); - } - void undo() override { - std::cout << "ڳ: " << order << "Ķ" << std::endl; - } -}; - -// ߣ -class FlowerShop { -private: - std::vector> commandHistory; -public: - void setCommand(const std::shared_ptr& command) { - std::cout << "µĿͻ" << std::endl; - commandHistory.push_back(command); - } - - void submitOrder() { - std::cout << "ûµ" << std::endl; - if (!commandHistory.empty()) { - auto& command = commandHistory.back(); - command->execute(); - } - } - - void undoOrder() { - if (!commandHistory.empty()) { - auto& command = commandHistory.back(); - std::cout << "ڳ..." << std::endl; - command->undo(); - commandHistory.pop_back(); - } - } -}; - -void testCommand(); diff --git a/Compose/Compose.cpp b/Compose/Compose.cpp new file mode 100644 index 0000000..57f00bd --- /dev/null +++ b/Compose/Compose.cpp @@ -0,0 +1,43 @@ +#include "Compose.h" + +class FlowerComponent; + +void pressEntertoContinue() { + std::cout << "س..."; + std::cin.ignore(); // û룬ȴس + std::cout << std::endl; +} + +void flowerShopClientCode(FlowerComponent* component) { + std::cout << "Ʒ: " << component->Operation() << std::endl; + pressEntertoContinue(); +} + +void testCompose() { + FlowerComponent* product1 = new SingleFlower("õ廨"); + FlowerComponent* product2 = new SingleFlower("ܰƿ"); + + FlowerComponent* set1 = new FlowerSet("˽رװ"); + set1->Add(product1); + set1->Add(product2); + + FlowerComponent* product3 = new SingleFlower("㻨ƿ"); + + FlowerComponent* set2 = new FlowerSet("װ"); + set2->Add(product3); + + flowerShopClientCode(product1); + flowerShopClientCode(product2); + flowerShopClientCode(set1); + flowerShopClientCode(set2); + + delete product1; + delete product2; + delete set1; + delete product3; + delete set2; +} +int main() +{ + testCompose(); +} \ No newline at end of file diff --git a/Compose/Compose.h b/Compose/Compose.h new file mode 100644 index 0000000..9837786 --- /dev/null +++ b/Compose/Compose.h @@ -0,0 +1,74 @@ +#pragma once +#include +#include +#include + +class FlowerComponent { +protected: + FlowerComponent* parent_; + std::string name; + +public: + FlowerComponent(std::string name) : name(name), parent_(nullptr) {} + + virtual void Add(FlowerComponent* component) {} + virtual void Remove(FlowerComponent* component) {} + virtual bool IsComposite() const { + return false; + } + + virtual void SetParent(FlowerComponent* parent) { + parent_ = parent; + } + + virtual std::string Operation() const = 0; +}; + +class SingleFlower : public FlowerComponent { +public: + SingleFlower(std::string name) : FlowerComponent(name) {} + + std::string Operation() const override { + return "һ: " + name; + } +}; + +class FlowerSet : public FlowerComponent { +private: + std::list children_; + +public: + FlowerSet(std::string name) : FlowerComponent(name) {} + + void Add(FlowerComponent* component) override { + children_.push_back(component); + component->SetParent(this); + } + + void Remove(FlowerComponent* component) override { + children_.remove(component); + component->SetParent(nullptr); + } + + bool IsComposite() const override { + return true; + } + + std::string Operation() const override { + std::string result = "װ: " + name + " "; + for (const FlowerComponent* c : children_) { + if (c == children_.back()) { + result += c->Operation(); + } + else { + result += c->Operation() + ", "; + } + } + return result; + } +}; + +void flowerShopClientCode(FlowerComponent* component); +void pressEntertoContinue(); + +void testCompose(); \ No newline at end of file diff --git a/Facade/Facade.cpp b/Facade/Facade.cpp new file mode 100644 index 0000000..ca9220d --- /dev/null +++ b/Facade/Facade.cpp @@ -0,0 +1,109 @@ +#include "Facade.h" +#include + +class InventorySystem; +class OrderProcessingSystem; +class CustomerServiceSystem; +class FlowerShopFacade; + +void pressEnterToContinue() { + std::cout << "س..."; + std::cin.ignore(); // û룬ȴس + std::cout << std::endl; +} + +// ʵϵͳ1 +void InventorySystem::checkInventory() { + std::cout << "黨ܿ..." << std::endl; + pressEnterToContinue(); +} + +void InventorySystem::updateInventory() { + std::cout << "»ܿ..." << std::endl; + pressEnterToContinue(); +} + +// ʵϵͳ2 +void OrderProcessingSystem::placeOrder() { + std::cout << "µ򻨻..." << std::endl; + pressEnterToContinue(); +} + +void OrderProcessingSystem::processPayment() { + std::cout << "֧..." << std::endl; + pressEnterToContinue(); +} + +// ʵϵͳ3 +void CustomerServiceSystem::provideAssistance() { + std::cout << "ṩͻ..." << std::endl; + pressEnterToContinue(); +} + +// ʵֻࣨFacade +FlowerShopFacade::FlowerShopFacade() { + // ʼϵͳ +} + +FlowerShopFacade::~FlowerShopFacade() { + // ϵͳ +} + +// ʵֹ򻨻ܵķ +void FlowerShopFacade::purchaseFlowers() { + std::cout << "ӭٻ꣡" << std::endl; + InventorySystem().checkInventory(); + OrderProcessingSystem().placeOrder(); + OrderProcessingSystem().processPayment(); + CustomerServiceSystem().provideAssistance(); + std::cout << "лǵĵ̣" << std::endl; + pressEnterToContinue(); +} + +// Żķ +void FlowerShopFacade::arrangeEvent() { + std::cout << "ӭŷ" << std::endl; + // ڴӰŻľ߼ + std::cout << "ɣ" << std::endl; + pressEnterToContinue(); +} + +// ͻķ +void FlowerShopFacade::deliverFlowers() { + std::cout << "ӭͻ" << std::endl; + // ڴͻľ߼ + std::cout << "ѳɹʹ" << std::endl; + pressEnterToContinue(); +} + +// ķ +void FlowerShopFacade::assignRandomActivity() { + std::vector activities = { + &FlowerShopFacade::purchaseFlowers, + &FlowerShopFacade::arrangeEvent, + &FlowerShopFacade::deliverFlowers + }; + + // ѡһ + int randomIndex = rand() % activities.size(); + (this->*activities[randomIndex])(); // ѡĻ +} + +void testFacade() { + srand((unsigned)time(0)); + + FlowerShopFacade flowerShop; + + for (int i = 1; i <= 3; i++) { + std::cout << " " << i << " ˿" << std::endl; + flowerShop.assignRandomActivity(); + std::cout << std::endl; + pressEnterToContinue(); + } +} + + +//int main() +//{ +// testFacade(); +//} \ No newline at end of file diff --git a/Facade/Facade.h b/Facade/Facade.h new file mode 100644 index 0000000..61c643d --- /dev/null +++ b/Facade/Facade.h @@ -0,0 +1,35 @@ +#pragma once +#include + +// ϵͳ1: ܿ +class InventorySystem { +public: + void checkInventory(); // 黨ܿ + void updateInventory(); // »ܿ +}; + +// ϵͳ2: +class OrderProcessingSystem { +public: + void placeOrder(); // µ + void processPayment(); // ֧ +}; + +// ϵͳ3: ͻ +class CustomerServiceSystem { +public: + void provideAssistance(); // ṩͻ +}; + +// ࣨFacade +class FlowerShopFacade { +public: + FlowerShopFacade(); + ~FlowerShopFacade(); + + // ͻѺõķ + void purchaseFlowers(); // 򻨻 + void arrangeEvent(); // Ż + void deliverFlowers(); // ͻ + void assignRandomActivity(); // +}; \ No newline at end of file diff --git a/FactoryMethod/FactoryMethod.cpp b/FactoryMethod/FactoryMethod.cpp deleted file mode 100644 index 8b13789..0000000 --- a/FactoryMethod/FactoryMethod.cpp +++ /dev/null @@ -1 +0,0 @@ - diff --git a/FactoryMethod/FactoryMethod.h b/FactoryMethod/FactoryMethod.h deleted file mode 100644 index 8b13789..0000000 --- a/FactoryMethod/FactoryMethod.h +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Flyweight/Flyweight.h b/Flyweight/Flyweight.h deleted file mode 100644 index addf99f..0000000 --- a/Flyweight/Flyweight.h +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -using namespace std; - -// Ԫӿ -class Flower { -public: - virtual void display(std::string color) const = 0; - virtual ~Flower() {} // ȷȷͷԴ -}; - -// Ԫ࣬ʾĻ -class ConcreteFlower : public Flower { -private: - std::string name; - -public: - ConcreteFlower(std::string n) : name(n) {} - - void display(std::string color) const override { - std::cout << "Flower: " << name << ", Color: " << color << std::endl; - } -}; - -// Ԫ𴴽͹Ԫ -class FlowerFactory { -private: - std::unordered_map flowerCache; - -public: - Flower* getFlower(std::string name) { - // ѾͬƵĻֱܶӷأ򴴽һµĶ󲢻 - if (flowerCache.find(name) != flowerCache.end()) { - return flowerCache[name]; - } - else { - Flower* newFlower = new ConcreteFlower(name); - flowerCache[name] = newFlower; - return newFlower; - } - } - - ~FlowerFactory() { - // ͷŻеԴ - for (auto& pair : flowerCache) { - delete pair.second; - } - flowerCache.clear(); - } -}; - -int testFlyweight(); - diff --git a/Flyweight/Flyweigth.cpp b/Flyweight/Flyweigth.cpp deleted file mode 100644 index fe0cbab..0000000 --- a/Flyweight/Flyweigth.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include"Flyweight.h" -#include -#include - -int testFlyweight() -{ - FlowerFactory flowerFactory; - - // 顾客1购买红色玫瑰 - Flower* redRose = flowerFactory.getFlower("Rose"); - redRose->display("Red"); - - // 顾客2购买蓝色玫瑰 - Flower* blueRose = flowerFactory.getFlower("Rose"); - blueRose->display("Blue"); - - // 顾客3购买红色郁金香 - Flower* redTulip = flowerFactory.getFlower("Tulip"); - redTulip->display("Red"); - - // 顾客4购买白色郁金香 - Flower* whiteTulip = flowerFactory.getFlower("Tulip"); - whiteTulip->display("White"); - - // 顾客5购买蓝色玫瑰 - Flower* anotherBlueRose = flowerFactory.getFlower("Rose"); - anotherBlueRose->display("Blue"); - - // 释放资源,通过~FlowerFactory() 实现 - - return 0; - -} \ No newline at end of file diff --git a/FrontController/FrontController.cpp b/FrontController/FrontController.cpp index 9f9f066..0e3954c 100644 --- a/FrontController/FrontController.cpp +++ b/FrontController/FrontController.cpp @@ -7,6 +7,14 @@ #include"../Strategy/Strategy.h" #include"../Proxy/Proxy.h" #include"../AbstractFactory/AbstractFactory.h" +#include"../interpreter/interpreter.h" +#include"../objectpool/objectpool.h" +#include"../nullobject/nullobject.h" +#include"../Adapter/Adapter.h" +#include"../Flyweight/Flyweight.h" +#include"../Facade/Facade.h" +#include"../RAII/RAII.h" +#include"../Compose/Compose.h" using namespace std; void Dispatcher::dispatch(string request) @@ -42,7 +50,7 @@ void Dispatcher::dispatch(string request) } else if (request == "compose") { - //testCompose(); + testCompose(); } else if (request == "decorator") { @@ -50,7 +58,7 @@ void Dispatcher::dispatch(string request) } else if (request == "facade") { - //testFacade(); + testFacade(); } else if (request == "factoryMethod") { @@ -66,7 +74,7 @@ void Dispatcher::dispatch(string request) } else if (request == "interpreter") { - //testInterpreter(); + test_interpreter(); } else if (request == "iterator") { @@ -86,11 +94,11 @@ void Dispatcher::dispatch(string request) } else if (request == "nullObject") { - //testNullObject(); + test_nullobject(); } else if (request == "objectPool") { - //testObjectPool(); + test_objectpool(); } else if (request == "observer") { diff --git a/Proxy/Proxy.cpp b/Proxy/Proxy.cpp deleted file mode 100644 index 5d03f89..0000000 --- a/Proxy/Proxy.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "Proxy.h" - -using namespace std; - -void testProxy() { - // 代理模式示例 - IBouquet* deliveryProxy = new DeliveryProxy(); - deliveryProxy->deliver("曼彻斯特-维多利亚街"); - - // 抽象工厂模式示例 - IBouquetFactory* highEndFactory = new HighEndBouquetFactory(); - IBouquet* highEndBouquet = highEndFactory->createBouquet(); - highEndBouquet->deliver("曼彻斯特-维多利亚街-曼彻斯特大教堂"); - - IBouquetFactory* economyFactory = new EconomyBouquetFactory(); - IBouquet* economyBouquet = economyFactory->createBouquet(); - economyBouquet->deliver("曼彻斯特-维多利亚街-曼彻斯特大学"); - - delete deliveryProxy; - delete highEndFactory; - delete economyFactory; - delete highEndBouquet; - delete economyBouquet; - -} - -/* -int main() -{ - testProxy(); -}*/ diff --git a/Proxy/Proxy.h b/Proxy/Proxy.h deleted file mode 100644 index ad8b03c..0000000 --- a/Proxy/Proxy.h +++ /dev/null @@ -1,112 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -// 花束接口 -class IBouquet { -public: - virtual void deliver(std::string address) = 0; -}; - -// 花店类 -class FlowerShop : public IBouquet { -public: - void deliver(std::string address) override; -}; - -// 送货代理类 -class DeliveryProxy : public IBouquet { -private: - FlowerShop* flowerShop; -public: - DeliveryProxy(); - void deliver(std::string address) override; -}; - -// 抽象工厂模式 - -// 前置声明 -class HighEndBouquet; -class EconomyBouquet; - -// 花束工厂接口 -class IBouquetFactory { -public: - virtual IBouquet* createBouquet() = 0; -}; - -// 高端花束类 -class HighEndBouquet : public IBouquet { -public: - void deliver(std::string address) override; -}; - -// 经济型花束类 -class EconomyBouquet : public IBouquet { -public: - void deliver(std::string address) override; -}; - -// 高端花束工厂 -class HighEndBouquetFactory : public IBouquetFactory { -public: - IBouquet* createBouquet() override; -private: - std::string Hf[2]; -}; - -// 经济型花束工厂 -class EconomyBouquetFactory : public IBouquetFactory { -public: - IBouquet* createBouquet() override; -private: - std::string Ef[2]; -}; - -void FlowerShop::deliver(std::string address) { - cout << "花店将花束送到地址:" << address << endl; -} - -DeliveryProxy::DeliveryProxy() { - flowerShop = new FlowerShop(); -} - -void DeliveryProxy::deliver(std::string address) { - cout << "代理收到送货请求,将协调外部快递公司来完成送货任务。" << endl; - flowerShop->deliver(address); -} - -void HighEndBouquet::deliver(std::string address) { - cout << "高端花束将花束送到地址:" << address << endl; -} - -void EconomyBouquet::deliver(std::string address) { - cout << "经济型花束将花束送到地址:" << address << endl; -} - -IBouquet* HighEndBouquetFactory::createBouquet() { - cout << "创建高端花束。" << endl; - // 实际创建高端花束的代码 - Hf[0] = "弗洛伊德玫瑰"; - Hf[1] = "岚黛"; - std::cout << Hf[0] << ' ' << Hf[1] << std::endl; - return new HighEndBouquet(); -} - -IBouquet* EconomyBouquetFactory::createBouquet() { - cout << "创建经济型花束。" << endl; - // 实际创建经济型花束的代码 - Ef[0] = "波斯菊"; - Ef[1] = "向日葵"; - std::cout << Ef[0] << ' ' << Ef[1] << std::endl; - return new EconomyBouquet(); -} - -void testProxy(); diff --git a/RAII/RAII.cpp b/RAII/RAII.cpp new file mode 100644 index 0000000..ff26ada --- /dev/null +++ b/RAII/RAII.cpp @@ -0,0 +1,17 @@ +#include "RAII.h" + +void ResourceManagement::acquireResources() { + std::cout << "ȡܡװϵԴ..." << std::endl; +} + +void ResourceManagement::releaseResources() { + std::cout << "ͷŻܡװϵԴ..." << std::endl; +} + +void FlowerShop::sellFlowers() { + std::cout << "..." << std::endl; +} + +void FlowerShop::arrangeEvent() { + std::cout << "ڰŻ..." << std::endl; +} \ No newline at end of file diff --git a/RAII/RAII.h b/RAII/RAII.h new file mode 100644 index 0000000..37d5772 --- /dev/null +++ b/RAII/RAII.h @@ -0,0 +1,34 @@ +#pragma once +#include +#include + +class ResourceManagement { +public: + ResourceManagement() { + acquireResources(); + std::cout << "Դ׼" << std::endl; + } + + ~ResourceManagement() { + releaseResources(); + std::cout << "Դɣ" << std::endl; + } + + void acquireResources(); + + void releaseResources(); +}; + +class FlowerShop { +public: + FlowerShop() { + std::cout << "ӭٻ꣡" << std::endl; + } + + void sellFlowers(); + void arrangeEvent(); + + ~FlowerShop() { + std::cout << "лĹ٣" << std::endl; + } +}; \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 3abddf4..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# Design-pattern -软院设计模式期末大作业 diff --git a/Strategy/Strategy.cpp b/Strategy/Strategy.cpp deleted file mode 100644 index 49279a4..0000000 --- a/Strategy/Strategy.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "Strategy.h" - -void testStrategy() -{ - Strategy* strategyWinter = new Winter; - Strategy* strategySpring = new Spring; - Strategy* strategyTanabata = new Tanabata; - Strategy* strategysalesPromotion = new salesPromotion; - - Adder add1(100, strategyWinter); - std::cout << "ڶԭ100ԪĻ\n"; - add1.Prize(); - - std::cout << "\n"; - Adder add2(300, strategyWinter); - std::cout << "ڶԭ300ԪĻ\n"; - add2.Prize(); - - std::cout << "\n"; - Adder add3(100, strategySpring); - std::cout << "ڴԭ100ԪĻ\n"; - add3.Prize(); - - std::cout << "\n"; - Adder add4(300, strategySpring); - std::cout << "ڴԭ300ԪĻ\n"; - add4.Prize(); - - std::cout << "\n"; - Adder add5(100, strategyTanabata); - std::cout << "Ϧԭ100ԪĻ\n"; - add5.Prize(); - - std::cout << "\n"; - Adder add6(300, strategyTanabata); - std::cout << "Ϧԭ300ԪĻ\n"; - add6.Prize(); - - std::cout << "\n"; - Adder add7(100, strategysalesPromotion); - std::cout << "ڴʱԭ100ԪĻ\n"; - add7.Prize(); - - std::cout << "\n"; - Adder add8(300, strategysalesPromotion); - std::cout << "ڴʱԭ300ԪĻ\n"; - add8.Prize(); - - delete strategyWinter; - delete strategySpring; - delete strategyTanabata; - delete strategysalesPromotion; -} \ No newline at end of file diff --git a/Strategy/Strategy.h b/Strategy/Strategy.h deleted file mode 100644 index 3024bcb..0000000 --- a/Strategy/Strategy.h +++ /dev/null @@ -1,109 +0,0 @@ -#pragma once -#include -#include -#include - -// ۸Զ -class Strategy -{ -public: - virtual ~Strategy() = default; - virtual std::double_t Prize(std::double_t prize) const = 0; -}; - - -class Adder -{ -private: - Strategy* strategy_; - std::double_t prize; -public: - - Adder(std::double_t startPrize) { - this->prize = startPrize; - } - - Adder(std::double_t startPrize, Strategy* strategy) { - this->prize = startPrize; - this->setStrategy(strategy); - } - - //̬޸ - void setStrategy(Strategy* strategy) - { - strategy_ = strategy; - } - //ü۸ʵ - void Prize() const - { - if (strategy_) { - std::cout << "ʼΪ" << prize<Prize(prize); - std::cout << "Ҫ֧"<< result << "Ԫ\n"; - } - else { - std::cout << "޲ԣԭ۸\n"; - } - } -}; - -class Spring : public Strategy -{ -public: - std::double_t Prize(std::double_t prize) const override - { - prize*=1.1; - if (prize >= 200)//200ۿۣ9 - { - return std::double_t(prize*0.9); - } - else - { - return std::double_t(prize); - } - } -}; -class Winter : public Strategy -{ -public: - std::double_t Prize(std::double_t prize) const override - { - prize*=1.2; - if (prize >= 200)//200ۿۣ9 - { - return std::double_t(prize*0.9); - } - else - { - return std::double_t(prize); - } - } -}; - -class Tanabata : public Strategy -{ - std::double_t Prize(std::double_t prize) const override - { - prize*=1.3; - if (prize >= 200)//200ۿۣ9 - { - return std::double_t(prize*0.9); - } - else - { - return std::double_t(prize); - } - } -}; -class salesPromotion : public Strategy// -{ - std::double_t Prize(std::double_t prize) const override - { - prize*=0.8; - return std::double_t(prize); - - } -}; - - -void testStrategy(); \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 8afca36..0000000 --- a/main.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include"FrontController/FrontController.h" - -#include - -using namespace std; - - - -int main() -{ - - Dispatcher dispatcher; - FrontController frontController; - frontController.set(&dispatcher); - const string noTestPattern[] = { "frontController", "RAII", "singleton"}; //ϲģʽ - - - for (int i = 0; i < 30; i++) - { - bool continueFlag = false; - for (auto str : noTestPattern) { - - if (designPatterns[i] == str) { - continueFlag = true; - break; - } - } - - if (continueFlag) { - continue; - } - - - - frontController.dispatchRequest(designPatterns[i]); - system("pause"); - cout << endl; - } - - system("pause"); - - testAll(); - return 0; -} \ No newline at end of file diff --git a/utils/cct_tools.cpp b/utils/cct_tools.cpp deleted file mode 100644 index c1fb1b8..0000000 --- a/utils/cct_tools.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once -#include "./cct_tools.h" - -//ƶָλ -void cct_gotoxy(int x, int y) -{ - COORD c; - c.X = x; c.Y = y; - SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); -} - -//ıɫ -void cct_setcolor(const int bg_color, const int fg_color) -{ - SetConsoleTextAttribute(__hout, bg_color * 16 + fg_color); -} - -//ضλضɫӡַͬ -void cct_showch(const int X, const int Y, const char ch, const int bg_color, const int fg_color, const int rpt) -{ - int i; - - cct_gotoxy(X, Y); - cct_setcolor(bg_color, fg_color); - - /* ѭnΣӡַch */ - for (i = 0; i < rpt; i++) - putchar(ch); -} - -//Ļǿɼ(ʹõǰɫ) -void cct_cls(void) -{ - COORD coord = { 0, 0 }; - CONSOLE_SCREEN_BUFFER_INFO binfo; /* to get buffer info */ - DWORD num; - - /* ȡǰϢ */ - GetConsoleScreenBufferInfo(__hout, &binfo); - /* ַ */ - FillConsoleOutputCharacter(__hout, (TCHAR)' ', binfo.dwSize.X * binfo.dwSize.Y, coord, &num); - /* */ - FillConsoleOutputAttribute(__hout, binfo.wAttributes, binfo.dwSize.X * binfo.dwSize.Y, coord, &num); - - /* ص(0,0) */ - SetConsoleCursorPosition(__hout, coord); - return; -} \ No newline at end of file diff --git a/utils/cct_tools.h b/utils/cct_tools.h deleted file mode 100644 index eb0316b..0000000 --- a/utils/cct_tools.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once -#include -//#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* ɫú궨ȡ֣䣩 */ -#define COLOR_BLACK 0 // -#define COLOR_BLUE 1 // -#define COLOR_GREEN 2 // -#define COLOR_CYAN 3 // -#define COLOR_RED 4 // -#define COLOR_PINK 5 // -#define COLOR_YELLOW 6 // -#define COLOR_WHITE 7 // -#define COLOR_HBLACK 8 // -#define COLOR_HBLUE 9 // -#define COLOR_HGREEN 10 // -#define COLOR_HCYAN 11 // -#define COLOR_HRED 12 // -#define COLOR_HPINK 13 // -#define COLOR_HYELLOW 14 // -#define COLOR_HWHITE 15 // - -static const HANDLE __hout = GetStdHandle(STD_OUTPUT_HANDLE); //ȡ׼豸Ӧľ -static const HANDLE __hin = GetStdHandle(STD_INPUT_HANDLE); //ȡ׼豸Ӧľ - -using namespace std; - -//ƶָλ -void cct_gotoxy(int x, int y); - -//ıɫ -void cct_setcolor(const int bg_color = COLOR_BLACK, const int fg_color = COLOR_WHITE); - -//ضλضɫӡַͬ -void cct_showch(const int X, const int Y, const char ch, const int bg_color, const int fg_color, const int rpt); - -//Ļǿɼ(ʹõǰɫ) -void cct_cls(void); \ No newline at end of file From 7e83ae64cd09b7b8d3cde412a27e5dde4e9b136e Mon Sep 17 00:00:00 2001 From: klVIllaGerG <805094070@qq.com> Date: Wed, 27 Dec 2023 19:32:31 +0800 Subject: [PATCH 2/6] =?UTF-8?q?gyy=20=E4=B8=8A=E4=BC=A02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gyy 上传 --- Compose/Compose.cpp | 4 ---- Facade/Facade.cpp | 11 ++--------- Facade/Facade.h | 4 +++- RAII/RAII.cpp | 4 ++-- RAII/RAII.h | 6 +++--- 5 files changed, 10 insertions(+), 19 deletions(-) diff --git a/Compose/Compose.cpp b/Compose/Compose.cpp index 57f00bd..9701f91 100644 --- a/Compose/Compose.cpp +++ b/Compose/Compose.cpp @@ -37,7 +37,3 @@ void testCompose() { delete product3; delete set2; } -int main() -{ - testCompose(); -} \ No newline at end of file diff --git a/Facade/Facade.cpp b/Facade/Facade.cpp index ca9220d..056909f 100644 --- a/Facade/Facade.cpp +++ b/Facade/Facade.cpp @@ -63,7 +63,6 @@ void FlowerShopFacade::purchaseFlowers() { // Żķ void FlowerShopFacade::arrangeEvent() { std::cout << "ӭŷ" << std::endl; - // ڴӰŻľ߼ std::cout << "ɣ" << std::endl; pressEnterToContinue(); } @@ -71,7 +70,6 @@ void FlowerShopFacade::arrangeEvent() { // ͻķ void FlowerShopFacade::deliverFlowers() { std::cout << "ӭͻ" << std::endl; - // ڴͻľ߼ std::cout << "ѳɹʹ" << std::endl; pressEnterToContinue(); } @@ -92,18 +90,13 @@ void FlowerShopFacade::assignRandomActivity() { void testFacade() { srand((unsigned)time(0)); - FlowerShopFacade flowerShop; + FlowerShopFacade flowerShopGYYF; for (int i = 1; i <= 3; i++) { std::cout << " " << i << " ˿" << std::endl; - flowerShop.assignRandomActivity(); + flowerShopGYYF.assignRandomActivity(); std::cout << std::endl; pressEnterToContinue(); } } - -//int main() -//{ -// testFacade(); -//} \ No newline at end of file diff --git a/Facade/Facade.h b/Facade/Facade.h index 61c643d..d1aee0c 100644 --- a/Facade/Facade.h +++ b/Facade/Facade.h @@ -32,4 +32,6 @@ class FlowerShopFacade { void arrangeEvent(); // Ż void deliverFlowers(); // ͻ void assignRandomActivity(); // -}; \ No newline at end of file +}; + +void testFacade(); \ No newline at end of file diff --git a/RAII/RAII.cpp b/RAII/RAII.cpp index ff26ada..ce448d8 100644 --- a/RAII/RAII.cpp +++ b/RAII/RAII.cpp @@ -8,10 +8,10 @@ void ResourceManagement::releaseResources() { std::cout << "ͷŻܡװϵԴ..." << std::endl; } -void FlowerShop::sellFlowers() { +void FlowerShopGYYR::sellFlowers() { std::cout << "..." << std::endl; } -void FlowerShop::arrangeEvent() { +void FlowerShopGYYR::arrangeEvent() { std::cout << "ڰŻ..." << std::endl; } \ No newline at end of file diff --git a/RAII/RAII.h b/RAII/RAII.h index 37d5772..da62ef7 100644 --- a/RAII/RAII.h +++ b/RAII/RAII.h @@ -19,16 +19,16 @@ class ResourceManagement { void releaseResources(); }; -class FlowerShop { +class FlowerShopGYYR { public: - FlowerShop() { + FlowerShopGYYR() { std::cout << "ӭٻ꣡" << std::endl; } void sellFlowers(); void arrangeEvent(); - ~FlowerShop() { + ~FlowerShopGYYR() { std::cout << "лĹ٣" << std::endl; } }; \ No newline at end of file From 7cc7d28969d5e1291bb63afec388922eb0b590c2 Mon Sep 17 00:00:00 2001 From: klVIllaGerG <805094070@qq.com> Date: Sat, 30 Dec 2023 21:37:49 +0800 Subject: [PATCH 3/6] gyy3 gyy3 --- Compose/Compose.cpp | 46 ++++++++++++++++++--------- Compose/Compose.h | 2 +- Facade/Facade.cpp | 77 +++++++++++++++++++++------------------------ Facade/Facade.h | 1 + 4 files changed, 69 insertions(+), 57 deletions(-) diff --git a/Compose/Compose.cpp b/Compose/Compose.cpp index 9701f91..3b2bf80 100644 --- a/Compose/Compose.cpp +++ b/Compose/Compose.cpp @@ -1,35 +1,53 @@ #include "Compose.h" +#include +#include class FlowerComponent; -void pressEntertoContinue() { - std::cout << "س..."; - std::cin.ignore(); // û룬ȴس - std::cout << std::endl; -} - void flowerShopClientCode(FlowerComponent* component) { - std::cout << "Ʒ: " << component->Operation() << std::endl; - pressEntertoContinue(); + string output_ = component->Operation(); + + std::istringstream ss(output_); + std::string token; + cout << "*Ʒ*"; + static int count = 1; + cct_setcolor(COLOR_BLACK, COLOR_HWHITE - (count++)); + while (std::getline(ss, token, ',')) { + + std::cout << token; + } } void testCompose() { - FlowerComponent* product1 = new SingleFlower("õ廨"); - FlowerComponent* product2 = new SingleFlower("ܰƿ"); + FlowerComponent* product1 = new SingleFlower("*õ廨*"); + FlowerComponent* product2 = new SingleFlower("*ܰ*"); + FlowerComponent* product3 = new SingleFlower("*㻨*"); - FlowerComponent* set1 = new FlowerSet("˽رװ"); + FlowerComponent* set1 = new FlowerSet("*˽رװ*"); set1->Add(product1); set1->Add(product2); - FlowerComponent* product3 = new SingleFlower("㻨ƿ"); - FlowerComponent* set2 = new FlowerSet("װ"); + FlowerComponent* set2 = new FlowerSet("*װ*"); set2->Add(product3); - + set2->Add(product3); + set2->Add(product1); + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + cct_setcolor(COLOR_BLACK, COLOR_HWHITE); flowerShopClientCode(product1); + cct_setcolor(COLOR_BLACK, COLOR_HWHITE); + std::cout << std::endl; flowerShopClientCode(product2); + cct_setcolor(COLOR_BLACK, COLOR_HWHITE); + std::cout << std::endl; flowerShopClientCode(set1); + cct_setcolor(COLOR_BLACK, COLOR_HWHITE); + std::cout << std::endl; flowerShopClientCode(set2); + cct_setcolor(COLOR_BLACK, COLOR_HWHITE); + std::cout << std::endl; + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + delete product1; delete product2; diff --git a/Compose/Compose.h b/Compose/Compose.h index 9837786..c598613 100644 --- a/Compose/Compose.h +++ b/Compose/Compose.h @@ -2,6 +2,7 @@ #include #include #include +#include "../utils/cct_tools.h" class FlowerComponent { protected: @@ -69,6 +70,5 @@ class FlowerSet : public FlowerComponent { }; void flowerShopClientCode(FlowerComponent* component); -void pressEntertoContinue(); void testCompose(); \ No newline at end of file diff --git a/Facade/Facade.cpp b/Facade/Facade.cpp index 056909f..9c823bb 100644 --- a/Facade/Facade.cpp +++ b/Facade/Facade.cpp @@ -1,4 +1,4 @@ -#include "Facade.h" +#include "Facade.h" #include class InventorySystem; @@ -6,75 +6,61 @@ class OrderProcessingSystem; class CustomerServiceSystem; class FlowerShopFacade; -void pressEnterToContinue() { - std::cout << "س..."; - std::cin.ignore(); // û룬ȴس - std::cout << std::endl; -} - -// ʵϵͳ1 +// 实现子系统1 void InventorySystem::checkInventory() { - std::cout << "黨ܿ..." << std::endl; - pressEnterToContinue(); + std::cout << "检查花卉库存..." << std::endl; } void InventorySystem::updateInventory() { - std::cout << "»ܿ..." << std::endl; - pressEnterToContinue(); + std::cout << "更新花卉库存..." << std::endl; } -// ʵϵͳ2 +// 实现子系统2 void OrderProcessingSystem::placeOrder() { - std::cout << "µ򻨻..." << std::endl; - pressEnterToContinue(); + std::cout << "下单购买花卉..." << std::endl; } void OrderProcessingSystem::processPayment() { - std::cout << "֧..." << std::endl; - pressEnterToContinue(); + std::cout << "处理订单支付..." << std::endl; } -// ʵϵͳ3 +// 实现子系统3 void CustomerServiceSystem::provideAssistance() { - std::cout << "ṩͻ..." << std::endl; - pressEnterToContinue(); + std::cout << "提供客户服务..." << std::endl; } -// ʵֻࣨFacade +// 实现花店外观类(Facade) FlowerShopFacade::FlowerShopFacade() { - // ʼϵͳ + // 初始化子系统 } FlowerShopFacade::~FlowerShopFacade() { - // ϵͳ + // 销毁子系统 } -// ʵֹ򻨻ܵķ +// 实现购买花卉的方法 void FlowerShopFacade::purchaseFlowers() { - std::cout << "ӭٻ꣡" << std::endl; + std::cout << "欢迎光临花店!" << std::endl; InventorySystem().checkInventory(); OrderProcessingSystem().placeOrder(); OrderProcessingSystem().processPayment(); CustomerServiceSystem().provideAssistance(); - std::cout << "лǵĵ̣" << std::endl; - pressEnterToContinue(); + std::cout << "感谢您光临我们的店铺!" << std::endl; } -// Żķ +// 新增安排活动的方法 void FlowerShopFacade::arrangeEvent() { - std::cout << "ӭŷ" << std::endl; - std::cout << "ɣ" << std::endl; - pressEnterToContinue(); + std::cout << "欢迎光临花店,为顾客安排花卉装饰服务!" << std::endl; + std::cout << "花卉装饰安排完成!" << std::endl; } -// ͻķ +// 新增送花服务的方法 void FlowerShopFacade::deliverFlowers() { - std::cout << "ӭͻ" << std::endl; - std::cout << "ѳɹʹ" << std::endl; - pressEnterToContinue(); + std::cout << "欢迎光临花店,为顾客安排送花服务!" << std::endl; + std::cout << "花卉已成功送达!" << std::endl; } -// ķ +// 新增随机分配活动的方法 void FlowerShopFacade::assignRandomActivity() { std::vector activities = { &FlowerShopFacade::purchaseFlowers, @@ -82,21 +68,28 @@ void FlowerShopFacade::assignRandomActivity() { &FlowerShopFacade::deliverFlowers }; - // ѡһ - int randomIndex = rand() % activities.size(); - (this->*activities[randomIndex])(); // ѡĻ + + // 随机选择一个活动 + int randomIndex = rand() % 3; + (this->*activities[randomIndex])(); // 调用选择的活动 } void testFacade() { + srand((unsigned)time(0)); FlowerShopFacade flowerShopGYYF; + std::cout << "*~正在为顾客随机分配花店活动~*" << std::endl; + for (int i = 1; i <= 3; i++) { - std::cout << " " << i << " ˿" << std::endl; + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + std::cout << "第 " << i << " 批顾客来到花店" << std::endl; + cct_setcolor(COLOR_BLACK, COLOR_HWHITE - i); flowerShopGYYF.assignRandomActivity(); std::cout << std::endl; - pressEnterToContinue(); } -} + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + +} \ No newline at end of file diff --git a/Facade/Facade.h b/Facade/Facade.h index d1aee0c..1885de8 100644 --- a/Facade/Facade.h +++ b/Facade/Facade.h @@ -1,5 +1,6 @@ #pragma once #include +#include"../utils/cct_tools.h" // ϵͳ1: ܿ class InventorySystem { From 8241c412b447251ebb331ef2d08f5dd9358200a2 Mon Sep 17 00:00:00 2001 From: klVIllaGerG <805094070@qq.com> Date: Sat, 30 Dec 2023 22:10:19 +0800 Subject: [PATCH 4/6] gyy4 gyy4 --- Compose/Compose.cpp | 33 +++++++++++++++++---------- Compose/Compose.h | 54 +++++++++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/Compose/Compose.cpp b/Compose/Compose.cpp index 3b2bf80..b691156 100644 --- a/Compose/Compose.cpp +++ b/Compose/Compose.cpp @@ -2,38 +2,45 @@ #include #include -class FlowerComponent; - -void flowerShopClientCode(FlowerComponent* component) { +// ͻ˴룬ڴӡIJ +void flowerShopClientCode(FlowerComponentGyy* component) { + // ȡIJ string output_ = component->Operation(); + // ӡ std::istringstream ss(output_); std::string token; cout << "*Ʒ*"; static int count = 1; cct_setcolor(COLOR_BLACK, COLOR_HWHITE - (count++)); while (std::getline(ss, token, ',')) { - std::cout << token; } } +// ʾڲģʽĹ void testCompose() { - FlowerComponent* product1 = new SingleFlower("*õ廨*"); - FlowerComponent* product2 = new SingleFlower("*ܰ*"); - FlowerComponent* product3 = new SingleFlower("*㻨*"); + // һܶ + FlowerComponentGyy* product1 = new SingleFlower("*õ廨*"); + FlowerComponentGyy* product2 = new SingleFlower("*ܰ*"); + FlowerComponentGyy* product3 = new SingleFlower("*㻨*"); - FlowerComponent* set1 = new FlowerSet("*˽رװ*"); + // װ󣬲ӵһܶ + FlowerComponentGyy* set1 = new FlowerSet("*˽رװ*"); set1->Add(product1); set1->Add(product2); - - FlowerComponent* set2 = new FlowerSet("*װ*"); - set2->Add(product3); + // װ󣬲ӵһܶװ + FlowerComponentGyy* set2 = new FlowerSet("*װ*"); set2->Add(product3); + set2->Add(product3); // ͬĵһܶ set2->Add(product1); + + // ÿ̨ɫ cct_setcolor(COLOR_BLACK, COLOR_WHITE); cct_setcolor(COLOR_BLACK, COLOR_HWHITE); + + // ӡһܺͻװIJ flowerShopClientCode(product1); cct_setcolor(COLOR_BLACK, COLOR_HWHITE); std::cout << std::endl; @@ -46,9 +53,11 @@ void testCompose() { flowerShopClientCode(set2); cct_setcolor(COLOR_BLACK, COLOR_HWHITE); std::cout << std::endl; - cct_setcolor(COLOR_BLACK, COLOR_WHITE); + // ָ̨ɫ + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + // ͷŶ̬ڴ delete product1; delete product2; delete set1; diff --git a/Compose/Compose.h b/Compose/Compose.h index c598613..9ab01b0 100644 --- a/Compose/Compose.h +++ b/Compose/Compose.h @@ -4,60 +4,78 @@ #include #include "../utils/cct_tools.h" -class FlowerComponent { +// +class FlowerComponentGyy { protected: - FlowerComponent* parent_; - std::string name; + FlowerComponentGyy* parent_; // ָ + std::string name; // public: - FlowerComponent(std::string name) : name(name), parent_(nullptr) {} + // 캯 + FlowerComponentGyy(std::string name) : name(name), parent_(nullptr) {} - virtual void Add(FlowerComponent* component) {} - virtual void Remove(FlowerComponent* component) {} + // ⷽ + virtual void Add(FlowerComponentGyy* component) {} + + // Ƴⷽ + virtual void Remove(FlowerComponentGyy* component) {} + + // жǷΪ϶ⷽ virtual bool IsComposite() const { return false; } - virtual void SetParent(FlowerComponent* parent) { + // øⷽ + virtual void SetParent(FlowerComponentGyy* parent) { parent_ = parent; } + // ȡIJĴⷽ virtual std::string Operation() const = 0; }; -class SingleFlower : public FlowerComponent { +// һ̳࣬Ի +class SingleFlower : public FlowerComponentGyy { public: - SingleFlower(std::string name) : FlowerComponent(name) {} + // 캯 + SingleFlower(std::string name) : FlowerComponentGyy(name) {} + // ȡһܵIJ std::string Operation() const override { return "һ: " + name; } }; -class FlowerSet : public FlowerComponent { +// װ̳࣬Ի +class FlowerSet : public FlowerComponentGyy { private: - std::list children_; + std::list children_; // б public: - FlowerSet(std::string name) : FlowerComponent(name) {} + // 캯 + FlowerSet(std::string name) : FlowerComponentGyy(name) {} - void Add(FlowerComponent* component) override { + // ʵַ + void Add(FlowerComponentGyy* component) override { children_.push_back(component); component->SetParent(this); } - void Remove(FlowerComponent* component) override { + // Ƴʵַ + void Remove(FlowerComponentGyy* component) override { children_.remove(component); component->SetParent(nullptr); } + // жǷΪ϶ʵַ bool IsComposite() const override { return true; } + // ȡװIJʵַ std::string Operation() const override { std::string result = "װ: " + name + " "; - for (const FlowerComponent* c : children_) { + for (const FlowerComponentGyy* c : children_) { if (c == children_.back()) { result += c->Operation(); } @@ -69,6 +87,8 @@ class FlowerSet : public FlowerComponent { } }; -void flowerShopClientCode(FlowerComponent* component); +// ͻ˴룬ڴӡIJ +void flowerShopClientCode(FlowerComponentGyy* component); -void testCompose(); \ No newline at end of file +// ʾڲģʽĹ +void testCompose(); From 8da67c5262903b14997c9827a36b73243fe4588d Mon Sep 17 00:00:00 2001 From: klVIllaGerG <805094070@qq.com> Date: Mon, 1 Jan 2024 16:44:02 +0800 Subject: [PATCH 5/6] gyy5 gyy5 --- RAII/RAII.cpp | 17 ----------------- RAII/RAII.h | 34 ---------------------------------- 2 files changed, 51 deletions(-) delete mode 100644 RAII/RAII.cpp delete mode 100644 RAII/RAII.h diff --git a/RAII/RAII.cpp b/RAII/RAII.cpp deleted file mode 100644 index ce448d8..0000000 --- a/RAII/RAII.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "RAII.h" - -void ResourceManagement::acquireResources() { - std::cout << "ȡܡװϵԴ..." << std::endl; -} - -void ResourceManagement::releaseResources() { - std::cout << "ͷŻܡװϵԴ..." << std::endl; -} - -void FlowerShopGYYR::sellFlowers() { - std::cout << "..." << std::endl; -} - -void FlowerShopGYYR::arrangeEvent() { - std::cout << "ڰŻ..." << std::endl; -} \ No newline at end of file diff --git a/RAII/RAII.h b/RAII/RAII.h deleted file mode 100644 index da62ef7..0000000 --- a/RAII/RAII.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once -#include -#include - -class ResourceManagement { -public: - ResourceManagement() { - acquireResources(); - std::cout << "Դ׼" << std::endl; - } - - ~ResourceManagement() { - releaseResources(); - std::cout << "Դɣ" << std::endl; - } - - void acquireResources(); - - void releaseResources(); -}; - -class FlowerShopGYYR { -public: - FlowerShopGYYR() { - std::cout << "ӭٻ꣡" << std::endl; - } - - void sellFlowers(); - void arrangeEvent(); - - ~FlowerShopGYYR() { - std::cout << "лĹ٣" << std::endl; - } -}; \ No newline at end of file From 6ea0c42fdd611c821dc9874796de20adaf6efd87 Mon Sep 17 00:00:00 2001 From: klVIllaGerG <805094070@qq.com> Date: Mon, 1 Jan 2024 17:58:10 +0800 Subject: [PATCH 6/6] gyy6 gyy6 --- Compose/Compose.cpp | 100 +++++++++++++++++++++++++++++++------------- Compose/Compose.h | 54 ++++++++---------------- Facade/Facade.cpp | 2 +- 3 files changed, 90 insertions(+), 66 deletions(-) diff --git a/Compose/Compose.cpp b/Compose/Compose.cpp index b691156..01adfd4 100644 --- a/Compose/Compose.cpp +++ b/Compose/Compose.cpp @@ -1,46 +1,78 @@ #include "Compose.h" -#include -#include +#include +#include +#include +#include -// ͻ˴룬ڴӡIJ -void flowerShopClientCode(FlowerComponentGyy* component) { - // ȡIJ +class FlowerComponent; + +void flowerShopClientCode(FlowerComponent* component) { string output_ = component->Operation(); - // ӡ std::istringstream ss(output_); std::string token; cout << "*Ʒ*"; static int count = 1; - cct_setcolor(COLOR_BLACK, COLOR_HWHITE - (count++)); - while (std::getline(ss, token, ',')) { + cct_setcolor(COLOR_BLACK, COLOR_HWHITE-(count++)); + while (std::getline(ss,token,',')) { + std::cout << token; } + if (count == 15) + count = 0; } -// ʾڲģʽĹ -void testCompose() { - // һܶ - FlowerComponentGyy* product1 = new SingleFlower("*õ廨*"); - FlowerComponentGyy* product2 = new SingleFlower("*ܰ*"); - FlowerComponentGyy* product3 = new SingleFlower("*㻨*"); +FlowerComponent* chooseFlowerBouquet() { + // ṩѡ + std::cout << "װ(0˳)" << std::endl; + std::cout << "1. õ廨 2. ܰ 3. 㻨" << std::endl; + std::cout << "سϣ" ; + + std::string userInput; + std::getline(std::cin, userInput); + + if (userInput == "0") { + return nullptr; // ûѡ˳ؿָ + } + + FlowerSet* userBouquet = new FlowerSet("*ûװ*"); + + for (char c : userInput) { + switch (c) { + case '1': + userBouquet->Add(new SingleFlower("*õ廨*")); + break; + case '2': + userBouquet->Add(new SingleFlower("*ܰ*")); + break; + case '3': + userBouquet->Add(new SingleFlower("*㻨*")); + break; + default: + std::cerr << "Чѡ" << c << std::endl; + delete userBouquet; + return nullptr; // ûЧѡؿָ + } + } + + return userBouquet; +} - // װ󣬲ӵһܶ - FlowerComponentGyy* set1 = new FlowerSet("*˽رװ*"); + + +void testCompose() { + FlowerComponent* product1 = new SingleFlower("*õ廨*"); + FlowerComponent* product2 = new SingleFlower("*ܰ*"); + FlowerComponent* product3 = new SingleFlower("*㻨*"); + FlowerComponent* set1 = new FlowerSet("*˽رװ*"); set1->Add(product1); set1->Add(product2); - - // װ󣬲ӵһܶװ - FlowerComponentGyy* set2 = new FlowerSet("*װ*"); + FlowerComponent* set2 = new FlowerSet("*װ*"); + set2->Add(product3); set2->Add(product3); - set2->Add(product3); // ͬĵһܶ set2->Add(product1); - - // ÿ̨ɫ cct_setcolor(COLOR_BLACK, COLOR_WHITE); cct_setcolor(COLOR_BLACK, COLOR_HWHITE); - - // ӡһܺͻװIJ flowerShopClientCode(product1); cct_setcolor(COLOR_BLACK, COLOR_HWHITE); std::cout << std::endl; @@ -53,14 +85,26 @@ void testCompose() { flowerShopClientCode(set2); cct_setcolor(COLOR_BLACK, COLOR_HWHITE); std::cout << std::endl; - - // ָ̨ɫ cct_setcolor(COLOR_BLACK, COLOR_WHITE); - - // ͷŶ̬ڴ delete product1; delete product2; delete set1; delete product3; delete set2; + + // ѡû + while (1) { + FlowerComponent* userBouquet = chooseFlowerBouquet(); + if (userBouquet == nullptr) + break; + if (userBouquet) { + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + cct_setcolor(COLOR_BLACK, COLOR_HWHITE); + flowerShopClientCode(userBouquet); + cct_setcolor(COLOR_BLACK, COLOR_HWHITE); + std::cout << std::endl; + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + delete userBouquet; + } + } } diff --git a/Compose/Compose.h b/Compose/Compose.h index 9ab01b0..c598613 100644 --- a/Compose/Compose.h +++ b/Compose/Compose.h @@ -4,78 +4,60 @@ #include #include "../utils/cct_tools.h" -// -class FlowerComponentGyy { +class FlowerComponent { protected: - FlowerComponentGyy* parent_; // ָ - std::string name; // + FlowerComponent* parent_; + std::string name; public: - // 캯 - FlowerComponentGyy(std::string name) : name(name), parent_(nullptr) {} + FlowerComponent(std::string name) : name(name), parent_(nullptr) {} - // ⷽ - virtual void Add(FlowerComponentGyy* component) {} - - // Ƴⷽ - virtual void Remove(FlowerComponentGyy* component) {} - - // жǷΪ϶ⷽ + virtual void Add(FlowerComponent* component) {} + virtual void Remove(FlowerComponent* component) {} virtual bool IsComposite() const { return false; } - // øⷽ - virtual void SetParent(FlowerComponentGyy* parent) { + virtual void SetParent(FlowerComponent* parent) { parent_ = parent; } - // ȡIJĴⷽ virtual std::string Operation() const = 0; }; -// һ̳࣬Ի -class SingleFlower : public FlowerComponentGyy { +class SingleFlower : public FlowerComponent { public: - // 캯 - SingleFlower(std::string name) : FlowerComponentGyy(name) {} + SingleFlower(std::string name) : FlowerComponent(name) {} - // ȡһܵIJ std::string Operation() const override { return "һ: " + name; } }; -// װ̳࣬Ի -class FlowerSet : public FlowerComponentGyy { +class FlowerSet : public FlowerComponent { private: - std::list children_; // б + std::list children_; public: - // 캯 - FlowerSet(std::string name) : FlowerComponentGyy(name) {} + FlowerSet(std::string name) : FlowerComponent(name) {} - // ʵַ - void Add(FlowerComponentGyy* component) override { + void Add(FlowerComponent* component) override { children_.push_back(component); component->SetParent(this); } - // Ƴʵַ - void Remove(FlowerComponentGyy* component) override { + void Remove(FlowerComponent* component) override { children_.remove(component); component->SetParent(nullptr); } - // жǷΪ϶ʵַ bool IsComposite() const override { return true; } - // ȡװIJʵַ std::string Operation() const override { std::string result = "װ: " + name + " "; - for (const FlowerComponentGyy* c : children_) { + for (const FlowerComponent* c : children_) { if (c == children_.back()) { result += c->Operation(); } @@ -87,8 +69,6 @@ class FlowerSet : public FlowerComponentGyy { } }; -// ͻ˴룬ڴӡIJ -void flowerShopClientCode(FlowerComponentGyy* component); +void flowerShopClientCode(FlowerComponent* component); -// ʾڲģʽĹ -void testCompose(); +void testCompose(); \ No newline at end of file diff --git a/Facade/Facade.cpp b/Facade/Facade.cpp index 9c823bb..3fcfedf 100644 --- a/Facade/Facade.cpp +++ b/Facade/Facade.cpp @@ -92,4 +92,4 @@ void testFacade() { cct_setcolor(COLOR_BLACK, COLOR_WHITE); -} \ No newline at end of file +}