Java implements Observer pattern
Observer pattern is a behavioral design pattern used to establish one to many dependencies between objects. When an object's state changes, its related dependent objects will be automatically notified and updated.
This mode includes the following roles:
1. Subject: The observed, that is, the object being observed. It maintains a list of observers that can be added or removed, and notifies observers when the state changes.
2. Observer: The observer interface defines the general methods of the observer for being called by the topic.
3. Concrete Subject: A specific class of the observed object that implements the subject interface and maintains the current state.
4. Concrete Observer: A specific observer class that implements the observer interface and updates its own state when notified.
Applicable scenarios:
1. When the state change of an object needs to be notified to multiple other objects, and the specific number of these objects is unknown in advance or changes dynamically, the Observer pattern can be used.
2. When an object needs to notify other objects of its changes, but does not want to form a tight coupling relationship with the notified object, the Observer pattern can be used.
The benefits of this design pattern are as follows:
1. Low coupling: The Observer pattern can decouple the subject and the observer, making the relationship between them loose.
2. Scalability: New observers can be added at any time, making the system easier to scale.
3. Notify at any time: When there is a change in the status of the topic, the observer can be notified at any time to ensure that the observer can update their own status in a timely manner.
The following is a simple code example for implementing the Observer pattern in Java:
import java.util.ArrayList;
import java.util.List;
//Observer Interface
interface Observer {
void update(String message);
}
//Observer Interface
interface Subject {
void attach(Observer observer);
void detach(Observer observer);
void notifyObservers(String message);
}
//Specific Observer Class
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);
}
}
//Specific categories of observed individuals
class ConcreteSubject implements Subject {
private List<Observer> observers = new ArrayList<>();
@Override
public void attach(Observer observer) {
observers.add(observer);
}
@Override
public void detach(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
public void changeState(String newState) {
System.out.println("Subject state changed to: " + newState);
notifyObservers(newState);
}
}
//Sample code
public class ObserverPatternExample {
public static void main(String[] args) {
ConcreteSubject subject = new ConcreteSubject();
Observer observer1 = new ConcreteObserver("Observer1");
subject.attach(observer1);
Observer observer2 = new ConcreteObserver("Observer2");
subject.attach(observer2);
subject.changeState("New State");
}
}
The above code represents the relationship between a Concrete Subject and two Concrete Observers. When the status of the topic changes, all observers are notified and their status is updated. Run the sample code and the output is as follows:
Subject state changed to: New State
Observer1 received message: New State
Observer2 received message: New State
The above code is just a simple example of the Observer pattern, which can be expanded and customized according to specific requirements in practical applications.