In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use Observer mode in Java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Java Quba: using Observer mode
A: I want to use Observer mode in my Java program. Based on this, can you provide some sample code to demonstrate how to do it?
Q: just as object-oriented programming helps code reuse, design patterns can promote design reuse. Indeed, design patterns allow you to reuse correct and mature designs. But recently, there have been more and more criticisms of design patterns. Critics point out that inexperienced developers can easily fall into a "pattern trap".
Pattern traps make inexperienced developers lose their way. Therefore, instead of looking for the best possible solution when dealing with problems, they focus on achieving as many design patterns as possible. In the eyes of some people, the use of design patterns seems bound to lead to good design. According to this logic, as long as the extensive use of design patterns, it is bound to produce an excellent design for you! In reality, however, this view leads to a lot of meaningless design-even if it uses multiple design patterns. It seems regrettable that design patterns do not guarantee good design.
In order to apply a design pattern correctly in a design, three conditions must be ensured:
1. Figure out your problem.
two。 Understand this pattern.
3. Understand how this model solves your problem
First of all, the most important thing is condition 1. If you can't fully understand the problem you want to solve, how can you talk about using the model?
Also know condition 2: you must fully understand the model you want to use. How can you use it if you don't understand it? More importantly, how can you think of using a pattern if you don't know what to do with it?
Finally, if you can't clearly say how the pattern will solve your problem (why the pattern is appropriate), give it up. If you just use the pattern for its own sake, you will fall into the pattern trap.
I'm not saying that readers who ask this question are bound to fall into a pattern trap. However, from the expression of the question, it is easy to mislead some developers to understand the design pattern. My understanding of this question is that the reader should be aware of the problem he or she needs to solve and understand the Observer pattern, he or she just doesn't know how to implement it in Java.
Before giving an example of Java, to help other readers understand, let's briefly introduce the Observer pattern.
Simply put, the Observer pattern allows one object (observer, Observer) to monitor another object (target, Subject); it enables a publish-subscribe (publish-subscribe) relationship between the target and the observer. Through the Observer mode, the observer can register with the target and indicate that he or she wants to receive events from the target. When the target needs to notify the observer of the event, it simply sends the event to each observer.
For example, there is a spreadsheet based on some data model. Whenever the data model changes, the spreadsheet needs to update the table cells and embedded charts. In this example, the goal is the data model, and the observers are table units and charts. Observers update themselves when they are notified that the data model has changed.
The advantage of the Observer model is that it decouples the observer and the target. The target does not need to know anything about its observer. Instead, the goal is simply to allow the observer to subscribe to events. When the target generates an event, it simply passes the event to each observer.
Take a look at the following Java example:
Public interface Subject {
Public void addObserver (Observer o)
Public void removeObserver (Observer o)
}
In the above code, the Subject interface defines two methods (method), and each Subject must implement them so that Observer can add or remove itself from the Subject.
Public interface Observer {
Public void update (Subject o)
}
The Observer interface (above) lists a method (method) that each Observer must implement so that Subject can send update messages to Observer.
Let's take a look at a simple implementation of Subject-IntegerDataBag:
Import java.util.ArrayList
Import java.util.Iterator
Public class IntegerDataBag implements Subject {
Private ArrayList list = new ArrayList ()
Private ArrayList observers = new ArrayList ()
Public void add (Integer I) {
List.add (I)
NotifyObservers ()
}
Public Iterator iterator () {
Return list.iterator ()
}
Public Integer remove (int index) {
If (index < list.size ()) {
Integer I = (Integer) list.remove (index)
NotifyObservers ()
Return i
}
Return null
}
Public void addObserver (Observer o) {
Observers.add (o)
}
Public void removeObserver (Observer o) {
Observers.remove (o)
}
Private void notifyObservers () {
/ / loop through and notify each observer
Iterator I = observers.iterator ()
While (i.hasNext ()) {
Observer o = (Observer) i.next ()
O.update (this)
}
}
}
IntegerDataBag is suitable for situations where Integer is used. IntegerDataBag also allows Observer to add and delete themselves.
Let's take a look at two Observer implementations-- IntegerAdder and IntegerPrinter:
Import java.util.Iterator
Public class IntegerAdder implements Observer {
Private IntegerDataBag bag
Public IntegerAdder (IntegerDataBag bag) {
This.bag = bag
Bag.addObserver (this)
}
Public void update (Subject o) {
If (o = = bag) {
System.out.println ("The contents of the IntegerDataBag have changed.")
Int counter = 0
Iterator I = bag.iterator ()
While (i.hasNext ()) {
Integer integer = (Integer) i.next ()
Counter+=integer.intValue ()
}
System.out.println ("The new sum of the integers is:" + counter)
}
}
}
Import java.util.Iterator
Public class IntegerPrinter implements Observer {
Private IntegerDataBag bag
Public IntegerPrinter (IntegerDataBag bag) {
This.bag = bag
Bag.addObserver (this)
}
Public void update (Subject o) {
If (o = = bag) {
System.out.println ("The contents of the IntegerDataBag have changed.")
System.out.println ("The new contents of the IntegerDataBag contains:")
Iterator I = bag.iterator ()
While (i.hasNext ()) {
System.out.println (i.next ())
}
}
}
}
IntegerAdder and IntegerPrinter add themselves as observers to IntegerDataBag. When IntegerAdder receives an update message, it counts the total in the bag and then displays the results. Similarly, when IntegerPrinter receives an update message, it prints out the Interger in bag.
Here is a simple main () that uses the above classes:
Public class driver {
Public static void main (String [] args) {
Integer i1 = new Integer (1); Integer i2 = new Integer (2)
Integer i3 = new Integer (3); Integer i4 = new Integer (4)
Integer i5 = new Integer (5); Integer i6 = new Integer (6)
Integer i7 = new Integer (7); Integer i8 = new Integer (8)
Integer i9 = new Integer (9)
IntegerDataBag bag = new IntegerDataBag ()
Bag.add (i1); bag.add (i2); bag.add (i3); bag.add (i4)
Bag.add (i5); bag.add (i6); bag.add (i7); bag.add (i8)
IntegerAdder adder = new IntegerAdder (bag)
IntegerPrinter printer = new IntegerPrinter (bag)
/ / adder and printer add themselves to the bag
System.out.println ("About to add another integer to the bag:")
Bag.add (i9)
System.out.println ("")
System.out.println ("About to remove an integer from the bag:")
Bag.remove (0)
}
}
Run main and you will see:
C:javaworldjava Driver
About to add another integer to the bag:
The contents of the IntegerDataBag have changed.
The new sum of the intergers is: 45
The contents of the IntegerDataBag have changed.
The new contents of the IntegerDataBag contains:
one
two
three
four
five
six
seven
eight
nine
About to remove an integer from the bag:
The contents of the IntegerDataBag have changed.
The new sum of the intergers is: 44
The contents of the IntegerDataBag have changed.
The new contents of the IntegerDataBag contains:
two
three
four
five
six
seven
eight
nine
IntegerDataBag/IntegerAdder/IntegerPrinter is a simple example of applying the Observer pattern. Java itself has a lot of examples of using the Observer pattern: the AWT/swing event model, as well as java.util.Observer and java.util.Observable interfaces, are good examples.
The above is all the content of the article "how to use Observer Mode in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.
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