大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
通俗点来说,就是一个主题类(subject)在状态发生改变时,通过观察者(observer)提供的方法,来更新观察者的状态。首先观察者需要到主题类中注册。
创新互联公司是一家成都网站建设、做网站,提供网页设计,网站设计,网站制作,建网站,按需网站策划,网站开发公司,2013年开创至今是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。附带维基百科的代码:
python:相对简洁
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;
}