Add a function in decorator Mode

Source: Internet
Author: User

Http://www.35java.com/zhibo/forum.php? MoD = viewthread & tid = 259 & extra = Page % 3d2

Maybe you are already using the decorator mode, but like all other models, it is important to know why you use it and learn to use it to communicate with other developers. The decorator mode, that is, the wrapper mode. Its force control function is not to change a class. Code Or add a function for the class without damaging the class interface. When you want a class to have more functions, using the decorator Mode means that the new version does not have to use the extension class to reuse the existing functions.
Assume that an interface called action has two methods: act1 () and Act2 (). This interface has a specific version of concreteaction. The decorator will be a class that implements the action, and its concreteaction has an action (usually concreteaction ). The Code is as follows:
Public
Class Actiondecorator implements action {

Private Action action;

Public Actiondecorator (action Action ){
This . Action = Action;
}


Public
Void Act1 (){
Action. act1 ();
}

Public
Void Act2 (){
// Do nothing
}

}

Using decorator produces fewer classes than using inheritance, so its code is relatively simple. However, it usually produces more objects, which increases the difficulty of debugging, especially because it increases flexibility, new errors may be introduced.

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.