Showing posts with label observer design pattern. Show all posts
Showing posts with label observer design pattern. Show all posts

Friday, November 29

Observer design pattern in java

Lets talk about observer design pattern today--

Lets take in consideration a real time scenarion 

There is a News Control Center that controls current news and subscribers . 

Lets say there are two subscribers Ching and Chong

Ching and Chong are registered by News Control center for current news 

If at any point of time current news are updated News needs to be informed to its all subscribers. 

Subscriber will not keep on checking news all the time If any current news is updated. They will register once for all and News

will ensure that they are timely informed about every new news. So how will that hapend





Ching and Chong will act as observers / listener . News will see If any new news is updated it is informed to its subscribers 


Below Code details this and implement the scenario in java code .

NewsControlCenter Class will hold the main control to apply for subscriber registeration and bring in new news to the agency


==================



public class NewsControlCenter {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Subscriber sub1=new Subscriber("ching");
        Subscriber sub2=new Subscriber("chong");
        News news=new TimesNews();
        news.registerSubscriber(sub1);
        news.registerSubscriber(sub2);
        
        System.out.println("==========news changed======");

        news.updateNews("Narayan traced");
        
        System.out.println("==========news changed again ======");

        news.updateNews("Govt declared prize money on informing Narayan's whereabout.");

        
       
    }

}

==================


import java.util.List;

public interface News {
    void registerSubscriber(Subscriber sub);

    void unRegisterSubscriber(Subscriber sub);

    void updateNews(String news);
    void inform(Subscriber sub);
    void inform(List subs);
    void readCurrentNews();
}


==================








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

public class TimesNews implements News {

    String currentNews = "Asharam in Jail";

    List subscribers = new ArrayList();

    @Override
    public void registerSubscriber(Subscriber sub) {
        subscribers.add(sub);
    }

    @Override
    public void unRegisterSubscriber(Subscriber sub) {
        subscribers.remove(sub);
    }

    @Override
    public void updateNews(String news) {

        this.currentNews = news;
        inform(subscribers);

    }

    public void inform(Subscriber sub) {

    }

    public void inform(List subs) {

        for (Iterator iterator = subs.iterator(); iterator.hasNext();) {
            Subscriber subscriber = (Subscriber) iterator.next();
            inform(subscriber);
        }

    }

    public void readCurrentNews() {
        System.out.println(this.currentNews);
    }

}


=====================


-->


public class Subscriber {

    String name;

    public Subscriber(String name) {
        this.name = name;
    }

    public void readNews(News news) {

        System.out.println("Mr. " +name+ ": got this news:::");
        news.readCurrentNews();
}
}


=================



On executing this program as java application . Below logs will be written on console


----------------------------------------------------------

opening news for ::: ching
Asharam in Jail
opening news for ::: chong
Asharam in Jail
==========news changed======
Mr. ching: got this news:::
Narayan traced
Mr. chong: got this news:::
Narayan traced
==========news changed again ======
Mr. ching: got this news:::
Govt declared prize money on informing Narayan's whereabout.
Mr. chong: got this news:::
Govt declared prize money on informing Narayan's whereabout.

-------------------------------------------------------------------------------------