Simple observer pattern implementation
Copy the code code as follows:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* Callbacks are used in the observer mode:
* A. The observer registers himself to the listener list of the observed, and the observer class itself provides a callback function
* B. The Observable (Observable or Subject) maintains a list of observers and can register and unregister observers
* C. Once the status of the observer changes, it can call notifyObservers(). This method will traverse the observer list and call it one by one.
callback function provided by the observer
* @author will
*
*/
public class SimpleObserverPattern {
public static void main(String[] args) {
SimpleObserverPattern sop = new SimpleObserverPattern();
List<IObserver> observers = new ArrayList<IObserver> ();
IObserver observerA = sop.new Observer("ObserverA");
IObserver observerB = sop.new Observer("ObserverB");
observers.add(observerA);
observers.add(observerB);
IObservable observable = sop.new Observable(observers);
observable.registerObserver(sop.new Observer("ObserverC"));
observable.changeState();
observable.close();
}
// The person being observed is called Subject in some places.
interface IObservable {
void registerObserver(IObserver observer);
void unregisterObserver(IObserver observer);
void notifyObservers();
String getState();
void changeState();
void close();
}
class Observable implements IObservable {
private static final String NEW = "New";
private static final String CHANGED = "Changed";
private static final String CLOSED = "Closed";
private String state;
private List<IObserver> observers;
public Observable() {
this(null);
}
public Observable(List<IObserver> observers) {
if(observers == null) {
observers = new ArrayList<IObserver> ();
}
this.observers = Collections.synchronizedList(observers);
this.state = NEW;
}
@Override
public void registerObserver(IObserver observer) {
observers.add(observer);
}
@Override
public void unregisterObserver(IObserver observer) {
observers.remove(observer);
}
@Override
public void notifyObservers() {
Iterator<IObserver> iter = observers.iterator();
while(iter.hasNext()) {
iter.next().update(this);
}
}
@Override
public String getState() {
return state;
}
@Override
public void changeState() {
this.state = CHANGED;
notifyObservers();
}
@Override
public void close() {
this.state = CLOSED;
notifyObservers();
}
}
interface IObserver {
void update(IObservable observalbe);
}
class Observer implements IObserver {
private String name;
public Observer(String name) {
this.name = name;
}
@Override
public void update(IObservable observalbe) {
System.out.println(
String.format("%s receive observalbe's change, current observalbe's state is %s",
name, observalbe.getState()));
}
}
}
The above implementation directly uses the observed object as a callback function parameter. This is very inelegant and may work in simple scenarios.
But in fact, in more cases, an observed person has many events or states, and each observer may be interested in different events or states, or for the purpose of information hiding, we do not want every observer to be able to Access all states inside the Observable.
In this way, I continued to evolve the code into the following version. Note that I did not consider concurrency issues in detail here.
Copy the code code as follows:
import java.util.Collections;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
public class MultiEventObserverPattern {
public static void main(String[] args) {
MultiEventObserverPattern meop = new MultiEventObserverPattern();
IObservable observable = meop.new Observable();
IObserver observerA = meop.new Observer("ObserverA");
IObserver observerB = meop.new Observer("ObserverB");
//Register events of interest
observable.registerObserver(observable.getEventA(), observerA);
observable.registerObserver(observable.getEventB(), observerB);
//Change the observed state
observable.changeStateA();
observable.changeStateB();
}
interface IEvent {
void eventChange();
String getState();
}
class EventA implements IEvent {
private static final String INITIALIZED = "Initialized";
private static final String PENDING = "Pending";
private String state;
public EventA() {
this.state = INITIALIZED;
}
@Override
public void eventChange() {
System.out.println("EventA change");
this.state = PENDING;
}
@Override
public String toString() {
return "EventA";
}
@Override
public String getState() {
return state;
}
}
class EventB implements IEvent {
private static final String NEW = "New";
private static final String IDLE = "Idle";
private String state;
public EventB() {
this.state = NEW;
}
@Override
public void eventChange() {
System.out.println("EventB change");
this.state = IDLE;
}
@Override
public String toString() {
return "EventB";
}
@Override
public String getState() {
return state;
}
}
// Observable, sometimes called Subject
interface IObservable {
void registerObserver(IEvent event, IObserver observer);
void unregisterObserver(IEvent event, IObserver observer);
// Notify observers that an event has occurred
void notifyObservers(IEvent event);
void changeStateA();
void changeStateB();
IEvent getEventA();
IEvent getEventB();
}
class Observable implements IObservable {
private IEvent eventA;
private IEvent eventB;
private Hashtable<IEvent, Set<IObserver>> eventObserverMapping;
public Observable() {
this(null);
}
// If some of the Set<IObserver> passed in by evenObserverMapping have not been modified synchronously, there is nothing you can do about it.
public Observable(Hashtable<IEvent, Set<IObserver>> eventObserverMapping) {
if(eventObserverMapping == null) {
eventObserverMapping = new Hashtable<IEvent, Set<IObserver>> ();
}
this.eventObserverMapping = new Hashtable<IEvent, Set<IObserver>> ();
this.eventA = new EventA();
this.eventB = new EventB();
}
@Override
public void registerObserver(IEvent event, IObserver observer) {
Set<IObserver> observers = eventObserverMapping.get(event);
if(observers == null) {
observers = Collections.synchronizedSet(new HashSet<IObserver> ());
observers.add(observer);
eventObserverMapping.put(event, observers);
}
else {
observers.add(observer);
}
}
@Override
public void unregisterObserver(IEvent event, IObserver observer) {
Set<IObserver> observers = eventObserverMapping.get(event);
if(observers != null) {
observers.remove(observer);
}
}
@Override
public void notifyObservers(IEvent event) {
Set<IObserver> observers = eventObserverMapping.get(event);
if(observers != null && observers.size() > 0) {
Iterator<IObserver> iter = observers.iterator();
while(iter.hasNext()) {
iter.next().update(event);
}
}
}
@Override
public void changeStateA() {
// Changing state A will trigger event A
eventA.eventChange();
notifyObservers(eventA);
}
@Override
public void changeStateB() {
// Changing state B will trigger event B
eventB.eventChange();
notifyObservers(eventB);
}
@Override
public IEvent getEventA() {
return eventA;
}
@Override
public IEvent getEventB() {
return eventB;
}
}
interface IObserver {
void update(IEvent event);
}
class Observer implements IObserver {
private String name;
public Observer(String name) {
this.name = name;
}
@Override
public void update(IEvent event) {
System.out.println(
String.format("%s receive %s's change, current observalbe's state is %s",
name, event, event.getState()));
}
}
}
It seems to be perfect, but it's still not perfect. Because events are hard-coded as properties of the Observer class. In this way, the event type is determined at compile time. If you want to add a new event type, you have to modify the IObservable interface and Observable class, which greatly reduces flexibility.
It is equivalent to the observer being coupled to these specific events, so how do we break this limitation?
The answer is to introduce a new component and let that component manage the relationship between events, observers, and observed objects. When an event occurs, that component will also call the observer's callback function. This is also a kind of decoupling, somewhat similar to Spring's IOC container.
As for the specific implementation, I think Guava EventBus has done a pretty good job. You can refer to the link I mentioned earlier.
PS: This post is not an advertisement for Guava EventBus, but my own ideas are being advanced step by step, and gradually they are consistent with the design ideas of Guava EventBus.
Let's continue to look at the example of the JDK standard class implementing the observer pattern, and then analyze its source code implementation. All we need to look at is an Observable class and an Observer interface.
JDK standard classes implement observer pattern
Copy the code code as follows:
import java.util.Observable;
import java.util.Observer;
/**
* Implement the observer pattern using standard classes in the java.util package
* @author will
*
*/
public class JDKObserverDemo {
public static void main(String[] args) {
JDKObserverDemo jod = new JDKObserverDemo();
// Observer
MyObservable myObservable = jod.new MyObservable("hello");
// Observer
Observer myObserver = jod.new MyObserver();
// register
myObservable.addObserver(myObserver);
//Change the observed state and trigger the observer callback function
myObservable.setValue("will");
}
class MyObservable extends Observable {
private String watchedValue; // the observed value
public MyObservable(String watchedValue) {
this.watchedValue = watchedValue;
}
public void setValue(String newValue) {
if(!watchedValue.equals(newValue)) {
watchedValue = newValue;
setChanged();
notifyObservers(newValue);
}
}
@Override
public String toString() {
return "MyObservable";
}
}
class MyObserver implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println(o + "'s state changed, argument is: " + arg);
}
}
}
After looking at the implementation of Observer and Observable in the JDK standard library, it is very simple, so I don’t want to say more.
Below is the listener implementation in Quartz.
QuartzScheduler listener
Copy the code code as follows:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Quartz core class, equivalent to Observable (observable)
* @author will
*
*/
public class QuartzScheduler {
private ArrayList<SchedulerListener> internalSchedulerListeners = new ArrayList<SchedulerListener>(10);
//private ArrayList<JobListener> interanlJobListeners = new ArrayList<JobListener>(); // An Observable can contain multiple sets of listeners
public Date scheduleJob(Trigger trigger) {
if(trigger == null) {
return null;
}
System.out.println("Schedule job, trigger: " + trigger);
notifySchedulerListenersScheduled(trigger);
return new Date();
}
public void unScheduleJob(Trigger trigger) {
if(trigger == null) {
return;
}
System.out.println("Unschedule job, trigger: " + trigger);
notifyShedulerListenerUnScheduled(trigger);
}
//Register SchedulerListener
public void addInternalSchedulerListener(SchedulerListener schedulerListener) {
synchronized (internalSchedulerListeners) {
internalSchedulerListeners.add(schedulerListener);
}
}
// Remove SchedulerListener
public boolean removeInternalSchedulerListener(SchedulerListener schedulerListener) {
synchronized (internalSchedulerListeners) {
return internalSchedulerListeners.remove(schedulerListener);
}
}
public List<SchedulerListener> getInternalSchedulerListeners() {
synchronized (internalSchedulerListeners) {
return java.util.Collections.unmodifiableList(new ArrayList<SchedulerListener>(internalSchedulerListeners));
}
}
public void notifySchedulerListenersScheduled(Trigger trigger) {
for(SchedulerListener listener: getInternalSchedulerListeners()) {
listener.jobScheduled(trigger);
}
}
public void notifyShedulerListenerUnScheduled(Trigger trigger) {
for(SchedulerListener listener: getInternalSchedulerListeners()) {
listener.jobUnScheduled(trigger);
}
}
}
SchedulerListener
Copy the code code as follows:
// Listening interface, callback function, Client needs to provide callback function implementation when registering for monitoring
public interface SchedulerListener {
void jobScheduled(Trigger trigger);
void jobUnScheduled(Trigger trigger);
}
Trigger
Copy the code code as follows:
//Trigger
public class Trigger {
private String triggerKey;
private String triggerName;
public Trigger(String triggerKey, String triggerName) {
this.triggerKey = triggerKey;
this.triggerName = triggerName;
}
public String getTriggerKey() {
return triggerKey;
}
public void setTriggerKey(String triggerKey) {
this.triggerKey = triggerKey;
}
public String getTriggerName() {
return triggerName;
}
public void setTriggerName(String triggerName) {
this.triggerName = triggerName;
}
public String toString() {
return String.format("{triggerKey: %s, triggerName: %s}", triggerKey, triggerName);
}
}
Test
Copy the code code as follows:
public class Test {
public static void main(String[] args) {
QuartzScheduler qs = new QuartzScheduler();
SchedulerListener listenerA = new SchedulerListener() {
@Override
public void jobUnScheduled(Trigger trigger) {
System.out.println("listenerA job unscheduled: " + trigger.getTriggerName());
}
@Override
public void jobScheduled(Trigger trigger) {
System.out.println("listenerA job scheduled: " + trigger.getTriggerName());
}
};
SchedulerListener listenerB = new SchedulerListener() {
@Override
public void jobUnScheduled(Trigger trigger) {
System.out.println("listenerB job unscheduled: " + trigger.getTriggerName());
}
@Override
public void jobScheduled(Trigger trigger) {
System.out.println("listenerB job scheduled: " + trigger.getTriggerName());
}
};
//Register Scheduler Listener
qs.addInternalSchedulerListener(listenerA);
qs.addInternalSchedulerListener(listenerB);
Trigger triggerA = new Trigger("Key1", "triggerA");
Trigger triggerB = new Trigger("Key2", "triggerB");
qs.scheduleJob(triggerA);
qs.scheduleJob(triggerB);
qs.unScheduleJob(triggerA);
}
}