In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand the java Observer Mode". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The observer pattern defines an one-to-many relationship between objects, allowing one or more observer objects to observe a subject object. When the state of the subject object changes, the system can notify all observers who depend on the object, so that the observer object can be updated automatically.
In the observer mode, the observed object is often called the target or subject (Subject), and the dependent object is also called the observer (Observer). The following is a simple example to demonstrate the observer mode. The program first provides an observer interface:
/ / Observer interface public interface Observer {public void update (Observable o, Object obj);}
The above Observer interface is an observer interface, which should be implemented by all observers in the program. A parameter of type Observable is included in the update () method of the interface, which represents the object being observed, that is, the target or topic described earlier. Here Observable is an abstract base class that should be inherited by the observer in the program. Here is the code for the Observable abstract class:
/ / the base class of the target object, public abstract class Observable {/ / uses a List to hold all bound observers on the object List observers = new ArrayList (); / / this method is used to register the observer public void registObserver (Observer observer) {observers.add (observer) from the target object. } / / remove the observer public void removeObserver (Observer ovserver) {observers.remove (observer) bound on this object } / / notify all observers on the target object public void notifyObservers (Object value) {/ / traverse the observer collection and update each observer for (Observer observer: observers) {observer.update (this, value);}
The Observer abstract class, which is the base class for all observers, mainly provides a registObserver () method for registering a new observer, and a removeObserver () method to remove a bound observer. When the state of a specific observed object changes (such as in the setter method), notifyObservers is called to notify all observers.
Here is a specific target object that inherits the above Observable base class:
Public class Product extends Observable {/ / define two properties private String name; private double price; / / nonparametric constructor public Product () {} / / with parametric constructor pubilc Product (String name, double price) {this.name = name; this.price = price;} public String getName () {return name } public double getPrice () {return price;} / / when the name property of Product is changed, the program triggers all observers registered on the object public void setName (String name) {this.name = name; notifyObservers (name) } / / when the price property of Product changes, the program also triggers all observers registered on the object public void setPrice (double price) {this.price = price; notifyObservers (price);}}
As the code in the program shows, when the program calls the setName and setPrice methods of Product to change the name and price properties of Product, these two methods automatically trigger the notifyObservers () method of the Observable base class.
Next, two observers are provided, one to observe the name property of Product, and the other to observe the price property of Product:
Public class NameObserver implements Observer {/ / update method @ Override public void update (Observable o, Object obj) {if (obj instanceof String) {String name = (String) obj; System.out.println ("NameObserver:" + o + ", Name of Product has been changed to" + name);}}
PriceObserver.java:
Public class PriceObserver implements Observer {/ / update method @ Override public void update (Observable o, Object obj) {if (obj instanceof Double) {double price = (Double) obj; System.out.println ("PriceObserver:" + o + ", Price of Product has been changed to" + price);}}
Finally, create a Product object (that is, the target object), and then bind two observers to the object:
Public void Test {public static void main (String [] args) {/ / create a target object Product p = new Product ("table", 34.56); / / create two observers NameObserver nameObserver = new NameObserver (); PriceObserver priceObserver = new PriceObserver (); / / bind the two observers to the target object p.registObserver (nameObserver) P.registObserver (priceObserver); / / now call the set method of the target object to modify the attribute value p.setName ("bench"); p.setPrice (12.34);}}
Running the above program, you will see that when the property value of the Product changes, the NameObserver and PriceObserver registered on the Product will be triggered.
Looking at the observer model described above, we find that it usually contains four roles:
The observed (that is, the target object) abstracts the base class (that is, Observable above): it usually holds references to multiple observer objects. Java provides the java.util.Observable base class to represent the abstract base class of the observed, so there is no need to develop this role yourself in actual development.
Observer interface (that is, Observer above): this interface is the interface that all observed objects should implement, usually containing only one abstract method, update. Java also provides a java.util.Observer interface to represent the observer interface, so there is no need to develop this role yourself in actual development.
The implementation class of the target object (that is, Product above): this class inherits from the Observable base class
Implementation classes of the observer object (that is, NameObserver and PriceObserver above): these classes implement the Observer interface and implement update abstract methods
That's all for "how to understand the java Observer Mode". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.