Use the Observer to achieve object monitoring

xiaoxiao2021-03-06  110

Use the Observer to achieve object monitoring

Bromon original, please respect the copyright

When there are very many times, we hope that our program can monitor the changes in data, then respond, this situation, such as the detection of data in the database, detect changes in user status, and so on. Usually we have lack a mechanism for duplex communication, only choose to let the program do the argument, detect once a period of time, record the comparison with the previous test results, thus determining whether the data changes. There is no doubt that this way is very awkward, not only writing pain, running, is also resource, is a typical problem with 20% of 80% of time.

Observer is a model, which is an API in Java. It makes a value object (Value Object) feature, and when he finds its own state, send a message to the relevant object, this The method of listening is of course better than polling. I have a cold, I will go to the hospital, I don't need a doctor to ask every month. The animal and Yuxi have given me a paragraph of the servers' code of mahjong. It was originally to study the mahjong algorithm, but was attracted by the use of Observer, which was highly effective. I used to use the thread pool reflection the observer written a real-time server, and the convenience of Socket also has the efficiency of UDP, but it is a pity that later because the sharply modified design code is invalid, but the observer is still worth studying.

Java's Observer API is an implementation of the observer mode. Suppose we have an object container that stores user messages, I hope this container is from the province, and when there is a new message coming in, it will respond to the observer. First define the message object, it is a very simple value object:

Package com.gwnet.smsMssenger.mm.bromon;

Public Class Message

{

Private int ID;

Private string sender;

PRIVATE STRING RECEIVER;

PRIVATE STRING Content;

PRIVATE STRING TIME;

/ / Please implement SET / GET methods, such as:

Public int getID ()

{

Return ID;

}

Public void setid (INT ID)

{

THIS.ID = ID;

}

}

Then write a container that stores Message, and the container uses ArrayList to store the object is a good choice, it is also very simple:

/ *

* CREATED ON 2004-8-11

* /

Package com.gwnet.smsMssenger.mm.bromon;

Import java.util. *;

/ **

* @Author bromon

* /

Public Class Messagelist Extends Observable

{

PRIVATE LIST M = New ArrayList ();

Private stat messageList ml = null;

Public Messagelist ()

{

}

Public Static Messagelist GetInstance ()

{

IF (ml == NULL)

{

ml = new messageelist ();

}

Return ML;

}

Public Void Add (Message MSG)

{

M.ADD (MSG);

Super.setchanged ();

Super.NotifyObservers (m);

}

Public void del (Message MSG)

{

M.Remove (MSG);

}

}

This class inherits the OBServable class, and the Add method is made, it is obvious that the role of the add method is to put an object to the ArrayList container, which is the operation we want to listen, so there is: uper.setchanged ();

Super.NotifyObservers (m);

This means that once the Add method is called, this class will send a message to all registered observer, what is the message content? The content is M. It is a container where the message is stored. The observer can receive this container that changes the state, then operates it, which has achieved the monitoring of the container, of course, we only have a monitoring of the Add method, you also Try other things.

It is necessary to pay special attention to this is an incomplete single case class, and write a single example is to ensure that only the container in the JVM, and is not a complete single case, because there may be additional instances in the future. Method. So understanding that it may be slightly difficult, you can refer to the single example mode in the design mode.

Here is to write an observer and register it:

/ *

* CREATED ON 2004-8-11

* /

Package com.gwnet.smsmessenger.bromon;

Import java.util. *;

/ **

* @Author bromon

* /

Public Class MessageObserver Implements Observer

{

/ * (non-javadoc)

* @see java.util.observer # Update (java.util.observable, java.lang.object)

* /

Public void update (Observable Arg0, Object Arg1)

{

List L = (list) arg1;

Message M = (Message) L.Get (L.SIZE () - 1);

String receiver = m.getReceiver ();

SYSTEM.OUT.PRINTLN ("Give M.GetReceiver ()" new message: " m.getContent ());

}

}

This class inherits the Oberver interface, Update (Observable, Object) is a method that must be provided. In this method, we receive data (containing messages with messages), then remove the last one, read it.

The observer in Java is very simple. Our example is that all operations are made in memory, and there is no need to polling, the efficiency is very high, and the disadvantage is that once the data in the machine is lost, so if there is a relatively complete object buffer mechanism, you can Cut the complex application, write efficient and simple multithreaded servers.

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

New Post(0)