Technology Sharing

Design Patterns - Observer Pattern

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. The core idea of ​​​​the observer pattern

The Observer pattern is also known as the Publish/Subscribe pattern. GOF defines the Observer pattern as follows: It defines a one-to-many dependency relationship between objects, allowing multiple observer objects to simultaneously monitor the same object. When the state of the object changes, all objects that depend on it are notified and automatically updated.
As shown in the figure below, the observer pattern includes 5 types of objects.
insert image description here

  • Target interface Subject: The target interface defines three interfaces: attach() for adding observers, detach() for removing observers, and notifyObservers() for notifying observers, and defines its own operation function operation().
  • AbstractSubject: This class defines the observer collection object vect, which is used to store the list of added observer objects; and implements three functions: attach(), detach(), and notifyObservers() to update the list and notify all observer objects to update themselves.
  • Specific target class MySubiect: Inherit from the abstract target class AbstractSubiect, and write a specific operation function to implement operation(), in which you can call notifyObservers() to notify all observers to update themselves.
  • Observer interface: Define a unified interface update() for objects that need to be notified when the target class changes. When notifyObservers() is called, the update() function will be called to update itself.
  • Specific Observer: You can define multiple observer objects, such as Observer1 and Observer2, to write a unified update function. The client Test can implement monitoring by adding an observer to the Subject class.

Let’s look at the specific implementation below.

(1) The observer interface Observer.java defines a unified update interface update(). Its source code is shown in the following program.

package behavior.observer;


/**
* @author Minggg
* 观察者接口
*/
public interface Observer {

	public void update();
}

(2) The observer implementation class Observer1.java is a concrete implementation of an observer. Its update function is used to output a string to the console. Its source code is shown in the following program.

package behavior.observer;


/**
* @author Minggg
* 具体观察者
*/
public class Observer1 implements Observer {

	public void update(){ 
		System.out.println("观察者1得到通知!");
	}
}

(3) The observer implementation class Observer2.java is another concrete implementation of an observer. Its update function is used to output a string to the console. Its source code is shown in the following program.

package behavior.observer;


/**
* @author Minggg
* 具体观察者
*/
public class Observer2 implements Observer {

	public void update(){ 
		System.out.println("观察者2得到通知!");
	}
}

(4) The observed interface Subject.java defines three interface functions for operating the observer, and defines a specific operation function interface to represent its own functions. Its source code is shown in the following program.

package behavior.observer;


/**
* @author Minggg
* 被观察者接口
*/
public interface Subject {

	// 增加观察者
	public void attach(Observer observer);
	// 删除观察者
	public void detach(Observer observer);
	// 通知所有观察者
	public void notifyObservers();
	// 自身的操作接口
	public void operation();
}

(5) The abstract class AbstractSubject.java provides a Vector list vect to store all observer objects and write implementation functions to operate the list object. The source code is shown in the following program.

package behavior.observer;

import java.util.Enumeration;
import java.util.Vector;


/**
* @author Minggg
* 被观察者抽象类
*/
public abstract class AbstractSubject implements Subject {

	private Vector