Small Talk on Design Patterns (15)—Observer Pattern
Column introduction
Column address
Column introduction
It mainly analyzes and summarizes the 23 common design patterns currently on the market one by one. I hope that interested friends can take a look and it will be continuously updated. I hope you can supervise me and we can learn and make progress together. Come on, everyone.
Observer pattern
Observer Pattern is a behavioral design pattern that defines a one-to-many dependency relationship that allows multiple observer objects to monitor a certain topic object at the same time. When the topic object changes, all of its Observers are notified and update themselves.
main idea
Decouple the dependencies between the observer and the observed so that they can change independently of each other. The observer only needs to know that the observer implements a certain interface, but does not need to know the specific observer class. Similarly, the observer only needs to know that the observer implements a certain interface, but does not need to know the specific observer class. .
main character
Subject
Defines the interface of the observer, including methods for registering observers, removing observers, and notifying observers.
ConcreteSubject (specific observer)
Implements the observed interface, maintains a list of observers, and notifies observers when the state changes.
Observer
Defines the observer's interface, including update methods for receiving notifications from the observed.
ConcreteObserver (concrete observer)
It implements the observer interface, specifically implements the update method, and performs corresponding processing when receiving notifications from the observed.
Java program implementation
// 定义观察者接口
interface Observer {
void update(String message);
}
// 定义被观察者接口
interface Subject {
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers(String message);
}
// 具体观察者类
class ConcreteObserver implements Observer {
private String name;
public ConcreteObserver(String name) {
this.name = name;
}
@Override
public void update(String message) {
System.out.println(name + " received message: " + message);
}
}
// 具体被观察者类
class ConcreteSubject implements Subject {
private List<Observer> observers = new ArrayList<>();
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
// 测试代码
public class ObserverPatternExample {
public static void main(String[] args) {
ConcreteSubject subject = new ConcreteSubject();
Observer observer1 = new ConcreteObserver("Observer 1");
Observer observer2 = new ConcreteObserver("Observer 2");
subject.registerObserver(observer1);
subject.registerObserver(observer2);
subject.notifyObservers("Hello, observers!");
subject.removeObserver(observer2);
subject.notifyObservers("Observer 2 has been removed!");
}
}
Output results
Observer 1 received message: Hello, observers!
Observer 2 received message: Hello, observers!
Observer 1 received message: Observer 2 has been removed!
program analysis
In the above example, we defined an observer interface (Observer) and an observed interface (Subject). The concrete observer class (ConcreteObserver) and the concrete observed class (ConcreteSubject) implement the corresponding interfaces.
Advantages and Disadvantages Analysis
advantage
decoupling
The observer pattern can decouple the dependency between the observer and the observed so that they can change independently. When the observed object changes, you only need to notify the observer, without knowing which observers exist.
Scalability
The observer pattern makes it easy to add new observers without modifying the code of the observed. This complies with the open-closed principle, making the system more flexible and scalable.
one-to-many relationship
The observer pattern can implement one-to-many dependency, and an observed object can have multiple observers. This can easily implement functions such as event monitoring and message subscription.
shortcoming
Too many observers
When there are too many observers, it may take a long time for the observed to notify the observer, which affects the performance of the system.
circular dependency
If there is a cyclic dependency between the observer and the observed, it may cause problems in the system, such as deadlock.
Update order issue
The update order of observers in the observer pattern is undefined, which may cause problems with dependencies between observers.
Summarize
It can improve the flexibility and scalability of the system. But at the same time, we also need to pay attention to issues such as too many observers, circular dependencies, and update order to ensure the stability and performance of the system. When using the observer pattern, trade-offs and designs need to be made based on specific scenarios and needs.