observer pattern

13
Observer Pattern Albert Guo [email protected] 1

Upload: guo-albert

Post on 19-May-2015

711 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Observer Pattern

1

Observer PatternAlbert Guo

[email protected]

Page 2: Observer Pattern

2

Motivation Intent Implementation Applicability & Examples

Agenda

Page 3: Observer Pattern

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

Page 4: Observer Pattern

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

Page 5: Observer Pattern

5

Implementation

Page 6: Observer Pattern

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.

Page 7: Observer Pattern

7

Applicability & Examples

Page 8: Observer Pattern

8

Applicability & Examples – cont.

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

Page 9: Observer Pattern

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.

Page 10: Observer Pattern

10

Applicability & Examples – cont.

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

Page 11: Observer Pattern

11

Applicability & Examples – cont.

ConcreteObserver

concrete Observer implementations.

Page 12: Observer Pattern

12

Applicability & Examples – cont.

ConcreteObserver concrete Observer implementations.

Page 13: Observer Pattern

13

Applicability & Examples – cont.

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