Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

define

The Observer Pattern is used when there is a one-to-many relationship between objects. For example, when an object is modified, dependent objects are automatically notified. The observer model belongs to the behavioral model.

Defines a one-to-many dependency between objects in which all dependent objects are notified and automatically updated when an object’s state changes.

The advantages and disadvantages

Advantages: 1. The observer and the observed are abstractly coupled. 2. Build a trigger mechanism.

Disadvantages: 1. If an observed object has many direct and indirect observers, it can take a long time to notify all observers. 2. If there is a cyclic dependency between the observer and the observing target, the observing target will trigger a cyclic call between them, possibly causing the system to crash. 3. The observer mode has no corresponding mechanism to let the observer know how the observed object has changed, but only to know that the observed object has changed.

Usage scenarios

  • An abstract model has two aspects, one of which depends on the other. Encapsulate these aspects in separate objects so that they can be changed and reused independently.
  • A change in one object will cause one or more other objects to change, and not knowing exactly how many objects will change can reduce the coupling between objects.
  • One object must notify other objects without knowing who they are.
  • You need to create A chain of triggers in the system where the behavior of object A affects object B, and the behavior of object B affects object C… You can use observer mode to create a chain trigger mechanism.

implementation

The Observer mode consists of the following four roles

  • Subject: An interface to abstract a topic through which an object registers itself as an observer or removes itself from the observer

  • ConcreteSubject: A concrete theme that implements the topic interface. In addition to implementing add and remove methods, it also has an update method that notifies all observers of state changes

  • Observer: An Observer interface that is implemented by all specific observers

  • ConcreteObserver: ConcreteObserver

// Observer interface
public interface Observer {
    // Process the business logic
    void update(String message);
}
Copy the code
// Concrete observer
class ConcreteObserver implements Observer {
    String name;

    // We just want to validate multiple observers
    public ConcreteObserver(String name) {
        this.name = name;
    }

    @Override
    public void update(String message) {
        // Simulate processing business logic
        System.out.println(name + "I see you updated it."+ message); }}Copy the code
// Abstract the topic's interface
public interface Subject {
    // Add a subscription
    void addObserver(Observer observer);

    // Remove the subscription
    void removeObserver(Observer observer);

    // Notify subscribers
    void notifyObservers(String message);
}
Copy the code
// Specific topics
public class ConcreteSubject implements Subject {

    // Subscriber container
    private List<Observer> observers = new ArrayList<Observer>();

    @Override
    public void addObserver(Observer observer) {
        // Add a subscription
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        // Remove the subscription
        observers.remove(observer);
    }

    @Override
    public void notifyObservers(String message) {
        // Notify subscribers
        for(Observer observer : observers) { observer.update(message); }}}Copy the code

validation

class Test {
    public static void main(String[] args) {

        ConcreteSubject subject = new ConcreteSubject();
        // Add 2 observers here
        ConcreteObserver concreteObserver1 = new ConcreteObserver("Observer 1");
        subject.addObserver(concreteObserver1);
        subject.addObserver(new ConcreteObserver("Observer 2"));

        // Notify the observer to update the data
        subject.notifyObservers("The first time");
        // Output result:
        // Observer 1 knows that you updated the first time
        // Observer 2 knows you updated it for the first time

        // Delete the first observer test
        subject.removeObserver(concreteObserver1);
        // Notify the observer to update the data (only observer 2 will receive the update)
        subject.notifyObservers("The second time");
        // Output result:
        // Observer 2 knows you updated it a second time}}Copy the code