728x90 AdSpace

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
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Observer Pattern Rating: 5 Reviewed By: Annamalai Thangaraj