Java Q & A: Using OBServer mode

zhaozj2021-02-11  206

Java Q & A: Using OBServer mode

A: I want to use OBServer mode in my 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. Clear 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 below:

Public interface subject {public void addobserver (Observer O); public Void RemoveobServer (Observer O);}

In the above code, the Subject interface defines two ways, each Subject must implement them so that Observer can add or delete itself 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 take a look at the Subject's simple implementation - ITEGERDATABAG:

Import java.util.Arraylist; import java.util.ITerator;

Public class integerAbag ustements 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

Public Void Addobserver (Observer O) {Observers.Add (O);

Public Void Removeobserver (Observer O) {Observers.Remove (O);

Private void notifyobservers () {// loop throughtor i = observers.iterator (); while (i.hasnext ()) {Observer o = (observer) i.next (); o.Update (this) }}}

IntegerDataBAG is suitable for applications with Integer. IntegerDataBAG also allows Observer to increase and delete them themselves.

Take a look at the implementation of the two Observer - ITEGERADDER 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 "); int counter = 0; item i = Bag.Iterator (); while (I.hasNext ()) {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 IntegerDataBag have changed"); System.out.println ( "The new contents of the IntegerDataBag contains:" ); Itrator i = Bag.Iterator (); while (I.hasNext ()) {system.out.println (i.next ());}}}

}

Integeradder and IntegerPrinter add 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 (), which 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 (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 (I5); Bag.Add (i6); Bag.Add ( I7); Bag.Add (I8);

Integeradder Adder = New Integeradder (BAG); IntegerPrinter Printer = New IntegerPrinter (BAG);

// adder andprinter add the 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, you will see:

c: / javaworld / java DriverAbout to add another integer to the bag: The contents of the IntegerDataBag have 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 of the IntegerDataBag have changed.The new contents of the IntegerDataBag contains: 23456789

IntegerDataBag / Integeradder / IntegerPrinter is a very simple example of applying Observer mode. Java itself has a lot of examples of Observer mode: AWT / SWING event model, as well as java.util.observer and java.util.observable interfaces, etc., are all good examples.

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

New Post(0)