Observer

xiaoxiao2021-03-06  74

definition

Name: OBServer Structure: Intent: A pair of dependencies between objects, when the status of an object changes, all objects that depend on its object are notified and automatically updated. Applicability: When an abstract model has two aspects, one is dependent on the other, encapsulating the two in a separate object enables them to change and multiplex them to change other objects at the same time as changes to an object. But don't know how many objects have to be changed. When an object must be notified, it can't assume what other objects are, that is, it does not want these objects to be closely coupled: Allow you to independently change the target and observer. You can reuse the target object alone without having to reuse the observer at the same time, and vice versa. You can also increase the observer without changing the target and other observer

Classic Case

1. The model for event processing of AWT / SWING DEM (Delegation Event Model) AWT1.0 is based on the responsible chain. This model does not apply to a complex system, so in all versions of the AWT1.1 and later, the event processing model is based on the observer mode (DELEGATION Event Model or DEM).

In the DEM model, the Subject role is responsible for publish events, and the observer role subscribes to a particular topic. Events it is interested in the event it is interested in the Subscribe. When a specific topic produces an event, it will notify all of the subscribers of interest.

The basic design goal of using this release-subscription mechanism is to provide a way in which the publisher and subscribers are loosely coupled together, and a way to dynamically register and unwind to a publisher subscription request. Obviously, implementing this concept is a design abstract interface and separates the abstraction layer and the specific layer. This can be clearly seen in the observer mode.

Using the words of DEM, the publisher is called an event source, and the subscriber is called an event listener (Event Listener). In Java, the event is represented by a class, and the release of the event is done by simultaneously calling the member method.

DEM mechanism in servlet technology

The DEM event model used in the AWT is actually applied to all Java event mechanisms. The event handling mechanism in servlet technology is also useful to use the DEM model.

DEM mechanism in SAX2 technology

The DEM event model is also applied to the event handling mechanism of SAX2.

Best Practices

1. Observer interface and OBSERVABLE class in J2SE

Observer and Observable respectively correspond to Observer and Subject in design mode, respectively.

Java.util.Observer:

Update (Observable Subject, Object Arg) Monitor Subject, what response occurs when the Subject object status changes, arg is the parameter passing to the NOTIFYOBSERVERS method for OBSERVABLE;

Java.util.Observable:

AddobServer (Observer Observer) Observer Register your own Haschanged () check whether the Subject state has changed setchanged () Set the status of the subject to "changed" notifyobservers () Notification Observer This subject state changes

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

New Post(0)