Design Patterns-Decorative Design Patterns

Source: Internet
Author: User

Decorator pattern design

First, we create an interface to create a blueprint for the class that will be used for the decorator. Then, we only implement the basic functions of this interface. So far, we have obtained a blueprint class with a decorator? Create a package? An attribute of this interface type. The constructor of this class assigns the instance of this type of interface to this attribute?
This class is the base class of the decorator. Now you can extend the class and create the decoration class you need. The specific modifier class will add its own method. Execute its own method before or after, and then the specific modifier class will call the method of the base instance? The key to the decorator mode is to bind the method and the base class instance to the constructor at runtime. Therefore, you can dynamically customize the behavior of a specific instance without affecting other instances? /P>

 

Package decorate; interface icecream {Public String makeicecream ();} class simpleicecream implements icecream {Public String makeicecream () {return "simpleicecream ";}} abstract class icecreamdecorator implements icecream {icecream ice = NULL; Public icecreamdecorator (icecream specialicecream) {This. ice = specialicecream;} Public String makeicecream () {return ice. makeicecream () ;}} class maid extends icecreamdecorator {public nuttydecorator (icecream specialicecream) {super (response);} Public String makeicecream () {return addnuts ();} private string addnuts () {return "Icecream + nuts" ;}} class honeydecorator extends icecreamdecorator {public honeydecorator (icecream iterator) {super (iterator);} Public String makeicecream () {return addhoney ();} private string addhoney () {return "Icecream + Honey" ;}} public class decorator {public static void main (string ARGs []) {icecream ice = new simpleicecream (); system. out. println (ice. makeicecream (); maid = new maid (New simpleicecream (); system. out. println (nuttyicecream. makeicecream (); honeydecorator honeyicecream = new honeydecorator (New simpleicecream (); system. out. println (honeyicecream. makeicecream () ;}}/*** structure: * icecream (Interface) defines the interface between the class and the decoration class * | -- simpleicecream (class) // define a class with only basic functions (decorated) * | -- icecreamdecorator (Abstract decorative class) // defines the abstract class of all decorative Classes * | -- nuttydecorator (decorative class) * | -- honeydecorator * the most important step in defining the abstract of the decoration class is * 1. define an icecream ice = new simpleicecream attribute * 2. input the decorated object in the constructor of the abstract decoration class and assign the value to the internal attribute **/

Http://javasight.net/2011/05/decorator-design-pattern/

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.