java listener design pattern (java observer design pattern)

Posted by magicmoose on Thu, 23 Dec 2021 10:35:49 +0100

Let's share the Observer today Design pattern (monitoring design mode). This mode is highly used in many mainstream frameworks and source codes. Before sharing, let's tell you a scene where we use mobile phones. We have all used mobile phones. When our mobile phones call, there will be various complex operations, such as ringing, shaking, and bright screen. Have you considered this scene How did it happen? In fact, this place uses the observer design pattern.

In the above example, if the observer design pattern is not used, how to design is more reasonable. The emergence of any design pattern has its use scenario, which is summarized through the experience of many predecessors, and the observer design pattern is no exception

Application scenario of observer mode: 1. To update the status of an object, other objects need to be updated synchronously, and the number of other objects is dynamically variable. 2. Objects only need to notify other objects of their updates without knowing the details of other objects.

definition

Observer mode (sometimes referred to as publish subscribe mode, model view mode, source listener mode or dependent mode) is a kind of software design mode. In this mode, a target object manages all observer objects that depend on it and actively notifies when its own state changes. This is usually provided by calling observers Method. This pattern is often used to implement event processing systems.

UML diagram

The observer design pattern mainly includes the following three parts:

1. Events

2. Event source

3. Observer (listener)

In the above example, event (incoming call), event source (call) and observers (corresponding objects on the screen, ring, vibration, etc.) subscribe to the call status. When the call status changes to incoming call, these observers can receive the corresponding information and then do some update operations.

Code design

CallObserverable.java

package mode.observe;

/**
 * @author hongtaolong
 * Abstract observed interface
 */
public interface CallObserverable {
	public void registerObserver(CallStateListener o);//subscribe
    public void removeObserver(CallStateListener o);//Delete subscription
    public void notifyObserver();//notice
}

CallStateListener.java

package mode.observe;

/**
 * @author hongtaolong
 * call Interface for status monitoring
 */
public interface CallStateListener {

	void update();
}

CallRing.java

package mode.observe;

/**
 * @author hongtaolong
 * Ring related classes
 */
public class CallRing implements CallStateListener {

	@Override
	public void update() {
		// TODO Auto-generated method stub
		System.out.println("There's a call. I'm going to ring");
	}

}

CallScreen.java

package mode.observe;

/**
 * @author hongtaolong
 * Ring related classes
 */
public class CallScreen implements CallStateListener {

	@Override
	public void update() {
		// TODO Auto-generated method stub
		System.out.println("There's a call. I want to light up the screen");
	}

}

CallShock.java

package mode.observe;

/**
 * @author hongtaolong
 * Vibration related classes
 */
public class CallShock implements CallStateListener {

	@Override
	public void update() {
		// TODO Auto-generated method stub
		System.out.println("There's a call. I'm shaking");
	}

}

CallState.java

package mode.observe;

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

/**
 * @author hongtaolong
 * call State class
 */
public class CallState implements CallObserverable{

	private int state;//call status
	private List<CallStateListener> mCallListeners;
	
	public CallState() {
		// TODO Auto-generated constructor stub
		mCallListeners = new ArrayList<>();
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
		//For the convenience of simply changing the call status here, set when state = 2 to indicate an incoming call
		if(state == 2) {
			notifyObserver();//Notify the observer
		}
	}
	@Override
	public void registerObserver(CallStateListener o) {
		// TODO Auto-generated method stub
		mCallListeners.add(o);
		
	}
	@Override
	public void removeObserver(CallStateListener o) {
		// TODO Auto-generated method stub
		mCallListeners.remove(o);
	}
	@Override
	public void notifyObserver() {
		// TODO Auto-generated method stub
		for(CallStateListener listener : mCallListeners) {
			listener.update();
		}
	}
	
	
}

CallClient.java

package mode.observe;

/**
 * @author hongtaolong
 * client
 */
public class CallClient {

	public static void main(String[] args) {
		CallStateListener shock = new CallShock();
		CallStateListener screen = new CallScreen();
		CallStateListener ring = new CallRing();
		CallState callState = new CallState();
		callState.registerObserver(screen);//Status of screen subscription call
		callState.registerObserver(shock);//Shocksubscription call status
		callState.registerObserver(ring);//ring subscription call status
		callState.setState(2);//Analog 2 indicates an incoming call
	}
}

Operation results:

Is the observer design pattern very simple? If there are mistakes in this article, please point them out. Thank you!