笔记:Gof设计模式--Observer

1、意图

  Define a one-to-many dependency between objects so that when oneobject changes state, all its dependents are notified and updatedautomatically.

2、适应性

  Use the Observer pattern in any of the following situations:

  •  When an abstraction has two aspects, one dependent on the other.Encapsulating these aspects in separate objects lets you vary and reuse them independently.
  •  When a change to one object requires changing others, and you don't know how many objects need to be changed.
  •  When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.
 

3、结构

 

 

 

 

 

4、示例代码

  An abstract class defines the Observer interface:

class Subject; 
 
class Observer { 
public: 
    virtual ~ Observer(); 
    virtual void Update(Subject* theChangedSubject) = 0; 
protected: 
    Observer(); 
}; 

 

  Similarly, an abstract class defines the Subject interface:

class Subject { 
public: 
    virtual ~Subject(); 
    virtual void Attach(Observer*); 
    virtual void Detach(Observer*); 
    virtual void Notify(); 
protected: 
    Subject(); 
private: 
    List<Observer*> *_observers; 
}; 

void Subject::Attach (Observer* o) {        
    _observers->Append(o);    
} 
 
void Subject::Detach (Observer* o) {        
    _observers->Remove(o);    
} 
 
void Subject::Notify () { 
    ListIterator<Observer*> i(_observers); 
    for (i.First(); !i.IsDone(); i.Next()) { 
       i.CurrentItem()->Update(this); 
    } 
} 


 

 ClockTimer is a concrete subject for storing andmaintaining the time of day. It notifies its observers every second.ClockTimer provides the interface for retrieving individualtime units such as the hour, minute, and second.

class ClockTimer : public Subject { 
public: 
    ClockTimer(); 
    virtual int GetHour(); 
    virtual int GetMinute(); 
    virtual int GetSecond(); 
    void Tick(); 
}; 

void ClockTimer::Tick () { 
    // update internal time-keeping state 
    // ... 
    Notify(); 
} 


 

 

class DigitalClock: public Widget, public Observer {     
public:         
    DigitalClock(ClockTimer*); 
    virtual ~DigitalClock(); 
    virtual void Update(Subject*); 
    // overrides Observer operation 
    virtual void Draw(); 
    // overrides Widget operation; 
    // defines how to draw the digital clock 
private: 
    ClockTimer* _subject; 
}; 
 
DigitalClock::DigitalClock (ClockTimer* s) { 
    _subject = s; 
    _subject->Attach(this); 
} 
 
DigitalClock:: DigitalClock () { 
    _subject->Detach(this); 
}

void DigitalClock::Update (Subject* theChangedSubject) { 
    if (theChangedSubject == _subject) { 
        Draw(); 
    } 
} 
 
void DigitalClock::Draw () { 
    // get the new values from the subject 
 
    int hour = _subject->GetHour(); 
    int minute = _subject->GetMinute(); 
    // etc. 
 
    // draw the digital clock 
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值