真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

設(shè)計(jì)模式(二):觀(guān)察者模式-創(chuàng)新互聯(lián)

 在此種模式中,一個(gè)目標(biāo)對(duì)象管理所有相依于它的觀(guān)察者對(duì)象,并且在它本身的狀態(tài)改變時(shí)主動(dòng)發(fā)出通知。這通常透過(guò)呼叫各觀(guān)察者所提供的方法來(lái)實(shí)現(xiàn)。此種模式通常被用來(lái)實(shí)作事件處理系統(tǒng)。設(shè)計(jì)模式(二):觀(guān)察者模式

 通俗點(diǎn)來(lái)說(shuō),就是一個(gè)主題類(lèi)(subject)在狀態(tài)發(fā)生改變時(shí),通過(guò)觀(guān)察者(observer)提供的方法,來(lái)更新觀(guān)察者的狀態(tài)。首先觀(guān)察者需要到主題類(lèi)中注冊(cè)。

創(chuàng)新互聯(lián)公司是一家成都網(wǎng)站建設(shè)、做網(wǎng)站,提供網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,按需網(wǎng)站策劃,網(wǎng)站開(kāi)發(fā)公司,2013年開(kāi)創(chuàng)至今是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶(hù)品牌價(jià)值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開(kāi)發(fā),后臺(tái)程序制作以及后期項(xiàng)目運(yùn)營(yíng)并提出專(zhuān)業(yè)建議和思路。

附帶維基百科的代碼:

python:相對(duì)簡(jiǎn)潔

class AbstractSubject(object):
    def register(self, listener):
        raise NotImplementedError("Must subclass me")
 
    def unregister(self, listener):
        raise NotImplementedError("Must subclass me")
 
    def notify_listeners(self,event):
        raise NotImplementedError("Must subclass me")
 
class Listener(object):
    def __init__(self, name, subject):
        self.name= name
        subject.register(self)
 
    def notify(self,event):
        print self.name,"received event", event 
class Subject(AbstractSubject):
    def __init__(self):
        self.listeners= []
        self.data= None
 
    def getUserAction(self):
        self.data= raw_input('Enter something to do:')
return self.data
 
    # Implementabstract Class AbstractSubject
 
    def register(self, listener):
        self.listeners.append(listener)
 
    def unregister(self, listener):
        self.listeners.remove(listener)
 
    def notify_listeners(self,event):
for listener in self.listeners:
            listener.notify(event)
 
 
if __name__=="__main__":
    # make a subjectobject to spy on
    subject= Subject()
 
    # register two listeners to monitor it.
    listenerA= Listener("", subject)
    listenerB= Listener("", subject)
 
    # simulatedevent
    subject.notify_listeners ("")
    # outputs:
    # received event 
    # received event 
 
    action= subject.getUserAction()
    subject.notify_listeners(action)
    #Enter something todo:hello
    # outputs:
    # received event hello
    # received event hello

C++代碼,需要多理解:

#include 
#include
#include
#include
using namespace std;
 
// The Abstract Observerclass ObserverBoardInterface
{
public:
virtual void update(float a,float b,float c) = 0;
};
 
// Abstract Interface for Displaysclass DisplayBoardInterface
{
public:
virtual void show() = 0;
};
 
// The Abstract Subjectclass WeatherDataInterface
{
public:
virtual void registerob(ObserverBoardInterface* ob) = 0;
virtual void removeob(ObserverBoardInterface* ob) = 0;
virtual void notifyOb() = 0;
};
 
// The Concrete Subjectclass ParaWeatherData: public WeatherDataInterface
{
public:
void SensorDataChange(float a,float b,float c)
    {
        m_humidity= a;
        m_temperature= b;
        m_pressure= c;
        notifyOb();
    }
 
void registerob(ObserverBoardInterface* ob)
    {
        m_obs.push_back(ob);
    }
 
void removeob(ObserverBoardInterface* ob)
    {
        m_obs.remove(ob);
    }
protected:
void notifyOb()
    {
        list::iterator pos = m_obs.begin();
while (pos != m_obs.end())
        {
            ((ObserverBoardInterface* )(*pos))->update(m_humidity,m_temperature,m_pressure);
            (dynamic_cast(*pos))->show();
++pos;
        }
    }
 
private:
float        m_humidity;
float        m_temperature;
float        m_pressure;
    list m_obs;
};
 
// A Concrete Observerclass CurrentConditionBoard : public ObserverBoardInterface, public DisplayBoardInterface
{
public:
    CurrentConditionBoard(WeatherDataInterface& a):m_data(a)
    {
        m_data.registerob(this);
    }
void show()
    {
        cout<<"_____CurrentConditionBoard_____"<m_maxt)
        {
            m_maxt= t;
        }
if (tSensorDataChange(10.2, 28.2, 1001);
    wdata->SensorDataChange(12, 30.12, 1003);
    wdata->SensorDataChange(10.2, 26, 806);
    wdata->SensorDataChange(10.3, 35.9, 900);
 
    wdata->removeob(currentB);
 
    wdata->SensorDataChange(100, 40, 1900);  
 
    delete statisticB;
    delete currentB;
    delete wdata;
 
return 0;
}

當(dāng)前標(biāo)題:設(shè)計(jì)模式(二):觀(guān)察者模式-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://weahome.cn/article/icjii.html

其他資訊

在線(xiàn)咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部