observer pattern

Post on 19-May-2015

711 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Observer PatternAlbert Guo

junyuo@gmail.com

2

Motivation Intent Implementation Applicability & Examples

Agenda

3

Let's assume we have a stock system which provides data for several types of client. We want to have a client implemented as a web based application but in near future we need to add clients for mobile devices, Palm or Pocket PC, or to have a system to notify the users with sms alerts. Now it's simple to see what we need from the observer pattern: we need to separate the subject(stocks server) from it's observers(client applications) in such a way that adding new observer will be transparent for the server.

Motivation

4

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

Intent

5

Implementation

6

Class/Interface Description

Subject interface or abstract class defining the operations for attaching and de-attaching observers to the client.

ConcreteSubject It maintain the state of the object and when a change in the state occurs it notifies the attached Observers.

Observer interface or abstract class defining the operations to be used to notify this object.

ConcreteObserver concrete Observer implementations.

Implementation – cont.

7

Applicability & Examples

8

Applicability & Examples – cont.

Subject interface or abstract class defining the operations for attaching and de-attaching observers to the client.

9

Applicability & Examples – cont.

ConcreteSubject

It maintain the state of the object and when a change in the state occurs it notifies the attached Observers.

10

Applicability & Examples – cont.

Observer interface or abstract class defining the operations to be used to notify this object.

11

Applicability & Examples – cont.

ConcreteObserver

concrete Observer implementations.

12

Applicability & Examples – cont.

ConcreteObserver concrete Observer implementations.

13

Applicability & Examples – cont.

Email Subscriber: 《集中市場》三大法人賣超 79 億元,法說會績優股力抗國際崩跌SMS Subscriber: 《集中市場》三大法人賣超 79 億元,法說會績優股力抗國際崩跌RSS Subscriber: 《集中市場》三大法人賣超 79 億元,法說會績優股力抗國際崩跌----------------------------------------------------Email Subscriber: 台股盤後-鴻家軍登高力抗希臘債信危機 開低拉高力守8000 關SMS Subscriber: 台股盤後-鴻家軍登高力抗希臘債信危機 開低拉高力守 8000關RSS Subscriber: 台股盤後-鴻家軍登高力抗希臘債信危機 開低拉高力守 8000關---------------------------------------------------

top related