Use observer mode to handle changes in business units

Source: Internet
Author: User

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 ());
}
}

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.