Design Patterns -- Obeserver

1. The intent of the Observer pattern is to define a one-to-many dependency such that when one object changes state, all its dependents are notified and updated automatically. see pic:

The Subject here is also called Observable. in SmallTalk, we also call Observer and Subject as View and Model.

The progress of Observer is like below:

" the observer registers with the subject, expressing its interest in observing. When a state change occurs, the subject notifies the observer of the change. When the observer no longer wishes to observe the subject, the observer unregisters from the subject. These steps are known as observer registration, notification, and unregistration, respectively." (from Exploring the Observer Design Pattern  Doug Purdy, Microsoft Corporation
Jeffrey Richter, Wintellect)

Register, "The observer invokes the Register method on the subject, passing itself as an argument. Once the subject receives this reference, it must store it in order to notify the observer when a state change occurs sometime in the future. Rather than storing the observer reference in an instance variable directly, most observer implementations delegate this responsibility to a separate object, typically a container. " see pic:

Notification: "When a state change occurs (AskPriceChanged), the subject retrieves all the observers within the container by invoking the GetObservers method. The subject then enumerates through the retrieved observers, calling the Notify method, which notifies the observer of the state change." see pic:

Unregistration, "This sequence is performed when the observer no longer needs to observe the subject. The observer calls the UnRegister method, passing itself as an argument. The subject then invokes the Remove method on the container, ending the period of observation", see pic:

"The use of a container provides the flexibility to support multiple observers per subject. This permits a subject to notify a potentially infinite number of observers of state changes, rather than just one."

For easy understanding, we can simplely combine the diagram above like below, (not whole)

 

2. Observer pattern supports MVC. MVC supports n-tier computing, which brings many practical advatages to software development and deployment. When the pattern uses MVC, 2 requirements must be met  for design:

2.1 Implementations of Observer(who observes...) must register their interest and must update themselves appropriately, often including repainting themselves(in GUI).

2.2 Subclasses of Observable(who is observed) must remember to notify observers when their value changed.

3. When applying MVC, the flow of events may seem indirect. Component(View, Observable) changes caused an update to the bean(Model, Observer). In turn, a change in the bean notifies the Conmponet(View) and they repaint. So changes from GUI layer to the business layer and back to the GUI layer.

4. One pattern of Observer is make the class we want to observe a subclass of Observable. But sometimes the class is already a subclass of another one. In this case, we can provide our class with an Observable object and have our class forward key method calls to it. The Component class in java.awt uses this approach, although it uses a PropertyChangeSupport object instead of an Observable object. In short, this approach implments Observer through delegation instead of subclassing.

Later today I will write an Observer example here to test the pattern.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值