通俗點(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;
}