In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to implement the observer mode in JAVA. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Concept
The observer pattern, also known as the publish-subscribe pattern, is an one-to-many relationship between objects. When the state of an object changes, all objects that depend on it are notified and updated automatically.
Composition
Abstract topic roles (Subject)
Also known as abstract target classes, abstract topics know what their observers are, provide methods to delete and add observers, and abstract methods to notify observers, implemented by abstract classes or interfaces
Abstract Observer role (Observer)
Contains an updated abstract method that is called when an update notification of a specific topic is received and is implemented by an abstract class or interface
Specific subject roles (Concrete Subject)
Also known as the specific target class, implements the notification method of the abstract target class, notifying all observers who subscribe to it when a specific topic changes.
Specific observer role (Concrete Observer)
Implement an abstract method that abstracts the role of the observer, changing its own state after receiving a notification of a subject change
Realize
Abstract Observer Observer Interface
Contains a method for the observer to receive notification to change his or her state
Public interface Observer {/ / received notification to change its status void update ();}
Specific observers ObserverOne and ObserverTwo
Implement the update method in Observer interface
Public class ObserverOne implements Observer {@ Override public void update () {System.out.println ("first observer received notification, status updated");}} public class ObserverTwo implements Observer {@ Override public void update () {System.out.println ("second observer received notification, status updated");}}
Subject interface
Contains methods to add, delete, and notify the observer, implemented by the abstract target class, and a method that operates itself, implemented by the specific target class
Public interface Subject {/ / add observer void add (Observer observer); / / delete observer void remove (Observer observer); / / notify all observers void notifyObservers (); / / self-operation void operation ();}
Abstract target class AbstractSubject
Abstract class, because the number of observers is not fixed, we use Vector dynamic array to store observers and implement the add,remove,notifyObservers method in the Subject interface. The operation method is implemented by the specific target class.
Public abstract class AbstractSubject implements Subject {Vector vector = new Vector (); @ Override public void add (Observer observer) {vector.add (observer);} @ Override public void remove (Observer observer) {vector.remove (observer);} @ Override public void notifyObservers () {for (Observer observer: vector) {observer.update ();}
Specific target class MySubject
Inherit the AbstractSubject class, implement the operation method, and call the notifyObservers method in the operation method to notify the observer after the state changes.
Public class MySubject extends AbstractSubject {@ Override public void operation () {System.out.println ("specific target status change"); System.out.println ("notifying observer.") ; notifyObservers (); System.out.println ("Notification is over!") ;}}
Test class Test
Public class Test {public static void main (String [] args) {MySubject mySubject = new MySubject (); mySubject.add (new ObserverOne ()); mySubject.add (new ObserverTwo ()); mySubject.operation ();}}
Summary
What is the main problem solved by the observer model?
Under the condition of low coupling, the state of one object changes and other objects are notified.
When will the observer mode be used
When the state of an object changes, all dependent objects will receive a broadcast notification
Advantages of the observer model
The observer and the target are abstractly coupled and low coupled, which is a set of trigger mechanisms.
The shortcomings of the observer model
① if there are many direct and indirect observers dependent on a target, it takes a lot of time to notify all observers.
② if there is a circular dependency between the observer and the observed, a circular call may occur, causing the system to crash
The ③ observer cannot know how the target state has changed, only that the target state has changed
Considerations for the Observer Model
There are already supporting classes for the Observer pattern in ① JAVA, and you can directly use the
② avoids circular calls
If ③ executes the observer's update method sequentially, an observer error will cause the system to get stuck, usually in an asynchronous way.
This is the end of the article on "how JAVA implements the Observer Mode". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.