When there is a one to many relationship between objects, the Observer Pattern is used. For example, when an object is modified, it will automatically notify the objects that depend on it. Observer mode belongs to behavioral mode.
Intent: defines a one to many dependency between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.
It mainly solves the problem of notifying other objects of an object's state change. In addition, it should consider ease of use and low coupling to ensure a high degree of cooperation.
When to use: when the state of an object (target object) changes, all dependent objects (observer object) will be notified and broadcast.
How to solve: using object-oriented technology, this dependency can be weakened.
Key code: there is a List in the abstract class for observers.
Application scenario:
- During the auction, the auctioneer observes the highest bid price and then notifies other bidders to bid.
- In the journey to the west, Wukong asks the Bodhisattva to subdue the red boy. The Bodhisattva sprinkles water on the ground to attract an old tortoise. The tortoise is the observer. He observes the Bodhisattva's action of sprinkling water.
advantage:
- The observer and the observed are abstractly coupled.
- Establish a trigger mechanism.
Disadvantages:
- If an observed object has many direct and indirect observers, it will take a lot of time to notify all observers.
- If there is a circular dependency between the observer and the observation target, the observation target will trigger a circular call between them, which may lead to system crash.
- The observer model has no corresponding mechanism to let the observer know how the observed target object has changed, but only know that the observed target has changed.
Usage scenario:
- An abstract model has two aspects, one of which depends on the other. These aspects are encapsulated in independent objects so that they can be changed and reused independently.
- The change of one object will lead to the change of one or more other objects, and the coupling between objects can be reduced by not knowing how many objects will change.
- An object must notify other objects without knowing who they are.
- You need to create A trigger chain in the system. The behavior of object A will affect object B, and the behavior of object B will affect object C.. You can use the observer mode to create A chain trigger mechanism.
matters needing attention:
- JAVA already has support classes for the observer pattern.
- Avoid circular references.
- If the sequence is executed, an observer error will cause the system to jam, and the asynchronous mode is generally adopted.
example:
We create Subject Class, observer Abstract classes and extended abstract classes Observer Entity class.
using System.Collections.Generic; namespace Observer { public class Subject { private List<Observer> _observers = new List<Observer>(); private int _state; public int State { get => _state; set { _state = value; NotifyAllObservers(); } } public void AddObserver(Observer observer) { _observers.Add(observer); } private void NotifyAllObservers() { foreach (var observer in _observers) { observer.Update(); } } } }
using UnityEngine; namespace Observer { public abstract class Observer { protected Observer(Subject subject) { _subject = subject; _subject.AddObserver(this); } protected Subject _subject; public abstract void Update(); } public class BinaryObserver : Observer { public BinaryObserver(Subject subject) : base(subject) { } public override void Update() { Debug.Log($"Binary String: {_subject.State}"); } } public class OctalObserver : Observer { public OctalObserver(Subject subject) : base(subject) { } public override void Update() { Debug.Log($"Octal String: {_subject.State}"); } } public class HexaObserver : Observer { public HexaObserver(Subject subject) : base(subject) { } public override void Update() { Debug.Log($"Hex String: {_subject.State}"); } } }
use Subject And observer entity class objects to demonstrate observer mode.
Subject subject = new Subject(); new HexaObserver(subject); new OctalObserver(subject); new BinaryObserver(subject); subject.State = 15; subject.State = 10;
The class diagram is designed as follows: