When you need upper-layer operations on the underlying layer, you can use the observer mode to achieve Upward collaboration. That is, upper-layer response
The underlying event, but the Execution Code of this event is provided by the upper layer.
1. Intention:
Defines the one-to-many dependency of an object. When an object changes, all objects dependent on it get
And is automatically updated.
2. Structure
The traditional observer pattern structure is as follows.
3. Example:
// Paymentevent. Java class for incoming data
Import java. util .*;
Public class paymentevent extends eventobject {
Private string text; // defines the internal variables of text.
// Constructor
Public paymentevent (Object source, string text ){
Super (source );
This. Text = text; // receives variables passed in from outside
}
Public String gettext (){
Return text; // Let the external method obtain the user input string
}
}
// Listener Interface
// Paymentlistener. Java
Import java. util .*;
Public interface paymentlistener extends eventlistener {
Public String action (paymentevent E );
}
// Payment. Java platform class
Import java. util .*;
Public class payment {
Private double amount;
Public double getamount (){
Return amount;
}
Public void set (double value ){
Amount = value;
}
// Event Programming
Private arraylist ELV; // event listening list object
// Adds an event listener.
Public void addpaymentlistener (paymentlistener m ){
// If the table is empty, create its object first
If (ELV = NULL ){
ELV = new arraylist ();
}
// If the listener does not exist, add it
If (! ELV. Contains (M )){
ELV. Add (m );
}
}
// Delete the event listener
Public void removepaymentlistener (paymentlistener m ){
If (ELV! = NULL & ELV. Contains (M )){
ELV. Remove (m );
}
}
// Ignition readtext Method
Protected string fireaction (paymentevent e ){
String M = E. gettext ();
If (ELV! = NULL ){
// Activate the writetextevett event for each listener
For (INT I = 0; I <ELV. Size (); I ++ ){
Paymentlistener S = (paymentlistener) ELV. Get (I );
M + = S. Action (E );
}
}
Return m;
}
Public String gosale (){
String x = "unchanged process 1 ";
Paymentevent M = new paymentevent (this, X );
X = fireaction (m); // variable stream
X + = Amount + ", querying inventory status"; // attributes and unchanged process 2
Return X;
}
}
Call: Test. Java
Import java. util .*;
Public class test {
// Entry
Public static void main (string [] ARGs ){
Payment O1 = new payment ();
Payment O2 = new payment ();
Payment O3 = new payment ();
O1.addpaymentlistener (New paymentlistener (){
Public String action (paymentevent e ){
Return e. gettext () + "pay in cash ";
}
});
O2.addpaymentlistener (New paymentlistener (){
Public String action (paymentevent e ){
Return e. gettext () + "pay by credit card ";
}
});
O3.addpaymentlistener (New paymentlistener (){
Public String action (paymentevent e ){
Return e. gettext () + "pay by check ";
}
});
O1.set (777 );
O2.set (777 );
O3.set (777 );
System. Out. println (o1.gosale ());
System. Out. println (o2.gosale ());
System. Out. println (o3.gosale ());
}
}