Definition
Observer mode (Publish and Subscribe mode) - Define one-to-many dependencies between objects, so that when an object changes state, the objects that depend on it are notified and updated automatically.
Use scenarios
- In the context of association behavior scenarios, it should be noted that the association behavior is separable rather than a "combination" relationship.
- Event multi-level trigger scenario.
- Cross-system message exchange scenarios, such as message queues, event bus processing mechanisms.
UML class diagram
- Subject: An Abstract topic, the role of the Observable, the abstract subject role stores references to all observer objects in a collection, and each topic can have any number of observers. Abstract topics provide an interface to add and delete observer objects.
- ConcreteSubject: A specific topic, which stores the relevant state in a specific observer object and sends notifications to all registered observers when the internal state of the specific topic changes.
- Observer: An Abstract observer. All potential observers must implement this interface. It defines a method of updating the interface, which is called when the subject state changes, so that they can update themselves.
- ConcreteObserver: A concrete observer that implements an update interface defined by an abstract observer to update itself when the subject state changes.
Example
The blog subscription function is used to describe the observer pattern.
public interface Subject {
public void register(Observer o);
public void remove(Observer o);
public void notify(Object arg);
}
public interface Observer {
public void update(Object arg);
}
public class Developer implements Observer {
public String name;
public Developer(String name) {
this.name = name;
}
@Override
public void update(Object arg) {
System.out.println("Hi,"+name+",New blog posts have been published:"+arg);
}
}
public class BlogSubject implements Subject {
private ArrayList<Observer> mObservers;
public BlogSubject() {
mObservers = new ArrayList<>();
}
@Override
public void register(Observer o) {
mObservers.add(o);
}
@Override
public void remove(Observer o) {
int i = mObservers.indexOf(o);
if (i >= 0) {
mObservers.remove(i);
}
}
@Override
public void notify(Object arg) {
for(Observer o : mObservers) {
o.update(arg);
}
}
public void postNews(String content) {
notify(content);
}
}
Client call:
public class Test {
public static void main(String[] args) {
//Instance of the Observed
BlogSubject blog = new BlogSubject();
//Observer instantiation
Developer dev1 = new Developer("dev 1");
Developer dev2 = new Developer("dev 2");
Developer dev3 = new Developer("dev 3");
//Observers are registered in the Observer List
blog.register(dev1);
blog.register(dev2);
blog.register(dev3);
//Publish news
blog.postNews("Learning Notes on Design Patterns: Observer Patterns");
}
}
summary
The main function of the observer pattern is to decouple the object, completely isolate the observer from the observee, and only rely on the abstraction of Subject and Observer.
Advantage
Objects are abstractly coupled, and their business changes do not affect each other.
shortcoming
We need to consider the development efficiency and operation efficiency. In Java, message notification is executed sequentially by default. An observer is too time-consuming, which will affect the overall execution efficiency. In this case, asynchronous processing is considered.
This blog is a reading note:
Head First Design Patterns
Dahua Design Model
Android Source Code Design Patterns Analysis and Practice