Java Q & A: Using OBServer mode

xiaoxiao2021-03-06  78

Author Blog: http://blog.9cbs.net/lostmouse/ A: I want to use OBServer mode in your Java program. Based on this, can I provide some sample code to demonstrate how to do it? Q: As the object-oriented programming helps the code multiplex, the design mode can facilitate design multiplexing. Indeed, the design pattern allows you to reuse the correct, mature design. But recently, the voice of criticism design model is getting more and more. The critics pointed out that the lack of experience will easily fall into the "mode trap". The pattern traps make the lack of experience developers lost the direction. Thus, they are not to find the best solution that may exist when dealing with the problem, and focusing the final target above more design patterns. In some people, using design patterns will inevitably bring good design. According to this logic, just use a large number of design patterns, you will inevitably produce an excellent design! However, in reality, this view has caused many meaningless designs - even if this design uses multiple design patterns. It seems unfortunate that the design model does not guarantee a good design. To properly use a design pattern correctly in your design, you must ensure three conditions: 1. Find your question 2. Understand this mode 3. Understand how this mode solves your problem First, the most important thing is the condition 1. If you can't fully understand the problem you want to solve, do you talk about the mode of use? Also knowing the condition 2: You must fully understand the mode you want to use. Don't understand how it can use it? More importantly, even don't know anything, how do you think of it with it? The last point, if you can't clear how the pattern will solve your problem (why this mode is appropriate), then give up it. Just use it to use the pattern itself, it will fall into the pattern trap. I am not saying that readers who will mention this issue will definitely fall into the pattern trap. But from the expression of questions, it is easy to mislead some developers to understand the design model. My understanding of this question is that this reader should know what I need to solve and understand the OBServer mode. He / she just doesn't know how to use Java to be implemented. Before giveing ​​a Java example, in order to help other readers can understand, first briefly introduce OBServer mode. Simply, the OBServer mode allows an object (observers, observer) to monitor another object (target, subject); it makes the relationship between the target and the observer (publish-subscribe) . Through Observer mode, the observer can register the target indicating that it is going to receive an event from the target. When the goal needs to notify the observer, it simply sends the event to each observer. For example, there is a spreadsheet based on some kind of data model. As long as the data model changes, the spreadsheet needs to update the table unit and the embedded chart. In this example, the target is a data model, and the observer is a table unit and a chart. They update themselves when the observers receive a notice that the data model has changed. The benefit of the OBServer mode is: it relieves the coupling relationship between the observer and the target. The goal does not need to know any information about its observer. Instead, the target is only allowed to subscribe to the event. When the target generates an event, it simply passes the event to each observer.

Take a look at the Java example: public interface subject {public void address (Observer O); public void removeobserver (Observer O);} Subject interface defines two methods, each subject must be implemented They so that OBServer can add or delete themselves in Subject. Public interface Observer {public void update (Subject O);} Observer interface (as above) lists a method (Method), each OBServer must implement it so that Subject can send update messages to Observer. Let's look at a simple implementation of a 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 itrator () {return list.iterator ();} public integer remove (int index) {if (INDEX

Look two implementations --IntegerAdder and Observer 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; item 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 (IntegerDatabase) {this.bag = BAG; Bag.addobserver (this);} public void update (Subject O) {if (o == Bag) {system.out.println ("The Contents of the IntegerDatabase"; System.out.Println ("The Content.Println (" THE New Contents of The IntegerDataBag Contains: "); Iterator i = Bag.Iterator (); while (i.hasnext ()) {system.out.println (i.next ());}}}}}}} Integeradder and IntegerPrinter will own yourself As an observer to INTEGERDATABAG. When the Integeradder receives a update message, it first statistics the total number of BAGs and then display the result.

Similarly, when the IntegerPrinter receives a new update message, it prints Interger in the BAG.

Here is a simple main (), it uses several classes above: 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 (4); 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 .out.println ("About to add another integer to the bag:"); Bag.Add (i9); system.out.println (""); system.out.println ("About To Remove an in Teger from the bag: "); Bag.Remove (0);}} Run Main, you will see: c: / javaworld / java driverabout to add annother integer to the bag: the contents of the integerdatabases, changed.the new sum of the intergers is: 45The contents of the IntegerDataBag have changed.The new contents of the IntegerDataBag contains: 123456789 About to remove an integer from the bag: The contents of the IntegerDataBag have changed.The new sum of the intergers is: 44The contents INTEGERDATABAG / INTEGERDDERDATABAG CONTAINS: 23456789 IntegerDatabase: 23456789 IntegerDatabase: 23456789 IntegerDatabase: 23456789 IntegerPrinter is a very simple example of applying Observer mode.

转载请注明原文地址:https://www.9cbs.com/read-92288.html

New Post(0)