Java Observer Pattern - Watching Company Share Values

This is a very simple example of using the Observer Design Pattern.

In this instance we have a company share value (subject) i.e. sharevalue. Each company has a defined sharevalue which may/may not change throughout the day.

We then want to notify via the company web site or mobile phones (observers) of any change to this share value.

Share will notify to CompanyWebSite and MobilePhone to disply new share value.

To implement this we have a Subject interface which contains methods for adding, removing and notifying Observers and an Observer interface which contains update(float sharevalue) method which will be called by Subject implementation when a company share value changes.

import java.util.ArrayList;

interface Observer {
       public void update(float tradingvalue);
}

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

class Share implements Subject {
       private ArrayList<Observer> observers = new ArrayList<Observer>();
       private float sharevalue;
       private String company;

       public Share(float sharevalue, String company) {
              this.sharevalue= sharevalue;
              this.company = company;
       }

       public float getShareValue() {
              return sharevalue;
       }

       public void setShareValue(float sharevalue) {
              this.sharevalue = sharevalue;
              notifyObservers();
       }

       public String getCompany() {
              return this.company;
       }

       @Override
       public void registerObserver(Observer observer) {
              observers.add(observer);
       }

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

       @Override
       public void notifyObservers() {
              for (Observer ob : observers) {
                     System.out
                                  .println("Notifying Observers on change in Company Share Price");
                     ob.update(this.sharevalue);
              }

       }

}

class CompanyWebSite implements Observer {
       @Override
       public void update(float sharevalue) {
              System.out.println("Company Web Site: Share Value updated, new Share Value is: "
                           + sharevalue);
       }
}

class MobilePhone implements Observer {
       @Override
      public void update(float sharevalue) {
              System.out.println("Mobile Phone: Share Value updated, new Share Value is: "
                           + sharevalue);
}

public class TestShareValueObserver {

       public static void main(String args[]) {
              // this will maintain all share value information
              CompanyWebSite companyWebSite = new CompanyWebSite();
              MobilePhone mobilePhone = new MobilePhone();

              Share companyShare = new Share(26.25f,
                           "Kerry Group");
              companyShare.registerObserver(companyWebSite);
              companyShare.registerObserver(mobilePhone);
              companyShare.setShareValue(26.35f);

       }
}

Comments

Popular posts from this blog

Basic Send Message to MQ with Java and IBM MQ JMS

Basic Receive Message to MQ with Java and IBM MQ JMS

Creating a simple Alert / Success Message with ASP.NET/VB using Bootstrap