Encapsulate core business units using the decorator Mode

Source: Internet
Author: User

Sometimes, it is easiest to implement a basic core code that is quickly encapsulated with dedicated performance by peripheral code.
The superclass is used, but the superclass uses inheritance to improve coupling. In this case, you can also use
Decorator mode, which is a good way to replace inheritance with combinations.
1. Intention
In fact, the above intention can be attributed to "dynamically adding functions without changing the object ",
In other words, we do not want to change the original class or create a subclass to add features. In this case,
You can use the decoration mode.
2. Structure
An important feature of the modifier structure is that it inherits from an abstract class, but it uses the aggregation of this abstract class.
(That is, decorative objects can contain abstract class objects). Proper design can achieve our purpose.
Suppose we have constructed a payment-based simple factory model system. Now you need to call the method in each class
In gosale (), in addition to the original functions, a dialog box is displayed to display the factory name.
To change the system, add a decoration class decorator in the module type of the factory class, and slightly rewrite one.
The code of the factory class.
// Decoration
Public class decorator extends payment {
Private string strname;
Public decorator (string strname ){
This. strname = strname;
}
Private payment PM;
Public void setpm (Payment value ){
PM = value;
}
Public String action (){
// A prompt box is displayed before the original code is executed.
System. Out. println (strname );
Return pm. Action ();
}
}
Factory:
// This is a factory class
Public class factory {
Public static payment paymentfactory (string paymentname ){
Payment MDB = NULL;
If (paymentname. Equals ("cash "))
MDB = new cashpayment ();
Else if (paymentname. Equals ("Credit Card "))
MDB = new creditpayment ();
Else if (paymentname. Equals ("check "))
MDB = new checkpayment ();
// Return MDB;
Decorator M = new decorator (paymentname );
M. setpm (MDB );
Return m;
}
}
It can be said that this changes the performance without the user's knowledge or the original class.

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.