設計模式:觀察者模式(observer)

gaopengtttt發表於2017-11-30

設計模式:觀察者模式(observer)

  • 這種設計模式大量的使用,簡單的說就是觀察者通過被觀察者來獲得外界某種事物的狀態

  • Observer模式提供給關聯物件一種同步通訊的手段,使某個物件與依賴它的其他物件之間保持狀態同步。
    如下圖:


    設計模式:觀察者模式(observer)
    image.png
  • 被觀察者(concreteSubject):其中必然包含了一個觀察者的列表,用於狀態發生變化的時候通過連結串列進行通知每個觀察者做出相應的變化

  • 觀察者(ConcreteObserver):其中必然包含各種應對外界狀態變化的方法,以供被觀察者呼叫

  • 圖中還有抽象類Subject和Observer其具體的被觀察者和觀察者可以通過虛擬函式從寫的方式來重寫這些定義好的介面

那麼我們做一個列子如下,比如上課的學生等待上課和下課的鈴音,那麼學生就是觀察者上下課鈴就是被觀察者如下:

點選(此處)摺疊或開啟

  1. /*************************************************************************
  2.   > File Name: test.cpp
  3.   > Author: gaopeng QQ:22389860 all right reserved
  4.   > Mail: gaopp_200217@163.com
  5.   > Created Time: Sat 25 Nov 2017 05:13:21 AM CST
  6.  ************************************************************************/

  7. #include<iostream>
  8. #include "vector"
  9. #include "string"
  10. #include<string.h>

  11. using namespace std;


  12. class StuObserver
  13. {

  14.         private:
  15.                 char name[256];
  16.         public:
  17.                 StuObserver(const char* name)
  18.                 {
  19.                         cout<< name<<"加入學習班!"<<endl;
  20.                         strcpy(this->name,name);
  21.                 }
  22.                 int class_begin()
  23.                 {

  24.                         cout<<name<<" 上課開始學習!"<<endl;
  25.                         return 0;
  26.                 }
  27.                 int class_end()
  28.                 {

  29.                         cout<<name<<" 結束學習下課!"<<endl;
  30.                         return 0;
  31.                 }
  32. };


  33. class bell
  34. {

  35.         private:
  36.                 vector<StuObserver* > v;//包含所有觀察者
  37.                 int i;
  38.         public:
  39.                 bell()
  40.                 {
  41.                         i = 0;
  42.                         v.clear();
  43.                 }
  44.                 ~ bell()
  45.                 {

  46.                         v.clear();
  47.                         for(i=0; i<v.size(); i++)
  48.                         {
  49.                                 delete v[i];
  50.                                 v[i]=NULL;
  51.                         }
  52.                 }
  53.                 int addObserver(StuObserver *o)
  54.                 {

  55.                         v.push_back(o);
  56.                         return 0;
  57.                 }

  58.                 int begin_class(const char* class_name)
  59.                 {

  60.                         cout<<"每位同學注意:"<<class_name<<"開始了上課了!!"<<endl;
  61.                         for(i=0; i<v.size(); i++)
  62.                         {

  63.                                 v[i]->class_begin();
  64.                         }
  65.                         return 0;
  66.                 }

  67.                 int end_class(const char* class_name)
  68.                 {

  69.                         cout<<"每位同學注意:"<<class_name<<"結束了下課了!!"<<endl;
  70.                         for(i=0; i<v.size(); i++)
  71.                         {

  72.                                 v[i]->class_end();
  73.                         }
  74.                         return 0;
  75.                 }
  76. };



  77. int main(void)
  78. {


  79.         bell m;
  80.         StuObserver* a=new StuObserver("高鵬1");
  81.         StuObserver* b=new StuObserver("高鵬2");
  82.         StuObserver* c=new StuObserver("高鵬3");
  83.         StuObserver* d=new StuObserver("高鵬4");

  84.         m.addObserver(a);
  85.         m.addObserver(b);
  86.         m.addObserver(c);
  87.         m.addObserver(d);

  88.         m.begin_class("數學");
  89.         m.end_class("數學");
  90.         m.begin_class("語文");
  91.         m.end_class("語文");
  92. }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2148092/,如需轉載,請註明出處,否則將追究法律責任。

相關文章