Design Pattern series-Observer

Source: Internet
Author: User

I. Class Diagram

 

Ii. Intention

Define a one-to-many dependency between objects,When the status of an object changes,All objects dependent on it are notified and automatically updated.

Iii. Applicability

A) When an abstract model has two aspects:,One aspect depends on the other. Encapsulate the two in independent objects so that they can be changed and reused independently.

B) when changing an object, you need to change other objects at the same time.,I don't know how many objects need to be changed.

C) when an object must notify other objects, it cannot assume who the other objects are. In other words,You do not want these objects to be tightly coupled.

Iv. Instances

In the fifth part of rural love, zhongda has come up with an idea. When Yang came, the security guard stopped Yang and informed Wang danale. I will not elaborate on the details. If you want to view the details online, you can. This is a typical observerDesign mode.

 

 Package Using e. observer;

Public Interface Observer {
Public Void Notice ();
}

Package Using e. observer;
// Observer object
Public Class Guardleader Implements Observer {
@ Override
Public Void Notice (){
System. Out. println ("Wang Zong, Yang Zong! ");
}
}

Package Using e. observer;
// Observer object
Public Class Guardmember Implements Observer {
@ Override
Public Void Notice (){
System. Out. println ("Yang has been here for 10 minutes ...");
}
}

Package Using e. observer;

Public Interface Observable {
Public Void Addobserver (Observer O );
Public Void Delobserver (Observer O );
Public Void Inform ();
}

Package Using e. observer;

Import Java. util. arraylist;
Import Java. util. List;

// Observable object
Public Class Gate Implements Observable {
Public List <observer> guards = Null ;

Public Gate (){
Guards = New Arraylist <observer> ();
}

Public Void Openthegate (string person ){
If ("Yang Zong". Equals (person )){
Inform ();
}
System. Out. println ("Welcome, Mr. Yang! ");
}

@ Override
Public Void Addobserver (Observer O ){
Guards. Add (O );
}

@ Override
Public Void Delobserver (Observer O ){
Guards. Remove (O );
}

@ Override
Public Void Inform (){
For (Observer O: Guards ){
O. Notice ();
}

}
}

Package Using e. observer;

Public Class Client {
Public Static Void Main (string [] ARGs ){
Guardleader leader = New Guardleader ();
Guardmember member = New Guardmember ();
Gate = New Gate ();
Gate. addobserver (leader );
Gate. addobserver (member );
Gate. openthegate ("Yang Zong ");
}
}

 

ThisObserverThe mode is customized.CodeAt the same timeJDKGiven inObserverThe implementation interfaces of areObservableAndObserver. I will not talk about the usage of these two classes. Please try to read them.APIRight.

 

5. AWTMediumObserverMode

 

Java AWTUsed in packageObserverThe mode is the most, and the image is written.ProgramAll know: we need to haveObserverClass listening, otherwise it will not produce the expected results.

 

Take a lookButtonClass source code:

 

 Public   Class Button Extends Component Implements Accessible {
......
Public Synchronized Void Addactionlistener (actionlistener L ){
If (L = Null ){
Return ;
}
Actionlistener = awteventmulticaster. Add (actionlistener, L );
Neweventsonly = True ;
}

Public Synchronized Void Removeactionlistener (actionlistener L ){
If (L = Null ){
Return ;
}
Actionlistener = awteventmulticaster. Remove (actionlistener, L );
}

Protected Void Processactionevent (actionevent e ){
Actionlistener listener = actionlistener;
If (Listener! = Null ){
Listener. actionreceivmed (E );
}
}

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.