From b43e32c98b9d0ef09f23ebd3cd8779c7d9f40297 Mon Sep 17 00:00:00 2001 From: Rekord <1324596506@qq.com> Date: Tue, 12 Nov 2024 02:23:39 +0000 Subject: [PATCH] chore: supply main function --- abstract-factory/AbstractFactory.cpp | 6 ++++++ decorator/Decorator.cpp | 5 ++--- flyweight/Flyweight.cpp | 2 ++ strategy/Strategy.cpp | 10 ++++++++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/abstract-factory/AbstractFactory.cpp b/abstract-factory/AbstractFactory.cpp index 98a3a78..d7cec49 100644 --- a/abstract-factory/AbstractFactory.cpp +++ b/abstract-factory/AbstractFactory.cpp @@ -156,6 +156,12 @@ int main() ProductA *p2 = factoryY->createProductA(); std::cout << "Product: " << p2->getName() << std::endl; + ProductB *p3 = factoryX->createProductB(); + std::cout << "Product: " << p3->getName() << std::endl; + + ProductB *p4 = factoryY->createProductB(); + std::cout << "Product: " << p4->getName() << std::endl; + delete p1; delete p2; diff --git a/decorator/Decorator.cpp b/decorator/Decorator.cpp index c192d6e..a67cfe9 100644 --- a/decorator/Decorator.cpp +++ b/decorator/Decorator.cpp @@ -99,12 +99,11 @@ int main() { ConcreteComponent *cc = new ConcreteComponent(); ConcreteDecoratorB *db = new ConcreteDecoratorB( cc ); - ConcreteDecoratorA *da = new ConcreteDecoratorA( db ); + Component *component = new ConcreteDecoratorA( db ); - Component *component = da; component->operation(); - delete da; + delete component; delete db; delete cc; diff --git a/flyweight/Flyweight.cpp b/flyweight/Flyweight.cpp index f00ac4d..00d2078 100644 --- a/flyweight/Flyweight.cpp +++ b/flyweight/Flyweight.cpp @@ -111,6 +111,8 @@ int main() FlyweightFactory *factory = new FlyweightFactory; factory->getFlyweight(1)->operation(); factory->getFlyweight(2)->operation(); + // already exist in pool + factory->getFlyweight(1)->operation(); delete factory; return 0; } diff --git a/strategy/Strategy.cpp b/strategy/Strategy.cpp index def778f..bf6feca 100644 --- a/strategy/Strategy.cpp +++ b/strategy/Strategy.cpp @@ -90,8 +90,14 @@ class Context int main() { - Context context( new ConcreteStrategyA() ); - context.contextInterface(); + Context context1( new ConcreteStrategyA() ); + context1.contextInterface(); + Context context2( new ConcreteStrategyB() ); + context2.contextInterface(); + + Context context3( new ConcreteStrategyB() ); + context3.contextInterface(); + return 0; }