Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java Observer pattern example Code Analysis

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this "Java Observer Mode example Code Analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java Observer Mode example Code Analysis" article.

Advantages

1. The observer and the observed are abstract and coupled.

two。 Establish a trigger mechanism.

Shortcoming

1. If an observed object has many direct and indirect observers, it will take a lot of time to notify all the observers.

two。 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 cause the system to crash.

3. The observer model has no corresponding mechanism to let the observer know how the observed object has changed, but only that the observed object has changed.

Working with scen

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.

The change of one object will lead to the change of one or more other objects, but we don't know how many objects will change, which can reduce the coupling between objects.

An object must notify other objects without knowing who those objects are.

You need to create a trigger chain in the system, the behavior of An object will affect B object, and the behavior of B object will affect C object. You can use observer mode to create a chained trigger mechanism

Matters needing attention

There are already support classes for the Observer pattern in 1.JAVA.

two。 Avoid circular references.

3. If executed sequentially, an observer error will cause the system to jam, usually in an asynchronous manner.

1. Observer abstract class package com.asurplus.common.observe;/** * Observer * / public abstract class Observer {protected Subject subject; / * release event * / public abstract void update ();} 2. The first observer package com.asurplus.common.observe;import lombok.extern.slf4j.Slf4j;@Slf4jpublic class Observer1 extends Observer {public Observer1 (Subject subject) {this.subject = subject This.subject.attach (this);} @ Override public void update () {log.info ("Observer 1 received notification: {}", this.subject.getState ());}} 3, second observer package com.asurplus.common.observe;import lombok.extern.slf4j.Slf4j;@Slf4jpublic class Observer2 extends Observer {public Observer2 (Subject subject) {this.subject = subject; this.subject.attach (this) } @ Override public void update () {log.info ("Observer 2 received notification: {}", this.subject.getState ());}} 4, third observer package com.asurplus.common.observe;import lombok.extern.slf4j.Slf4j;@Slf4jpublic class Observer3 extends Observer {public Observer3 (Subject subject) {this.subject = subject; this.subject.attach (this) } @ Override public void update () {log.info ("Observer 3 received notification: {}", this.subject.getState ());}} 5. Define topic package com.asurplus.common.observe;import java.util.ArrayList;import java.util.List;/** * topic * / public class Subject {/ * Observer list * / private List observers = new ArrayList () / * Flag * / private int state; public int getState () {return state;} / * the logo has been changed * * @ param state * / public void setState (int state) {this.state = state; / / notify all observers notifyAllObservers () } / * add to recipient list * * @ param observer * / public void attach (Observer observer) {observers.add (observer);} / * notify everyone that it has been changed * / public void notifyAllObservers () {for (Observer observer: observers) {observer.update ();}

Similar to message publishing, there are multiple recipients who notify multiple recipients when the flag bit is changed.

Test package com.asurplus.common.observe;public class TestMain {public static void main (String [] args) {/ / create the topic Subject subject = new Subject (); / / add Observer new Observer1 (subject); new Observer2 (subject); new Observer3 (subject); / / change the flag subject.setState (10);}}

Output result

As you can see, all three recipients received event notifications, thus implementing our observer pattern.

The above is about the content of the article "Java Observer Mode example Code Analysis". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report