Created a stock market application by implementing observable pattern.
In observable pattern the subsribers will be notified when ever there is any event occured in publishers object. In our example stock is the publisher ( Observables) and we kept a list of subscribers list in NotificationManager class.
Stock.java
package com.learning.designpattern.observer;
import java.io.File;
/**
* Stock class
* Act as Observable or publisher
*
* Where ever a change happens in Stock object (publisher), it will notifies the subscribers
*
* two methods stoploss() & entry() corresponds to file opening and saving events
*
*
*
*
* @author smithesh k k
*
*/
public class Stock {
private Breakout breakout;
public NotificationManager events;
public Stock() {
this.events = new NotificationManager();
}
public void stoploss(String nseTicker) {
if (!nseTicker.isEmpty()) {
this.breakout = new Breakout(nseTicker);
events.notify(Alerts.STOPLOSS.toString(), breakout);
}
}
public void entry(String nseTicker) {
if (!nseTicker.isEmpty()) {
this.breakout = new Breakout(nseTicker);
events.notify(Alerts.ENTRY.toString(), breakout);
}
}
}
NotificationManager.java
/**
*
*/
package com.learning.designpattern.observer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* NotificationManager
*
* keeps the subscriber list ( watchlist), through NotificationManager you can subscribe or unsusbscribe for an event
*
* notify() helps to notify the subscribers/observer's
*
*
* @author bijuvas
*
*/
public class NotificationManager {
private Map<String, List<EventListener>> watchlist;
public NotificationManager() {
this.watchlist = new HashMap<>();
addEvents();
}
public void notify(String eventType, Breakout file) {
List<EventListener> users = watchlist.get(eventType);
users.forEach(user -> {
user.update(eventType, file);
});
}
public void subscribe(String eventType, EventListener listener) {
List<EventListener> users = watchlist.get(eventType);
users.add(listener);
}
public void unsubscribe(String eventType, EventListener listener) {
List<EventListener> users = watchlist.get(eventType);
users.remove(listener);
}
public void addEvents() {
final Alerts[] alerts = Alerts.values();
for (Alerts event : alerts) {
this.watchlist.put(event.toString(), new ArrayList<>());
}
}
}
EventListener.java
/**
*
*/
package com.learning.designpattern.observer;
/**
* EventListener
*
* update()
*
* @author sreekumar
*
*/
public interface EventListener {
public void update(String eventType, Breakout breakout);
}
EmailNotificationListener.java
/**
*
*/
package com.learning.designpattern.observer;
/**
* EmailNotificationListener
*
* @author perambra kozhikode
*
*/
public class EmailNotificationListener implements EventListener {
private String email;
public EmailNotificationListener(String email) {
this.email = email;
}
@Override
public void update(String eventType, Breakout breakout) {
System.out.println(" email send to "+email +" for "+eventType);
}
}
MainClass.java
/**
*
*/
package com.learning.designpattern.observer;
/**
* @author valiyakode
*
*/
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
Stock editor = new Stock();
NotificationManager event = editor.events;
event.subscribe(Alerts.STOPLOSS.toString(), new EmailNotificationListener("test@gmail.com"));
editor.stoploss("SGX");
}
}
Breakout.java
/**
*
*/
package com.learning.designpattern.observer;
/**
* @author Zamorins Guruvayoorappan college
*
*/
public class Breakout{ //implements Strategies{
public Breakout(String nseTicker) {
}
}
Alerts.java
/**
*
*/
package com.learning.designpattern.observer;
/**
* @author lijesh
*
*/
public enum Alerts {
TARGET1, ENTRY, TARGET2, STOPLOSS;
}
Full source code
Git url