13
Observer Design Pattern From Definition to Implementation 17-Dec-14 Mudasir Qazi - [email protected] 1

Design Pattern - Observer Pattern

Embed Size (px)

Citation preview

Page 1: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 1

Observer Design Pattern

From Definition to Implementation

17-Dec-14

Page 2: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 2

Contents / Agenda

• Definition• Diagram• Sequence Diagram• Examples• Advantages and Usage• Implementation – UML Class diagram• Implementation – Step 1 to 3• Test and Result

17-Dec-14

Page 3: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 3

Definition1. The observer pattern is a software design pattern in which an object,

called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

2. There are two typical models for Observer based on the way data is passed. It can be a Push model or a Pull model. In the push model, data is pushed along with the notification. An example for this is mail delivery, magazine subscriptions etc. In the pull model, the client will be notified about what is happening. But, it is always the responsibility of the client to check whether the relevant data is there or not. An example for this is RSS feeds. Whenever there is a change in the website, and if you have subscribed for the feed, you will get a notification. But, it is up to you to go back and see if you are interested.

• It comes under the “Behavioral Design Patterns”.• It is also known as Dependents, Publish-Subscribe, Model-View.

17-Dec-14

Page 4: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 4

Diagram

17-Dec-14

Page 5: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 5

Sequence Diagram

17-Dec-14

Page 6: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 6

Examples1. Company updates all its shareholders for any decision they make here Company is

Subject and Shareholders are Observers, any change in policy of company and Company notifies all its Shareholders or Observer.

2. A typical example for the Observer pattern is the conventional mail delivery system. Whenever you have a mail, the postman will come and knock at your door with the mail. Just imagine if you have to go to the post office every day and check whether there are any mails. It would have been a really inefficient system as each individual needs to go to the post office periodically. This is eliminated in real life by introducing a mail delivery mechanism.

3. It is the same in a GUI system . Your business logic, in this case, the cassette player, will update all the GUI components periodically, just like a post man who comes to your house with mail. It would have been inefficient for the total system to implement a periodic check by each widget to get the progress.

4. We read newspaper. We get the daily news by news paper. Here we are the observer and the news paper is observable. Observer Design Pattern keeps the data of its entire listener and whenever there is any change it notifies to its listener.

5. Speaking clock is another example of Observer Design Pattern. Speaking clock notifies when it finishes one hour.17-Dec-14

Page 7: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 7

Advantages and Usage

• Benefits:1. Objects are loosely coupled with each other but still communicates properly.2. Observers can be added/removed at any time.3. Subject and observer can belong to different abstraction layers.4. Subject does not need to know the concrete class of an observer, just that each

observer implements the update interface.5. Observers can be added without modifying the subject.6. Note that: there is java.util.Observable Class and java.util.Observer Interface

available in java library to implement this pattern.

• Usage:1. It is used when there is one to many relationship between objects such as if one

object is modified, its dependent objects are to be notified automatically.2. When an abstraction has two aspects, one dependent on the other.

Encapsulating these aspects in separate objects lets you vary and reuse them independently.

17-Dec-14

Page 8: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 8

Implementation - UML

This is a example where three observers are attached to a subject through abstract observer class. If any changes happened in subject, it will notify all observers about its changes.

For Example, number base converter observers are attached to a subject containing a integer value. If we change the integer in test, all observer will be changing new value of integer to their respective base.

17-Dec-14

Page 9: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 9

Step 1 : Subject

List: to keep track of attached observers.State: current stateattach: to attach a observer with subjectnotifyAllObservers: to send message to

all attached observers.

17-Dec-14

Page 10: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 10

Step 2 : Observer (abstract)

17-Dec-14

Page 11: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 11

Step 3 : Observers

There are our three observer concrete classes that inherits the abstract observer class and updates there function with respect to new state of subject.Note all these observer are attached to the subject.

17-Dec-14

Page 12: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 12

Step 4 : Test

• We can easily test our pattern by following code.

Following are the two test in the code:First,the state of subject is set to 15, means every observer will print number 15 after converting it in its respective base.Second,the state of subject is set to 10, observer observe this and print this number in there respective base without any extra effort. Our notify method notifies all observers that state of subject has been changed.

17-Dec-14

Page 13: Design Pattern - Observer Pattern

Mudasir Qazi - [email protected] 13

Step 5 : Result

• Here is a clear result of our test.

17-Dec-14