The understanding and implementation of the observer pattern of the GOF23 design pattern 2 (using Java-brought observer classes and interfaces)

Source: Internet
Author: User
Tags event listener


Observer mode Observer
The broadcast mechanism scenario:
Scene:
The creation of a chat room program. After the server is created,a,B, andC three clients connect to the public chat. A sends data to the server and changes the server-side chat data. We want to send these chat data to other online customers, respectively. In other words, each client needs to update the server-side data.
On the website, many people subscribe to the "Java theme " News. When this topic is on the news, the news will be sent to all subscribers.
When we play CS game, the server needs to send everyone's orientation to all the customers.
Above these scenarios, we can all use the observer pattern to handle. We can put multiple subscribers, what the customer calls the Observer: data that needs to be synchronized to multiple subscribers is encapsulated in the object, called the target.
Core:
The observer pattern is primarily used for 1:N notifications. When the state of an object (target object Subject or objservable) changes, he needs to tell a series of objects ( The Observer object,Observer)in a timely manner so that they respond
How to notify the Observer:
Push:
Each time the notice is broadcast to all observers, all observers can only passively accept
Pull:
As long as the observer knows that there is a change in circumstances, it is up to them to decide when to get content.
Java.util.Observable and Java.util.Observer to implement the observer pattern
Common scenarios in development:
The chat room program, the server forwards to all the clients
Online game (Multiplayer) scenario, the server distributes the state of the client
Mail Subscription
Servelet, the implementation of the listener
AWT in the JDK event processing model, observer-mode-based delegation time model (delegation)
Event Source:====== target Object
Event Listener ============ Viewer
======================================
Package com.bjsxt.cn.observer2;
Import java.util.Observable;
public class ConcreteSubject extends observable{private int state;
public void SetState (int.) {this.state = state;  Setchanged (); Notifyobservers (state); } public int getState () {return state;}
Public ConcreteSubject (int.) {super ();  This.state = State;  Setchanged (); Notifyobservers (state); } @Override public void Notifyobservers () {super.notifyobservers ();}}
======================================
Package com.bjsxt.cn.observer2;
Import Java.util.observable;import java.util.Observer;
public class Observera implements Observer {
private int state; public int getState () {return state;}
public void SetState (int.) {this.state = state;}
@Override public void Update (Observable arg0, Object arg1) {setState (((ConcreteSubject) arg0). GetState ());}
}====================================

The understanding and implementation of the observer pattern of the GOF23 design pattern 2 (using Java-brought observer classes and interfaces)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.