Sunday 13, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 6 August 2017

Design Patterns - Observer Pattern

Observer Pattern

Definition

An observer is a structural pattern that enables publish/subscribe functionality. This is accomplished by an autonomous object, publisher that allows other objects to attach or detach their subscription as they like. The pattern does not impose any limit to the number of observers that can
attach, or subscribe, themselves for notification on future changes in the publisher's state.

Where to use

When an object wants to publish information and many objects will need to receive that information.

Benefits

Makes for a loose coupling between publisher and subscriber as the publisher does not need to know who or how many subscribers there will be.

Drawbacks/consequences

In a complex scenario there may be problems to determining whether the update to the publisher is of relevance to all subscribers or just some of them. Sending an update signal to all subscribers might impose a communication overhead of not needed information.

Observer Pattern Class Diagram


Observer Pattern Example

package observer;
public interface IObserver {
void update(String state);
}
package subject;
import observer.IObserver;
public class Observer implements IObserver {
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void update(String state) {
setState(state);
System.out.println("Observer has received update signal
with new state: " + getState());
}
}

package observer;
public class Observer1 implements IObserver {
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void update(String state) {
setState(state);
System.out.println("Observer1 has received update signal
with new state: " + getState());
}
}
package observer;
public class Observer2 implements IObserver {
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void update(String state) {
setState(state);
System.out.println("Observer2 has received update signal
with new state: " + getState());
}
}

package subject;
import java.util.*;
import observer.IObserver;
public class LogSubject {
private List observerList = new
ArrayList();
private String state;
public String getState() {
return state;
}
public void attach(IObserver observer) {
observerList.add(observer);
}
public void detach(IObserver observer) {
observerList.remove(observer);
}
public void setState(String state) {
this.state = state;
stateChanged();
}
private void stateChanged() {
for (IObserver item: observerList) {
item.update(getState());
}
}
}
package observer;
import subject.*;
public class Client {
public static void main(String[] args) {
LogSubject subject = new LogSubject();
IObserver ob = new Observer();
IObserver ob1 = new Observer1();
IObserver ob2 = new Observer2();
subject.attach(ob);
subject.attach(ob1);
subject.attach(ob2);
subject.setState("state1");
subject.setState("state2");
subject.detach(ob1);
subject.setState("state3");
}
}

The following class diagram describes the relations in the above small example.


When Client is executed the result is:
c:>Observer has received update signal with new state: state1
c:>Observer1 has received update signal with new state: state1
c:>Observer2 has received update signal with new state: state1
c:>Observer has received update signal with new state: state2
c:>Observer1 has received update signal with new state: state2
c:>Observer2 has received update signal with new state: state2
c:>Observer has received update signal with new state: state3
c:>Observer2 has received update signal with new state: state3

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Design Patterns - Mediator Pattern Mediator Pattern Definition With the mediator pattern communication between objects is encapsulated with a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediat… Read More
  • Design Patterns - Iterator Pattern Iterator Pattern Definition The Iterator design pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Where to use Use to access the elements of… Read More
  • Design Patterns - Chain-of-responsibility Pattern Chain-of-responsibility Pattern The Chain-of-responsibility pattern lets more than one object handle a request without mutual knowledge. We avoid coupling between the sender of a request and the possible receivers. We place… Read More
  • Design Patterns - Command Pattern Command Pattern Definition The Command pattern is used to create objects that represents actions and events in an application. A command object encapsulates an action or event and contains all information required to under… Read More
  • Design Patterns - Memento Pattern Memento Pattern Definition To record an object internal state without violating encapsulation and reclaim it later without knowledge of the original object. A memento is an object that stores a snapshot of the internal sta… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Observer Pattern Rating: 5 Reviewed By: eHowToNow