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..01adfd4 --- /dev/null +++ b/Compose/Compose.cpp @@ -0,0 +1,110 @@ +#include "Compose.h" +#include +#include +#include +#include + +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,',')) { + + std::cout << token; + } + if (count == 15) + count = 0; +} + +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; +} + + + +void testCompose() { + FlowerComponent* product1 = new SingleFlower("*õ廨*"); + FlowerComponent* product2 = new SingleFlower("*ܰ*"); + FlowerComponent* product3 = new SingleFlower("*㻨*"); + FlowerComponent* set1 = new FlowerSet("*˽رװ*"); + set1->Add(product1); + set1->Add(product2); + 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; + 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 new file mode 100644 index 0000000..c598613 --- /dev/null +++ b/Compose/Compose.h @@ -0,0 +1,74 @@ +#pragma once +#include +#include +#include +#include "../utils/cct_tools.h" + +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 testCompose(); \ No newline at end of file diff --git a/Facade/Facade.cpp b/Facade/Facade.cpp new file mode 100644 index 0000000..3fcfedf --- /dev/null +++ b/Facade/Facade.cpp @@ -0,0 +1,95 @@ +#include "Facade.h" +#include + +class InventorySystem; +class OrderProcessingSystem; +class CustomerServiceSystem; +class FlowerShopFacade; + +// 实现子系统1 +void InventorySystem::checkInventory() { + std::cout << "检查花卉库存..." << std::endl; +} + +void InventorySystem::updateInventory() { + std::cout << "更新花卉库存..." << std::endl; +} + +// 实现子系统2 +void OrderProcessingSystem::placeOrder() { + std::cout << "下单购买花卉..." << std::endl; +} + +void OrderProcessingSystem::processPayment() { + std::cout << "处理订单支付..." << std::endl; +} + +// 实现子系统3 +void CustomerServiceSystem::provideAssistance() { + std::cout << "提供客户服务..." << std::endl; +} + +// 实现花店外观类(Facade) +FlowerShopFacade::FlowerShopFacade() { + // 初始化子系统 +} + +FlowerShopFacade::~FlowerShopFacade() { + // 销毁子系统 +} + +// 实现购买花卉的方法 +void FlowerShopFacade::purchaseFlowers() { + std::cout << "欢迎光临花店!" << std::endl; + InventorySystem().checkInventory(); + OrderProcessingSystem().placeOrder(); + OrderProcessingSystem().processPayment(); + CustomerServiceSystem().provideAssistance(); + std::cout << "感谢您光临我们的店铺!" << std::endl; +} + +// 新增安排活动的方法 +void FlowerShopFacade::arrangeEvent() { + std::cout << "欢迎光临花店,为顾客安排花卉装饰服务!" << std::endl; + std::cout << "花卉装饰安排完成!" << std::endl; +} + +// 新增送花服务的方法 +void FlowerShopFacade::deliverFlowers() { + std::cout << "欢迎光临花店,为顾客安排送花服务!" << std::endl; + std::cout << "花卉已成功送达!" << std::endl; +} + +// 新增随机分配活动的方法 +void FlowerShopFacade::assignRandomActivity() { + std::vector activities = { + &FlowerShopFacade::purchaseFlowers, + &FlowerShopFacade::arrangeEvent, + &FlowerShopFacade::deliverFlowers + }; + + + // 随机选择一个活动 + 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++) { + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + std::cout << "第 " << i << " 批顾客来到花店" << std::endl; + cct_setcolor(COLOR_BLACK, COLOR_HWHITE - i); + flowerShopGYYF.assignRandomActivity(); + std::cout << std::endl; + } + + cct_setcolor(COLOR_BLACK, COLOR_WHITE); + +} diff --git a/Facade/Facade.h b/Facade/Facade.h new file mode 100644 index 0000000..1885de8 --- /dev/null +++ b/Facade/Facade.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include"../utils/cct_tools.h" + +// ϵͳ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(); // +}; + +void testFacade(); \ 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/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