Advertisement
Guest User

ObserverPatternPractice

a guest
Sep 17th, 2019
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. interface Subject {
  4.  
  5.     void register(Observer observer);
  6.     void unregister(Observer observer);
  7.     void notifyObservers();
  8. }
  9.  
  10.  
  11. interface Observer {
  12.     void update(String notification);
  13. }
  14.  
  15.  
  16. class BarristerSumonFacebookPage implements Subject {
  17.  
  18.     // Barrister Sumon's page has to know his followers
  19.     private ArrayList<Observer> observers = new ArrayList<Observer>();
  20.  
  21.     // His status
  22.     private String status;
  23.  
  24.  
  25.     // The way followers can 'Like' or register to his page
  26.     @Override
  27.     public void register(Observer observer) {
  28.         observers.add(observer);
  29.     }
  30.  
  31.     // The way followers can 'Unlike' or unsubscribe from his page
  32.     @Override
  33.     public void unregister(Observer observer) {
  34.         observers.remove(observer);
  35.     }
  36.  
  37.     // His page's way of notifying all this followers
  38.     @Override
  39.     public void notifyObservers() {
  40.         for (Observer observer : observers) {
  41.             observer.update("Barrister Sumon has new notification!");
  42.         }
  43.     }
  44.  
  45.     // Barrister Sumon use's this method to update his status.
  46.     // And when he does that, it should notify his followers
  47.     public void updatePage(String status) {
  48.         this.status = status;
  49.         notifyObservers();
  50.     }
  51. }
  52.  
  53. class CrazyFan implements Observer {
  54.     // Fan's name
  55.     private String name;
  56.  
  57.     // Constructor to set Fan's name
  58.     CrazyFan(String name) {
  59.         this.name = name;
  60.     }
  61.  
  62.     // This is the method, Barrister Sumon's FB page uses to notify the fan
  63.     public void update(String notification) {
  64.         // Just to prove that individual fan's get separate notifications
  65.         System.out.print("This is " + this.name + "'s page! ");
  66.         System.out.println(notification);
  67.     }
  68. }
  69.  
  70.  
  71. class ObserverPatternTestDrive {
  72.     public static void main(String[] args) {
  73.         // Barrister Sumon creates his own page
  74.         BarristerSumonFacebookPage fb_page = new BarristerSumonFacebookPage();
  75.  
  76.         // Let's create two crazy fans: Mofiz and Moznu
  77.         CrazyFan crazyFan1 = new CrazyFan("Mofiz");
  78.         CrazyFan crazyFan2 = new CrazyFan("Moznu");
  79.  
  80.         // Fans 'Like' his page, subscribes to it
  81.         fb_page.register(crazyFan1);
  82.         fb_page.register(crazyFan2);
  83.  
  84.         // Barrister Sumon updates his page with new video
  85.         fb_page.updatePage("I'm now live!");
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement