Observer
Observer pattern is a pattern used when there are many different objects (Observer) depend on changes on a particular object (Observable).
For instance javax.swing packages event management is designed using Observer pattern. There may be several implementations of Listener (Observer) that are observing a JButton's (Observable) click, mouseEntered, mouseExited etc. events.
Following code snippet shows a small implementation of Observer Design Pattern.
Consumer.java
public class Consumer {
private String name;
public Consumer(String name){
this.name = name;
}
public void update() {
System.out.println(name + " Updated");
}
}
Producer.java
public class Producer {
private Market supplyMarket;
public Producer(Market sm){
supplyMarket = sm;
}
public void notifyMarket() {
supplyMarket.updateConsumers();
}
}
Market.java
import java.util.*;
public class Market {
private Set<Consumer> consumers;
public Market(){
consumers = new HashSet<Consumer>();
}
public void updateConsumers() {
Iterator<Consumer> it = consumers.iterator();
while(it.hasNext()){
it.next().update();
}
}
public void addConsumer(Consumer consumer){
consumers.add(consumer);
}
}
ObserverTest.java
public class ObserverTest{
public static void main(String[] args){
//Market is the Observable
Market stockMarket = new Market();
//Producer is the manipulator of the market
Producer dialog = new Producer(stockMarket);
//Consumer is the Observer of the Observable (Market)
Consumer shareHolder1 = new Consumer("Shazin");
Consumer shareHolder2 = new Consumer("Shahim");
stockMarket.addConsumer(shareHolder1);
stockMarket.addConsumer(shareHolder2);
//When there is something to be notified producer notifies
//the Market in turn market notifies all the consumers of that
//Market
dialog.notifyMarket();
}
}
For instance javax.swing packages event management is designed using Observer pattern. There may be several implementations of Listener (Observer) that are observing a JButton's (Observable) click, mouseEntered, mouseExited etc. events.
Following code snippet shows a small implementation of Observer Design Pattern.
Consumer.java
public class Consumer {
private String name;
public Consumer(String name){
this.name = name;
}
public void update() {
System.out.println(name + " Updated");
}
}
Producer.java
public class Producer {
private Market supplyMarket;
public Producer(Market sm){
supplyMarket = sm;
}
public void notifyMarket() {
supplyMarket.updateConsumers();
}
}
Market.java
import java.util.*;
public class Market {
private Set<Consumer> consumers;
public Market(){
consumers = new HashSet<Consumer>();
}
public void updateConsumers() {
Iterator<Consumer> it = consumers.iterator();
while(it.hasNext()){
it.next().update();
}
}
public void addConsumer(Consumer consumer){
consumers.add(consumer);
}
}
ObserverTest.java
public class ObserverTest{
public static void main(String[] args){
//Market is the Observable
Market stockMarket = new Market();
//Producer is the manipulator of the market
Producer dialog = new Producer(stockMarket);
//Consumer is the Observer of the Observable (Market)
Consumer shareHolder1 = new Consumer("Shazin");
Consumer shareHolder2 = new Consumer("Shahim");
stockMarket.addConsumer(shareHolder1);
stockMarket.addConsumer(shareHolder2);
//When there is something to be notified producer notifies
//the Market in turn market notifies all the consumers of that
//Market
dialog.notifyMarket();
}
}