设计模式-观察者模式-程序

  1 #include <iostream>
  2 #include <list>
  3 using namespace std;
  4 //抽象的英雄
  5 class AbstractHero{
  6 public:
  7         virtual void Update() = 0;
  8 
  9 };
 10 class HeroA : public AbstractHero{
 11 public:
 12         virtual void Update( ){
 13                 cout<<"英雄A停止鲁"<<endl;
 14         }
 15         HeroA(){
 16                 cout <<"英雄A鲁BOSS"<<endl;
 17         }
 18 };
 19 class HeroB : public AbstractHero{
 20 public:
 21         virtual void Update( ){
 22 
 23                 cout<<"英雄B停止鲁"<<endl;
 24         }
 25         HeroB(){
 26                 cout <<"英雄B鲁BOSS"<<endl;
 27         }
 28 };
  29 class HeroC : public AbstractHero{
 30 public:
 31         virtual void Update( ){
 32                 cout<<"英雄C停止鲁"<<endl;
 33         }
 34         HeroC(){
 35                 cout <<"英雄C鲁BOSS"<<endl;
 36         }
 37 };
 38 class HeroD : public AbstractHero{
 39 public:
 40         virtual void Update( ){
 41 
 42                 cout<<"英雄D停止鲁"<<endl;
 43         }
 44         HeroD(){
 45                 cout <<"英雄D鲁BOSS"<<endl;
 46         }
 47 };
 48 
 49 class HeroE : public AbstractHero{
 50 public:
 51         virtual void Update( ){
 52 
 53                 cout<<"英雄E停止鲁"<<endl;
 54         }
 55         HeroE(){
 56                 cout <<"英雄E鲁BOSS"<<endl;
 57         }
 58 };
 59 //观察目标抽象
 60 class AbstractBoss{
 61 public:
 62         virtual void addHero(AbstractHero* hero) = 0;
 63         virtual void deleteHero(AbstractHero*hero) = 0;
 64         virtual void notify() = 0;
 65 };
 66 class BOSSA:public AbstractBoss{
 67 public:
 68           list<AbstractHero*> pHeroList;
 69 public:
 70           //
 71           virtual void addHero(AbstractHero* hero){
 72                   pHeroList.push_back(hero);
 73           }
 74           //
 75           virtual void deleteHero(AbstractHero* hero){
 76                   pHeroList.remove(hero);
 77           }
 78           //
 79           virtual void notify() {
 80                 for( list< AbstractHero* > :: iterator it = pHeroList.begin(); it != pHeroList.end(); it++){
 81                         (*it)->Update();
 82                 }
 83          }
 84 };
 85 void test01(){
 86         AbstractHero* heroA = new HeroA;
 87         AbstractHero* heroB = new HeroB;
 88         AbstractHero* heroC = new HeroC;
112 
-- 插入 --                                                                                      
 89         AbstractHero* heroD = new HeroD;
 90         AbstractHero* heroE = new HeroE;
 91        //
 92         AbstractBoss*bossA = new BOSSA;
 93         bossA->addHero(heroA);
 94         bossA->addHero(heroB);
 95         bossA->addHero(heroC);
 96         bossA->addHero(heroD);
 97         bossA->addHero(heroE);
 98         cout << "英雄C死了..." << endl;
 99         bossA->deleteHero(heroC);
100 
101         cout << "Boss死了..." << endl;
102         bossA->notify();
103 
104 }
105 
106 
107 int main(void){
108         test01();
109         return 0;
111 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值