Biography: Swing unruly, love life. Java Cultivator (wechat official ID: Java Cultivator), welcome to follow. Access to 2000G of detailed information on the 2020 interview questions
Design patterns: Design patterns are solutions to problems. Learning from existing design patterns can reuse experience. Having a design pattern vocabulary allows you to communicate in fewer words and without needing to understand the low-level details.
Observer design pattern
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.
When we play a team game, what do you do when you get attacked and need help from your teammates?
Send a message to all your teammates that you are being attacked. All your teammates will react to the message you send. Like having a sense of teamwork to help you, or not to help you keep playing your own.
The team members are the observers in the name of the design pattern. So what’s the one that’s being attacked. Observed? No, exactly what we call goals or themes.
So the whole process is basically: when the status of the target (topic) is sent to change, the observer will be notified, and the observer will make corresponding actions according to their own situation.
import java.util.Observable; /** * @author gaotian * @description The concrete target class Subject inherits the abstract class and implements methods to notify observers * it has its own state and notifies observers when its state changes. * @date 9:01 2020/6/23 **/ public class Subject extends Observable {/** * Observable this is an Observable. We define a collection of observers in the theme. * An observer object can receive any number of observers. It also provides a series of methods for managing these observers. **/ ** * @author gaotian * @description // Enable change status * @date 15:45 2020/6/23 **/ @override protected synchronized void setChanged() { super.setChanged(); } } import java.util.Observable; import java.util.Observer; /** * @author gaotian * @description * A reference to a specific target object is maintained in a specific observer. It stores the state of the specific observer, and these states should be consistent with the state of the specific target. * * It implements the updata method of the abstract observer object. * * Usually when implemented, you can call the attach and detach methods of the specific target to add or remove yourself from the collection. * @Date 9:06 2020/6/23 **/ public class ConcreteObserver implements Observer { private String observerName; public ConcreteObserver(String observerName) { this.observerName = observerName; } @override public void update(Observable, Object arg) {system.out.println (observerName +......)} @override public void update(Observable, Object arg) {system.out.println (observerName +......) ); System.out.println(" target class: "+ o); System.out.println(" argument = "+ arg); } } import java.util.Observable; import java.util.Observer; /** * @author gaotian * @description Second concrete observer, * @date 9:09 2020/6/23 **/ Public Class ConcreteOberverOther implements Observer {private String observerName; public ConcreteOberverOther(String observerName) { this.observerName = observerName; } @override public void update(Observable, Object arg) {system.out.println (observerName +......)} @override public void update(Observable, Object arg) {system.out.println (observerName +......) ); System.out.println(" target class: "+ o); System.out.println(" argument = "+ arg); } } import java.util.Observer; public class NotifyMain { public static void main(String[] args) { Subject subject = new Subject(); subject.setChanged(); Observer Observer = new ConcreteObserver(" ConcreteObserver "); Observer observer2 = new ConcreteOberverOther(" ConcreteOberverOther "); // Add all observers to the observer list subject.addobServer (observer); subject.addObserver(observer2); Boolean hasChanged = subject.haschanged (); System.out.println("hasChanged = " + hasChanged); subject.notifyObservers("123"); (1) The observer pattern can achieve the separation of the presentation layer and the data logic layer, define a stable messaging mechanism, and abstract the update interface, so that a variety of presentation layers can act as concrete observers. * * (2) The observer mode establishes an abstract coupling between the observed target and the observer. An observer object only needs to maintain a collection of abstract observers without knowing anything about its specific observers. * * (3) The observer mode supports broadcast communication, and the observation target will send notification to all registered observers, reducing the design difficulty of one-to-many system. * * (4) The observer mode meets the requirements of the open and closed principle, and it is unnecessary to modify the original system code to add a new specific observer. (1) If an observation object has many direct and indirect observers, it will take a lot of time for all observers to receive messages. * * (2) If there is a cyclic dependency between the observer and the observed, the observation target will trigger a cyclic call between them, which may lead to system crash. * * (3) The observer mode has no corresponding mechanism to let the observer know how the observed object changes, but only know that the observed object changes. / * *}Copy the code
1. Main advantages
(1) Observer mode can realize the separation of presentation layer and data logic layer, define a stable messaging mechanism, and abstract the update interface, so that a variety of presentation layer can act as a specific observer. (2) The observer mode establishes an abstract coupling between the observation target and the observer. An observer object only needs to maintain a collection of abstract observers without knowing anything about its specific observers. (3) The observer mode supports broadcast communication, and the observation target will send notification to all registered observers, reducing the design difficulty of one-to-many system. (4) The observer mode meets the requirements of the open and closed principle, and it is unnecessary to modify the original system code to add a new specific observer. 2. Main disadvantages (1) If an observation object has many direct and indirect observers, it will take a lot of time for all observers to receive messages. (2) If there is cyclic dependence between observer and observed, the observation target will trigger cyclic calls between them, which may lead to system crash. (3) The observer mode has no corresponding mechanism to let the observer know how the observed object changes, but only know that the observed object changes.