From fda85a81360a683c855c62c44fa36b41f07e84b0 Mon Sep 17 00:00:00 2001 From: FengJungle Date: Thu, 14 Oct 2021 07:54:58 +0800 Subject: [PATCH] bugfix: fix memory leak issue for ObserverPattern --- 20.ObserverPattern/2.Code/AllyCenter.cpp | 16 ++++++++-------- 20.ObserverPattern/2.Code/Demo.h | 3 +++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/20.ObserverPattern/2.Code/AllyCenter.cpp b/20.ObserverPattern/2.Code/AllyCenter.cpp index 4ce2978..8083c5c 100644 --- a/20.ObserverPattern/2.Code/AllyCenter.cpp +++ b/20.ObserverPattern/2.Code/AllyCenter.cpp @@ -3,24 +3,24 @@ /*********** AllyCenter ****************/ AllyCenter::AllyCenter(){ - printf("󼪴Լ!\n"); + printf("大吉大利,今晚吃鸡!\n"); } -// +// 加入玩家 void AllyCenter::join(Observer* player){ if (playerList.size() == 4){ - printf("\n"); + printf("玩家已满!\n"); return; } - printf(" %s \n", player->getName().c_str()); + printf("玩家 %s 加入\n", player->getName().c_str()); playerList.push_back(player); if (playerList.size() == 4){ - printf("ӳɹҪˣһϣ\n"); + printf("组队成功,不要怂,一起上!\n"); } } -// Ƴ +// 移除玩家 void AllyCenter::remove(Observer* player){ - printf(" %s ˳\n", player->getName().c_str()); + printf("玩家 %s 退出\n", player->getName().c_str()); //playerList.remove(player); } /*********** AllyCenter ****************/ @@ -31,7 +31,7 @@ AllyCenterController::AllyCenterController(){ } -// ʵ֪ͨ +// 实现通知方法 void AllyCenterController::notify(INFO_TYPE infoType, std::string name){ switch (infoType){ case RESOURCE: diff --git a/20.ObserverPattern/2.Code/Demo.h b/20.ObserverPattern/2.Code/Demo.h index 62b9bd6..9b3cda2 100644 --- a/20.ObserverPattern/2.Code/Demo.h +++ b/20.ObserverPattern/2.Code/Demo.h @@ -64,6 +64,9 @@ int main() Observer *obs = new ConcreteObserver(); sub->attach(obs); sub->notify(); + + delete sub; + delete obs; return 0; } #endif \ No newline at end of file