observer software design pattern

Post on 21-Feb-2017

341 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Click to edit Master text stylesPresentationOn

Software Design Pattern

Software Design Pattern

Observer Pattern

R. Nirthika 2012/SP/142S. Dharshika 2012/SP/040T. Janani 2012/SP/035S.Kugarajeevan 2012/CSC/014

Click to edit Master text styles

What is Design Pattern?

Click to edit Master text styles

What is Design Patterns?

• In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design - Wikipedia

• It is a description for how to solve a problem that can be used in many different situations

Click to edit Master text stylesSoftware Design PatternCreational

Structural

Behavioral

Types Of Design Patterns

• Used to construct objects such that they can be decoupled from their implementing system.

• Used to form large object structures between many disparate objects.

• Used to manage algorithms, relationships, and responsibilities between objects

Click to edit Master text styles

Behavioral•Chain of Responsibility•Command•Interpreter•Iterator•Mediator•Memento•Observer•State•Strategy•Template•Visitor

Design Patterns

Behavioral•Chain of Responsibility•Command•Interpreter•Iterator•Mediator•Memento•Observer•State•Strategy•Template•Visitor

Click to edit Master text styles

Observer Design Pattern

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

Click to edit Master text styles

When to use the observer pattern

When you need many other objects to receive an update when another object changesExample

Stock market with thousands of stocks needs to send updates to objects representing individual stocks. The subject [Publisher] sends many stocks to the observers. The observers [subscribers] takes the ones they want and use them.

1

Click to edit Master text styles

Structure Of Observer

Click to edit Master text styles

Class Diagram

Click to edit Master text styles

Sequence Diagram

Click to edit Master text styles

Observer Design Pattern Real Time Example

Click to edit Master text styles

Demo

public interface Subject { public void registerObserver(Observer observer); public void removeObserver(Observer observer); public void notifyObservers();}

Subject.java

Click to edit Master text styles

Demo

import java.util.ArrayList;public class Product implements Subject{

private ArrayList<Observer> observers = new ArrayList<Observer>();public void registerObserver(Observer observer) {

observers.add(observer);}public void removeObserver(Observer observer) {

observers.remove(observer);}

Product.java

Click to edit Master text styles

Demo

private String productName;private String productType;String availability;

public Product(String productName, String productType,String availability) {super();this.productName = productName;this.productType = productType;this.availability=availability;

}

Continue…

Click to edit Master text styles

Demo

public void setAvailability(String availability) {this.availability = availability;notifyObservers();}public void notifyObservers() {System.out.println("Notifying to all the subscribers when "+productName+" "+productType+"

became available"); for (Observer ob : observers) {

ob.update(productName,productType,availability ); }

System.out.println();}

}

Continue…

Click to edit Master text styles

Demopublic interface Observer {

public void update(String productName, String productType,String availability);}

Observer.java

public class Person implements Observer{

String personName;

public Person(String personName) {this.personName = personName;

}public void update(String productName, String productType,String availability) {

System.out.println("Hello "+personName+", "+productName+" "+productType+" is now "+availability+" on flipkart");

}}

Person.java

Click to edit Master text styles

Demo

public class ObserverPatternMain {public static void main(String[] args) {

Person arpitPerson=new Person("Arpit"); Person johnPerson=new Person("John");

Product Camera=new Product("Canon", "Camera", "Not available"); Camera.registerObserver(johnPerson); Camera.setAvailability("Available"); Product samsungMobile=new Product("Samsung", "Mobile", "Not available"); samsungMobile.registerObserver(arpitPerson); samsungMobile.setAvailability("Available"); }

ObserverPatternMain.java

Click to edit Master text styles

Observer Pattern Pros & ConsPros:• Supports the principle to strive for loosely coupled designs between objects that interact.• Allows you to send data to many other objects in a very efficient manner.• No modification is need to be done to the subject to add new observers.• You can add and remove observers at anytime.

Cons:• If not used carefully the observer pattern can add unnecessary complexity• The order of Observer notifications is undependable

top related