青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

eryar

PipeCAD - Plant Piping Design Software.
RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
posts - 603, comments - 590, trackbacks - 0, articles - 0

[轉]設計模式-Observer

Posted on 2012-10-19 22:36 eryar 閱讀(1343) 評論(0)  編輯 收藏 引用

設計模式-Observer

1. 解決的問題:

   假如現在要編寫一個天氣預報的公布欄, 公布欄有兩種顯示方式, 一種是圖像方式顯示, 一種是表格形式顯示.

2. 問題分析:

   應該根據數據與現實分離的原則將天氣預報數據和現實形式分別封裝起來,

   今后可能增加其他的顯示形式;

   天氣預報數據發生變化后,需要對所有的顯示形式進行更新.

3. UML圖與代碼實現:

1)用Push的方式更新Observer數據, 通過Subject對Observer進行注冊:

  1: //這個例子中WeatherData就是Subject, 而WeatherView則是Observer,
  2: //這里WeatherView中沒有包含到WeatherData的引用,
  3: //因此這里是Subject用push的方法向Observer發送數據;
  4: //并且注冊和反注冊Observer的時候都是由Subject(WeatherData)執行的
  5: 
  6: #include <iostream>
  7: #include <vector>
  8: #include <algorithm>
  9: 
 10: using namespace std;
 11: 
 12: class WeatherData;
 13: 
 14: class WeatherView
 15: {
 16: public:
 17:     void Update(int temp, int hum)
 18:     {
 19:         temperature = temp;
 20:         humidity = hum;
 21:     };
 22:     virtual void Display()
 23:     {
 24:         cout << "temperature = " << temperature << ", humidity = " << humidity << endl;
 25:     }
 26: 
 27: private:
 28: 
 29:     int temperature;
 30:     int humidity;
 31: };
 32: 
 33: class GraphicView: public WeatherView
 34: {
 35: public:
 36:     void Display()
 37:     {
 38:         cout << "====--Weather Report With Graphic Format--===="<< endl;
 39:         WeatherView::Display();
 40:     }
 41: };
 42: 
 43: class TableView: public WeatherView
 44: {
 45: public:
 46:     void Display()
 47:     {
 48:         cout << "====--Weather Report With Table Format--===="<< endl;
 49:         WeatherView::Display();
 50:     }
 51: };
 52: 
 53: class WeatherData
 54: {
 55: public:
 56:     void SetWeahterData(int temp, int hum)
 57:     {
 58:         temperature = temp;
 59:         humidity = hum;
 60:         NotifyObservcer();
 61:     }
 62: 
 63:     void RegisterObserver(WeatherView* obs)
 64:     {
 65:         obsList.push_back(obs);
 66:     }
 67: 
 68:     void RemoveObserver(WeatherView* obs)
 69:     {
 70:         vector<WeatherView*>::iterator it;
 71:         it = find(obsList.begin(), obsList.end(), obs);
 72:         if (it != obsList.end())
 73:             obsList.erase(it);
 74:     }
 75: 
 76: private:
 77:     vector<WeatherView*> obsList;
 78:     int temperature;
 79:     int humidity;
 80:     void NotifyObservcer()
 81:     {
 82:        vector<WeatherView*>::iterator it;
 83:        for(it = obsList.begin(); it < obsList.end(); it++)
 84:        {
 85:            (*it)->Update(temperature, humidity);
 86:        }
 87:     }
 88: };
 89: 
 90: 
 91: int main()
 92: {
 93:     WeatherData *wd = new WeatherData();
 94:     GraphicView *gv = new GraphicView();
 95:     TableView *tv = new TableView();
 96: 
 97:     wd->RegisterObserver(gv);
 98:     wd->RegisterObserver(tv);
 99:     wd->SetWeahterData(23,45);
100:     gv->Display();
101:     tv->Display();
102: 
103:     wd->RemoveObserver(gv);
104:     wd->SetWeahterData(67,89);
105:     gv->Display();
106:     tv->Display();
107: 
108:     return 0;
109: }
2)用Pull的方式更新Observer數據, Observer自己進行注冊:

  1: #ifndef WEATHERDATA_HPP_INCLUDED
  2: #define WEATHERDATA_HPP_INCLUDED
  3: #include <iostream>
  4: #include <vector>
  5: 
  6: #include "WeatherView.hpp"
  7: 
  8: class WeatherData
  9: {
 10: public:
 11:     void SetWeahterData(int temp, int hum)
 12:     {
 13:         temperature = temp;
 14:         humidity = hum;
 15:         NotifyObservcer();
 16:     }
 17: 
 18:     int GetTemperature()
 19:     {
 20:         return temperature;
 21:     }
 22: 
 23:     int GetHumidty()
 24:     {
 25:         return humidity;
 26:     }
 27: 
 28:     void RegisterObserver(WeatherView* obs);
 29:     void RemoveObserver(WeatherView* obs);
 30: 
 31: private:
 32:     vector<WeatherView*> obsList;
 33:     int temperature;
 34:     int humidity;
 35:     void NotifyObservcer();
 36: };
 37: 
 38: #endif

=================

  1: #ifndef WEATHERVIEW_HPP_INCLUDED
  2: #define WEATHERVIEW_HPP_INCLUDED
  3: 
  4: #include <iostream>
  5: #include <vector>
  6: #include <algorithm>
  7: 
  8: 
  9: class WeatherData;
 10: 
 11: using namespace std;
 12: 
 13: class WeatherView
 14: {
 15: public:
 16:     WeatherView(WeatherData* wd);
 17: 
 18:     void Update();
 19:     void Register();
 20:     void Unregister();
 21: 
 22:     virtual void Display()
 23:     {
 24:         cout << "temperature = " << temperature << ", humidity = " << humidity << endl;
 25:     }
 26: 
 27: private:
 28:     WeatherData* wd;
 29:     int temperature;
 30:     int humidity;
 31: };
 32: 
 33: class GraphicView: public WeatherView
 34: {
 35: public:
 36:     GraphicView(WeatherData* wd);
 37:     void Display()
 38:     {
 39:         cout << "====--Weather Report With Graphic Format--===="<< endl;
 40:         WeatherView::Display();
 41:     }
 42: };
 43: 
 44: class TableView: public WeatherView
 45: {
 46: public:
 47:     TableView(WeatherData* wd);
 48:     void Display()
 49:     {
 50:         cout << "====--Weather Report With Table Format--===="<< endl;
 51:         WeatherView::Display();
 52:     }
 53: };
 54: 
 55: #endif

===================

  1: //這個例子中WeatherData就是Subject, 而WeatherView則是Observer,
  2: //這里WeatherView中有一個包含到WeatherData的指針,
  3: //因此這里是Observer用pull的方法主動向Observer索取數據;
  4: //并且注冊和反注冊都是Observer自己執行的
  5: #include <iostream>
  6: #include <vector>
  7: #include <algorithm>
  8: #include "WeatherView.hpp"
  9: #include "WeatherData.hpp"
 10: 
 11: void WeatherData::RegisterObserver(WeatherView* obs)
 12: {
 13:     obsList.push_back(obs);
 14: }
 15: 
 16: void WeatherData::RemoveObserver(WeatherView* obs)
 17: {
 18:     vector<WeatherView*>::iterator it;
 19:     it = find(obsList.begin(), obsList.end(), obs);
 20:     if (it != obsList.end())
 21:         obsList.erase(it);
 22: }
 23: 
 24: void WeatherData::NotifyObservcer()
 25: {
 26:    vector<WeatherView*>::iterator it;
 27:    for(it = obsList.begin(); it < obsList.end(); it++)
 28:    {
 29:        (*it)->Update();
 30:    }
 31: }
 32: 
 33: 
 34: WeatherView::WeatherView(WeatherData* pwd)
 35: {
 36:     wd = pwd;
 37: }
 38: 
 39: void WeatherView::Update()
 40: {
 41:     temperature = wd->GetTemperature();
 42:     humidity = wd->GetHumidty();
 43: };
 44: 
 45: void WeatherView::Register()
 46: {
 47:     wd->RegisterObserver(this);
 48: };
 49: 
 50: void WeatherView::Unregister()
 51: {
 52:     wd->RemoveObserver(this);
 53: };
 54: 
 55: GraphicView::GraphicView(WeatherData* pwd) : WeatherView(pwd)
 56: {
 57: 
 58: }
 59: 
 60: TableView::TableView(WeatherData* pwd) : WeatherView(pwd)
 61: {
 62: 
 63: }
 64: int main()
 65: {
 66:     WeatherData *wd = new WeatherData();
 67:     GraphicView *gv = new GraphicView(wd);
 68:     gv->Register();
 69:     TableView *tv = new TableView(wd);
 70:     tv->Register();
 71: 
 72:     wd->SetWeahterData(23,45);
 73:     gv->Display();
 74:     tv->Display();
 75: 
 76:     gv->Unregister();
 77:     wd->SetWeahterData(67,89);
 78:     gv->Display();
 79:     tv->Display();
 80: 
 81:     return 0;
 82: }

======================

上面兩種實現的執行結果如下:

  1: ====--Weather Report With Graphic Format--====  
  2: temperature = 23, humidity = 45  
  3: ====--Weather Report With Table Format--====  
  4: temperature = 23, humidity = 45  
  5: ====--Weather Report With Graphic Format--====  
  6: temperature = 23, humidity = 45  
  7: ====--Weather Report With Table Format--====  
  8: temperature = 67, humidity = 89  

4. Push還是Pull?

對于上面的例子, Observer中的數據是從Subject中一次性全部更新的(temperature 和 humidity), 這種更新數據的方式便是push;然而如果WeatherData中的數據量非常大, 而有些Observer并不需要所有的數據, 比如現在要新增兩個顯示方式,一個是只顯示溫度,而另一個則只顯示濕度, 這樣的話就沒有必要讓所有的Observer都得到所有的數據. 最好的方式是Observer能根據自己的需要從Subject中去取得數據,這種更新數據的方式便是Pull. Observer模式中Push和Pull兩種設計方法體現在具體的程序中就是Observer中的Update()接口參數不同, 對于Push模式, Update()接口的參數通常就是需要Push的那些數據,比如這里的溫度和濕度; 對于Pull模式, Update()的參數是Subject的一個引用, 然后Subject提供一些數據接口,由Observer通過這些接口自己取得所需要的數據.

5. 總結:

   1. Strategy 模式定義:

      定義對象間的一種一對多的依賴關系,當一個對象的狀態發生改變時,所有依賴于它的對象都得到通知并被自動更新.

   2. 體現的設計原則:

  • 將數據與現實分別封裝;
  • 多使用組合,少使用繼承;
  • 面向接口編程,而不面向實現編程;

   3. UML圖:

   4. 要點:

  • Strategy 基類需要定義出可供Client使用的一些算法接口;
  • 可以隨時根據需要增加 Strategy 而不會影響到Client;
  • Client 里面需要包含對 Strategy 的引用;
  • Client 可以隨時更換 Strategy;

6. 理解:

  • Observer模式是解決對象之間數據傳遞問題的一種模式;
  • Observer的注冊可以由Subject執行也可以由Observer自己執行;
  • 和Strategy模式的比較:
    1) Observer模式中Observer 中定義了 Update()接口供 Subject調用; 而Strategy模式中,Strategy定義了AlgrithmInterface()供Client調用;
    2) Observer模式中Subject和Observer是一對多的關系, 因此Subject是一次調用n個Observer的Update()接口;而Strategy模式中Client與Strategy之間是一對一的關系, Client 就是調用指定的那個Strategy的AlgrithmInterface();
    3) 也正因為這種對應關系的不同, 在Observer模式中, Subject可以Register或者Remove某個Observer, 而Strategy模式中通常只是set某個Strategy

 

原文地址: http://blog.csdn.net/zhongjiekangping/article/details/6903017

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久视频免费观看| 久久精品1区| 欧美午夜电影一区| 你懂的视频一区二区| 亚洲一级黄色| 亚洲专区一二三| 亚洲免费影视第一页| 亚洲欧美日韩一区二区在线| 午夜精品www| 久久久青草婷婷精品综合日韩 | 欧美影院视频| 欧美一区午夜视频在线观看| 久久国产欧美日韩精品| 久久久精品性| 亚洲丰满在线| 亚洲国产视频一区二区| 夜夜嗨av一区二区三区网页| 亚洲午夜未删减在线观看| 亚洲欧美资源在线| 噜噜噜噜噜久久久久久91| 欧美日韩八区| 国产性天天综合网| 最新国产精品拍自在线播放| 久久在线免费观看视频| 免费高清在线一区| 免费成人av在线看| 亚洲免费观看高清完整版在线观看熊 | 欧美欧美天天天天操| 国产精品欧美日韩一区二区| 国产中文一区| 一区二区三区四区五区视频| 久久爱www| 91久久久在线| 久久久精品tv| 国产精品久久久久久久久免费桃花 | 亚洲福利小视频| 亚洲欧美国产另类| 欧美成人精品不卡视频在线观看| 国产精品久久久99| 最新中文字幕一区二区三区| 欧美一级专区| 亚洲免费观看在线观看| 老司机免费视频久久| 国产精品区一区| 一本色道久久综合亚洲91| 麻豆成人av| 欧美一级欧美一级在线播放| 欧美日韩亚洲高清一区二区| 亚洲第一精品在线| 久久精品国产第一区二区三区最新章节 | 亚洲精品一区二区三区樱花| 久久精品91| 国产精品久久久久av免费| 日韩午夜电影在线观看| 麻豆成人综合网| 新片速递亚洲合集欧美合集| 久久精品国产77777蜜臀| 国产精品video| 日韩天堂在线观看| 亚洲激情婷婷| 开元免费观看欧美电视剧网站| 午夜视频一区在线观看| 欧美日韩一区二区在线播放| 1024欧美极品| 久久久噜噜噜久久| 欧美在线视频一区二区三区| 国产日韩精品一区二区三区在线| 午夜精品久久久久久久久久久久久| 亚洲毛片网站| 欧美日韩三级视频| 亚洲一区二区免费| 在线视频欧美精品| 国产精品激情| 久久激情婷婷| 久久精品国语| 久久久青草婷婷精品综合日韩| 亚洲免费在线看| 国产精品视频yy9099| 亚洲视频 欧洲视频| 99成人免费视频| 欧美视频一区| 欧美亚洲自偷自偷| 久久综合九色欧美综合狠狠| 欧美激情精品久久久久久| 一区二区三区黄色| 欧美三区美女| 午夜精品久久| 久久久国产精品一区二区中文| 精品成人在线视频| 亚洲电影自拍| 欧美三级精品| 久久久精品视频成人| 欧美大学生性色视频| 一本到高清视频免费精品| 亚洲视频久久| 一区在线影院| 亚洲人成在线播放| 国产视频精品网| 亚洲国产精品免费| 国产精品日本一区二区 | 乱中年女人伦av一区二区| 久久夜色精品国产噜噜av| 亚洲三级视频| 午夜精品久久久久久| 亚洲国产欧美另类丝袜| 亚洲最黄网站| 激情五月综合色婷婷一区二区| 亚洲二区视频| 国产欧美日本| 亚洲人成在线观看一区二区| 国产午夜精品在线观看| 亚洲日本欧美| 激情综合亚洲| 亚洲一区免费看| 亚洲大片一区二区三区| 亚洲影院色无极综合| 亚洲品质自拍| 亚洲男人的天堂在线aⅴ视频| 黄色日韩在线| 亚洲一区二区高清| 亚洲精品久久在线| 一区二区三区视频在线看| 亚洲欧洲精品一区二区三区波多野1战4 | 久久深夜福利免费观看| 欧美日韩系列| 亚洲品质自拍| 在线国产精品播放| 亚洲一区在线观看视频 | 一本一道久久综合狠狠老精东影业 | 午夜精品美女自拍福到在线 | 一区二区电影免费在线观看| 久久久一二三| 久久久精品一品道一区| 国产精品国产| 日韩一级大片| 一本大道久久精品懂色aⅴ| 蜜桃av噜噜一区二区三区| 久久精品国产综合| 国产精品久久久久久久久久直播 | 精品av久久707| 羞羞视频在线观看欧美| 欧美一区二区三区喷汁尤物| 国产精品成人在线观看| 一区二区三区回区在观看免费视频| 亚洲精品视频在线| 欧美电影在线免费观看网站| 欧美福利一区二区| 亚洲激情网站| 欧美激情亚洲国产| 亚洲黑丝在线| 一区二区三区国产在线| 欧美日韩美女在线观看| 夜夜嗨一区二区| 亚洲欧美日韩国产中文| 国产精品福利网| 中文精品视频| 欧美一区影院| 欧美日韩在线直播| 亚洲午夜激情网站| 一区二区三区**美女毛片| 欧美日韩亚洲视频一区| 99在线|亚洲一区二区| 亚洲欧美日韩国产精品 | 日韩视频不卡| 欧美日韩免费视频| 午夜免费久久久久| 欧美成人一二三| 亚洲婷婷免费| 国产日韩欧美一二三区| 久久综合中文色婷婷| 91久久精品一区| 亚洲宅男天堂在线观看无病毒| 午夜精品影院在线观看| 欧美1级日本1级| 亚洲在线观看| 黄色一区二区在线观看| 免费在线亚洲欧美| 国产精品国产三级国产a| 久久久亚洲精品一区二区三区| 国产啪精品视频| 葵司免费一区二区三区四区五区| 亚洲观看高清完整版在线观看| 中文日韩在线| 好吊色欧美一区二区三区视频| 欧美国产三级| 午夜精品久久久久久久99樱桃| 欧美国产视频日韩| 欧美一级理论片| 亚洲国内精品在线| 国产日韩欧美a| 欧美日韩美女在线观看| 久久久999| 亚洲一区二区三区影院| 欧美激情欧美激情在线五月| 亚洲影视在线| 亚洲美女精品久久| 在线观看亚洲精品视频| 国产老肥熟一区二区三区| 欧美肥婆bbw| 久久亚洲影音av资源网|