From 5b2135202895be722a8f508ac9b5e5b8b949cb8c Mon Sep 17 00:00:00 2001 From: FengJungle Date: Sun, 4 Apr 2021 21:09:08 +0800 Subject: [PATCH] fix vitual base classes issues: add virtual destruct functions. --- 01.SimpleFactory/2.Code/SimpleFactory.h | 17 ++++++++++++++-- 02.FactoryMethod/2.Code/FactoryMethod.h | 6 ++++-- 03.AbstractFactory/2.Code/AbstractFactory.h | 7 +++++-- 04.BuilderPattern/2.Code/BuilderPattern.h | 20 +++++++++---------- 05.PrototypePattern/2.Code/PrototypePattern.h | 1 + 07.AdapterPattern/2.Code/AdapterPattern.h | 1 + 08.BridgePattern/2.Code/BridgePattern.h | 2 ++ 09.CompositePattern/2.Code/CompositePattern.h | 1 + 10.DecoratorPattern/2.Code/DecoratorPattern.h | 1 + 12.FlyweightPattern/2.Code/FlyweightPattern.h | 1 + 13.ProxyPattern/2.Code/ProxyPattern.h | 2 ++ .../2.Code/ChainOfResponsibility.h | 1 + 15.CommandPattern/2.Code/CommandPattern.h | 1 + .../2.Code/InterpreterPattern.h | 1 + 17.IteratorPattern/2.Code/Aggregate.cpp | 2 +- 17.IteratorPattern/2.Code/Aggregate.h | 1 + 18.MediatorPattern/2.Code/Colleague.h | 1 + 18.MediatorPattern/2.Code/Mediator.h | 1 + 20.ObserverPattern/2.Code/AllyCenter.h | 1 + 20.ObserverPattern/2.Code/Demo.h | 2 ++ 20.ObserverPattern/2.Code/Observer.h | 1 + 21.StatePattern/2.Code/Demo.h | 1 + 21.StatePattern/2.Code/Level.h | 1 + 22.StrategyPattern/2.Code/Strategy.h | 1 + 23.TemplateMethodPattern/2.Code/Demo.h | 1 + .../2.Code/FingerprintModule.h | 1 + 24.VisitorPattern/2.Code/Demo.h | 2 ++ 24.VisitorPattern/2.Code/Element.h | 1 + 24.VisitorPattern/2.Code/Visitor.h | 1 + 29 files changed, 63 insertions(+), 17 deletions(-) diff --git a/01.SimpleFactory/2.Code/SimpleFactory.h b/01.SimpleFactory/2.Code/SimpleFactory.h index bdb48eb..94b7aa8 100644 --- a/01.SimpleFactory/2.Code/SimpleFactory.h +++ b/01.SimpleFactory/2.Code/SimpleFactory.h @@ -12,9 +12,10 @@ public: AbstractSportProduct(){ } + virtual ~AbstractSportProduct(){} //抽象方法: - void printName(){}; - void play(){}; + virtual void printName(){}; + virtual void play(){}; }; //具体产品类Basketball @@ -24,6 +25,10 @@ public: Basketball(){ printName(); play(); + } + ~Basketball() + { + } //具体实现方法 void printName(){ @@ -41,6 +46,10 @@ public: Football(){ printName(); play(); + } + ~Football() + { + } //具体实现方法 void printName(){ @@ -58,6 +67,10 @@ public: Volleyball(){ printName(); play(); + } + ~Volleyball() + { + } //具体实现方法 void printName(){ diff --git a/02.FactoryMethod/2.Code/FactoryMethod.h b/02.FactoryMethod/2.Code/FactoryMethod.h index df1231b..9f66ed2 100644 --- a/02.FactoryMethod/2.Code/FactoryMethod.h +++ b/02.FactoryMethod/2.Code/FactoryMethod.h @@ -12,9 +12,10 @@ public: AbstractSportProduct(){ } + virtual ~AbstractSportProduct(){} //抽象方法: - void printName(){}; - void play(){}; + virtual void printName(){}; + virtual void play(){}; }; //具体产品类Basketball @@ -72,6 +73,7 @@ public: class AbstractFactory { public: + virtual ~AbstractFactory(){} virtual AbstractSportProduct *getSportProduct() = 0; }; diff --git a/03.AbstractFactory/2.Code/AbstractFactory.h b/03.AbstractFactory/2.Code/AbstractFactory.h index e7c8f16..6caf8f4 100644 --- a/03.AbstractFactory/2.Code/AbstractFactory.h +++ b/03.AbstractFactory/2.Code/AbstractFactory.h @@ -12,8 +12,9 @@ public: AbstractBall(){ } + virtual ~AbstractBall(){} //抽象方法: - void play(){}; + virtual void play(){}; }; //具体产品类Basketball @@ -47,8 +48,9 @@ class AbstractShirt { public: AbstractShirt(){} + virtual ~AbstractShirt(){} //抽象方法: - void wearShirt(){}; + virtual void wearShirt(){}; }; //具体产品类BasketballShirt @@ -81,6 +83,7 @@ public: class AbstractFactory { public: + virtual ~AbstractFactory() {} virtual AbstractBall *getBall() = 0; virtual AbstractShirt *getShirt() = 0; }; diff --git a/04.BuilderPattern/2.Code/BuilderPattern.h b/04.BuilderPattern/2.Code/BuilderPattern.h index 198dc34..df5a1ed 100644 --- a/04.BuilderPattern/2.Code/BuilderPattern.h +++ b/04.BuilderPattern/2.Code/BuilderPattern.h @@ -5,7 +5,7 @@ #include using namespace std; -//产品类House +//锟斤拷品锟斤拷House class House { public: @@ -19,7 +19,7 @@ public: void setRoof(string iRoof) { this->roof = iRoof; } - //打印House信息 + //锟斤拷印House锟斤拷息 void printfHouseInfo() { printf("Floor:%s\t\n", this->floor.c_str()); printf("Wall:%s\t\n", this->wall.c_str()); @@ -31,7 +31,7 @@ private: string roof; }; -//抽象建造者AbstractBall +//锟斤拷锟斤拷锟斤拷锟斤拷AbstractBall class AbstractBuilder { public: @@ -46,7 +46,7 @@ public: house = nullptr; } } - //抽象方法: + //锟斤拷锟襟方凤拷锟斤拷 virtual void buildFloor() = 0; virtual void buildWall() = 0; virtual void buildRoof() = 0; @@ -55,7 +55,7 @@ public: House *house; }; -//具体建造者ConcreteBuilderA +//锟斤拷锟藉建锟斤拷锟斤拷ConcreteBuilderA class ConcreteBuilderA :public AbstractBuilder { public: @@ -70,7 +70,7 @@ public: house = nullptr; } } - //具体实现方法 + //锟斤拷锟斤拷实锟街凤拷锟斤拷 void buildFloor() { this->house->setFloor("Floor_A"); } @@ -85,7 +85,7 @@ public: } }; -//具体建造者ConcreteBuilderB +//锟斤拷锟藉建锟斤拷锟斤拷ConcreteBuilderB class ConcreteBuilderB :public AbstractBuilder { public: @@ -100,7 +100,7 @@ public: house = nullptr; } } - //具体实现方法 + //锟斤拷锟斤拷实锟街凤拷锟斤拷 void buildFloor() { this->house->setFloor("Floor_B"); } @@ -115,7 +115,7 @@ public: } }; -//指挥者Director +//指锟斤拷锟斤拷Director class Director { public: @@ -128,7 +128,7 @@ public: builder = nullptr; } } - //具体实现方法 + //锟斤拷锟斤拷实锟街凤拷锟斤拷 void setBuilder(AbstractBuilder *iBuilder) { this->builder = iBuilder; } diff --git a/05.PrototypePattern/2.Code/PrototypePattern.h b/05.PrototypePattern/2.Code/PrototypePattern.h index f9adbc1..cea6d10 100644 --- a/05.PrototypePattern/2.Code/PrototypePattern.h +++ b/05.PrototypePattern/2.Code/PrototypePattern.h @@ -20,6 +20,7 @@ class PrototypeWork { public: PrototypeWork(){} + virtual ~PrototypeWork(){} virtual PrototypeWork *clone() = 0; private: diff --git a/07.AdapterPattern/2.Code/AdapterPattern.h b/07.AdapterPattern/2.Code/AdapterPattern.h index 4319ade..d7d2f31 100644 --- a/07.AdapterPattern/2.Code/AdapterPattern.h +++ b/07.AdapterPattern/2.Code/AdapterPattern.h @@ -11,6 +11,7 @@ class Controller { public: Controller(){} + virtual ~Controller(){} virtual void pathPlanning() = 0; private: }; diff --git a/08.BridgePattern/2.Code/BridgePattern.h b/08.BridgePattern/2.Code/BridgePattern.h index 0ed6871..41787be 100644 --- a/08.BridgePattern/2.Code/BridgePattern.h +++ b/08.BridgePattern/2.Code/BridgePattern.h @@ -11,6 +11,7 @@ class Game { public: Game(){} + virtual ~Game(){} virtual void play() = 0; private: }; @@ -41,6 +42,7 @@ class Phone public: Phone(){ } + virtual ~Phone(){} //安装游戏 virtual void setupGame(Game *igame) = 0; virtual void play() = 0; diff --git a/09.CompositePattern/2.Code/CompositePattern.h b/09.CompositePattern/2.Code/CompositePattern.h index 9834c2e..d3ce818 100644 --- a/09.CompositePattern/2.Code/CompositePattern.h +++ b/09.CompositePattern/2.Code/CompositePattern.h @@ -14,6 +14,7 @@ public: Component(string iName){ this->name = iName; } + virtual ~Component(){} //增加一个部门或办公室 virtual void add(Component*) = 0; //撤销一个部门或办公室 diff --git a/10.DecoratorPattern/2.Code/DecoratorPattern.h b/10.DecoratorPattern/2.Code/DecoratorPattern.h index 861463b..e3bb945 100644 --- a/10.DecoratorPattern/2.Code/DecoratorPattern.h +++ b/10.DecoratorPattern/2.Code/DecoratorPattern.h @@ -6,6 +6,7 @@ class Component { public: Component(){} + virtual ~Component(){} virtual void operation() = 0; }; diff --git a/12.FlyweightPattern/2.Code/FlyweightPattern.h b/12.FlyweightPattern/2.Code/FlyweightPattern.h index 0cd4001..35280f7 100644 --- a/12.FlyweightPattern/2.Code/FlyweightPattern.h +++ b/12.FlyweightPattern/2.Code/FlyweightPattern.h @@ -10,6 +10,7 @@ class NetDevice { public: NetDevice(){} + virtual ~NetDevice(){} virtual string getName() = 0; /*void print(){ diff --git a/13.ProxyPattern/2.Code/ProxyPattern.h b/13.ProxyPattern/2.Code/ProxyPattern.h index b2e05b2..4102dcc 100644 --- a/13.ProxyPattern/2.Code/ProxyPattern.h +++ b/13.ProxyPattern/2.Code/ProxyPattern.h @@ -10,6 +10,7 @@ class Subject { public: Subject(){} + virtual ~Subject(){} virtual void method() = 0; }; @@ -18,6 +19,7 @@ class RealSubject :public Subject { public: RealSubject(){} + virtual ~RealSubject(){} void method(){ printf("调用业务方法\n"); } diff --git a/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h b/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h index 99978a0..02812b2 100644 --- a/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h +++ b/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h @@ -36,6 +36,7 @@ public: Approver(string iName){ setName(iName); } + virtual ~Approver(){} // 添加上级 void setSuperior(Approver *iSuperior){ this->superior = iSuperior; diff --git a/15.CommandPattern/2.Code/CommandPattern.h b/15.CommandPattern/2.Code/CommandPattern.h index b0fbe50..5e2b24c 100644 --- a/15.CommandPattern/2.Code/CommandPattern.h +++ b/15.CommandPattern/2.Code/CommandPattern.h @@ -13,6 +13,7 @@ class Command { public: Command(){} + virtual ~Command(){} // 声明抽象接口:发送命令 virtual void execute() = 0; private: diff --git a/16.InterpreterPattern/2.Code/InterpreterPattern.h b/16.InterpreterPattern/2.Code/InterpreterPattern.h index 744b8d7..7d78b52 100644 --- a/16.InterpreterPattern/2.Code/InterpreterPattern.h +++ b/16.InterpreterPattern/2.Code/InterpreterPattern.h @@ -9,6 +9,7 @@ class AbstractNode { public: AbstractNode(){} + virtual ~AbstractNode(){} // 声明抽象接口 virtual char interpret() = 0; }; diff --git a/17.IteratorPattern/2.Code/Aggregate.cpp b/17.IteratorPattern/2.Code/Aggregate.cpp index 4fc5702..83eafa8 100644 --- a/17.IteratorPattern/2.Code/Aggregate.cpp +++ b/17.IteratorPattern/2.Code/Aggregate.cpp @@ -17,5 +17,5 @@ int Television::getTotalChannelNum(){ } void Television::play(int i){ - printf("现在播放:%s……\n", channelList[i].c_str()); + printf("锟斤拷锟节诧拷锟脚o拷%s锟斤拷锟斤拷\n", channelList[i].c_str()); } \ No newline at end of file diff --git a/17.IteratorPattern/2.Code/Aggregate.h b/17.IteratorPattern/2.Code/Aggregate.h index ac3f81f..5df2f5a 100644 --- a/17.IteratorPattern/2.Code/Aggregate.h +++ b/17.IteratorPattern/2.Code/Aggregate.h @@ -13,6 +13,7 @@ class Aggregate { public: Aggregate(){} + virtual ~Aggregate(){} virtual Iterator* createIterator() = 0; }; diff --git a/18.MediatorPattern/2.Code/Colleague.h b/18.MediatorPattern/2.Code/Colleague.h index cfe231e..bd104e1 100644 --- a/18.MediatorPattern/2.Code/Colleague.h +++ b/18.MediatorPattern/2.Code/Colleague.h @@ -13,6 +13,7 @@ class Colleague { public: Colleague(){} + virtual ~Colleague(){} void setMediator(Mediator* iMediator){ this->mediator = iMediator; } diff --git a/18.MediatorPattern/2.Code/Mediator.h b/18.MediatorPattern/2.Code/Mediator.h index 05c1461..9ef0365 100644 --- a/18.MediatorPattern/2.Code/Mediator.h +++ b/18.MediatorPattern/2.Code/Mediator.h @@ -9,6 +9,7 @@ class Mediator { public: Mediator(){} + virtual ~Mediator(){} // 声明抽象方法 virtual void operation(Colleague*) = 0; // 声明注册方法 diff --git a/20.ObserverPattern/2.Code/AllyCenter.h b/20.ObserverPattern/2.Code/AllyCenter.h index 12194b3..9b940a7 100644 --- a/20.ObserverPattern/2.Code/AllyCenter.h +++ b/20.ObserverPattern/2.Code/AllyCenter.h @@ -13,6 +13,7 @@ class AllyCenter { public: AllyCenter(); + virtual ~AllyCenter(){} // 声明通知方法 virtual void notify(INFO_TYPE infoType, std::string name) = 0; // 加入玩家 diff --git a/20.ObserverPattern/2.Code/Demo.h b/20.ObserverPattern/2.Code/Demo.h index 2fe284f..62b9bd6 100644 --- a/20.ObserverPattern/2.Code/Demo.h +++ b/20.ObserverPattern/2.Code/Demo.h @@ -8,6 +8,7 @@ using namespace std; class Observer { public: + virtual ~Observer() {} // 声明响应更新方法 virtual void update() = 0; }; @@ -26,6 +27,7 @@ public: class Subject { public: + virtual ~Subject() {} // 添加观察者 void attach(Observer* obs){ obsList.push_back(obs); diff --git a/20.ObserverPattern/2.Code/Observer.h b/20.ObserverPattern/2.Code/Observer.h index 0c7e4bf..e06b8d0 100644 --- a/20.ObserverPattern/2.Code/Observer.h +++ b/20.ObserverPattern/2.Code/Observer.h @@ -10,6 +10,7 @@ using namespace std; class Observer { public: + virtual ~Observer(){} Observer(){} // 声明抽象方法 virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0; diff --git a/21.StatePattern/2.Code/Demo.h b/21.StatePattern/2.Code/Demo.h index 2280374..5c982bb 100644 --- a/21.StatePattern/2.Code/Demo.h +++ b/21.StatePattern/2.Code/Demo.h @@ -5,6 +5,7 @@ class State { public: + virtual ~State(){} // 声明抽象方法 virtual void handle() = 0; }; diff --git a/21.StatePattern/2.Code/Level.h b/21.StatePattern/2.Code/Level.h index a3ea573..7eff13d 100644 --- a/21.StatePattern/2.Code/Level.h +++ b/21.StatePattern/2.Code/Level.h @@ -7,6 +7,7 @@ class Level { public : Level(); + virtual ~Level(){} // 声明方法 void playCard(); void play(); diff --git a/22.StrategyPattern/2.Code/Strategy.h b/22.StrategyPattern/2.Code/Strategy.h index c610fd4..38d4974 100644 --- a/22.StrategyPattern/2.Code/Strategy.h +++ b/22.StrategyPattern/2.Code/Strategy.h @@ -8,6 +8,7 @@ class Strategy { public: Strategy(){} + virtual ~Strategy(){} virtual void sort(int arr[], int N) = 0; }; diff --git a/23.TemplateMethodPattern/2.Code/Demo.h b/23.TemplateMethodPattern/2.Code/Demo.h index 83fe157..78c6956 100644 --- a/23.TemplateMethodPattern/2.Code/Demo.h +++ b/23.TemplateMethodPattern/2.Code/Demo.h @@ -5,6 +5,7 @@ class AbstractClass { public: + virtual ~AbstractClass(){} // 模板方法,定义一个算法的框架流程 void templateMethod(){ // do something diff --git a/23.TemplateMethodPattern/2.Code/FingerprintModule.h b/23.TemplateMethodPattern/2.Code/FingerprintModule.h index 1858827..1b66419 100644 --- a/23.TemplateMethodPattern/2.Code/FingerprintModule.h +++ b/23.TemplateMethodPattern/2.Code/FingerprintModule.h @@ -8,6 +8,7 @@ class FingerprintModule { public: FingerprintModule(){} + virtual ~FingerprintModule(){} void getImage(){ printf("采指纹图像\n"); } diff --git a/24.VisitorPattern/2.Code/Demo.h b/24.VisitorPattern/2.Code/Demo.h index 39ba6e3..6791bf7 100644 --- a/24.VisitorPattern/2.Code/Demo.h +++ b/24.VisitorPattern/2.Code/Demo.h @@ -5,6 +5,7 @@ class Visitor { public: + virtual ~Visitor() {} virtual void visit(ConcreteElementA*) = 0; virtual void visit(ConcreteElementB*) = 0; }; @@ -26,6 +27,7 @@ public: class Element { public: + virtual ~Element() {} // 声明抽象方法,以一个抽象访问者的指针作为函数参数 virtual void accept(Visitor*) = 0; }; diff --git a/24.VisitorPattern/2.Code/Element.h b/24.VisitorPattern/2.Code/Element.h index 9f17d97..38ed092 100644 --- a/24.VisitorPattern/2.Code/Element.h +++ b/24.VisitorPattern/2.Code/Element.h @@ -10,6 +10,7 @@ class Element { public: Element(){}; + virtual ~Element(){} virtual void accept(Visitor*) = 0; void setPrice(int iPrice){ this->price = iPrice; diff --git a/24.VisitorPattern/2.Code/Visitor.h b/24.VisitorPattern/2.Code/Visitor.h index a9c5fe7..98f4b7f 100644 --- a/24.VisitorPattern/2.Code/Visitor.h +++ b/24.VisitorPattern/2.Code/Visitor.h @@ -14,6 +14,7 @@ class Visitor { public: Visitor(){}; + virtual ~Visitor(){} // 声明一组访问方法 virtual void visit(Apple*) = 0; virtual void visit(Book*) = 0;