Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe for all free courses

Observer Design Pattern in Java

What is Observer Design Pattern in Java ?.

The Gang of Four book (Design Patterns: Elements of Reusable Object-Oriented Software, 1995, Pearson Education, Inc. Publishing as Pearson Addison Wesley) says that the Observer design pattern should

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

Example : Let's say there is Weather Broadcasting company which broadcast weather changes to its subscribers. The subscribers or observers such as News Channels , News Paper Agencies and Weather Information Provider websites etc ... gets updated as soon as weather changes by the Weather Broadcasting company. Let's see how we can implement this Subject - Observer model in Java.

Program to demonstrate how to implement Observer Design Pattern in Java.

1. Subject.java
package com.hubberspot.designpattern.behavioral.observer;

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

}


2. Observer.java
package com.hubberspot.designpattern.behavioral.observer;

public interface Observer {
    
    public void update(int temperature);

}


3. WeatherBroadcast.java
package com.hubberspot.designpattern.behavioral.observer;

import java.util.ArrayList;
import java.util.List;

public class WeatherBroadcast implements Subject {

    private List< Observer > listOfObservers;
    private int temperature;    
    
    public WeatherBroadcast() {
        listOfObservers = new ArrayList< Observer >();
    }
    
    @Override
    public void addObserver(Observer observer) {
        
        listOfObservers.add(observer);
        
    }

    @Override
    public void removeObserver(Observer observer) {
        
        listOfObservers.remove(observer);        
        
    }

    @Override
    public void notifyObservers() {
        
        for(Observer observer : listOfObservers) {
            observer.update(temperature);
        }
        
    }
    
    public void temperatureChanged(int newTemperature) { 
        temperature = newTemperature;
        notifyObservers();
    }

}


4. NewsPaperAgency.java
package com.hubberspot.designpattern.behavioral.observer;

public class NewsPaperAgency implements Observer {

    @Override
    public void update(int temperature) {

        System.out.println("News Paper Agencies gets current temperature as: "
                + temperature + " degrees");

    }

}


5. NewsChannel.java
package com.hubberspot.designpattern.behavioral.observer;

public class NewsChannel implements Observer {

    @Override
    public void update(int temperature) {

        System.out.println("News Channel gets current temperature as: "
                + temperature + " degrees");

    }

}


6. WeatherInfoWebsite.java
package com.hubberspot.designpattern.behavioral.observer;

public class WeatherInfoWebsite implements Observer {

    @Override
    public void update(int temperature) {

        System.out.println("Weather Info Websites gets current temperature as: "
                + temperature + " degrees");

    }

}


7. ObserverDesignPatternTest.java
package com.hubberspot.designpattern.behavioral.observer;

public class ObserverDesignPatternTest {

    public static void main(String[] args) throws InterruptedException {

        WeatherBroadcast weatherBroadcast = new WeatherBroadcast();

        Observer newsPaperAgency = new NewsPaperAgency();
        Observer newsChannel = new NewsChannel();
        Observer weatherInfoWebsite = new WeatherInfoWebsite();

        weatherBroadcast.addObserver(newsPaperAgency);
        weatherBroadcast.addObserver(newsChannel);
        weatherBroadcast.addObserver(weatherInfoWebsite);
        
        weatherBroadcast.temperatureChanged(30);
        
        System.out.println("\nAfter 2 minutes temperature changes to 33 degrees...\n");
        
        Thread.sleep(120000);
        
        weatherBroadcast.temperatureChanged(33);

    }

}


Output of the program : 

 



Video tutorial to demonstrate how to implement Observer Design Pattern in Java.







 
© 2021 Learn Java by Examples Template by Hubberspot