Decorative mode of design mode (Java implementation)

Source: Internet
Author: User

"What's the matter, brother fish?" ”

"Alas, do not mention, the net bought a dress, the results found and business description of the difference is too much, the color difference on it, quality is not good, quality is not OK, unexpectedly size is not good, said 3 x, Mail is an X, do not say, return to. You start with the lecture, you say just one thing also decorate what kind of the vigor. (Decorative mode petrified: It's none of my business.) Well, today, by me, to talk about before a joke: the noodles were chased to a barber shop, out of a instant noodles, and then chased his people a catch him on the fight: Boy (Fourth sound), hot hair I do not know you? In fact, that person is mistaken, come out of really is instant noodles, I know, because instant noodles have birthmark on the buttocks, uh. Then why did you admit it? Yes, he thought the noodles were "decorated". Well, I'm just decorating. But fish brother's clothes really don't blame me 、、、

Self-introduction of decoration mode

First look at my definition: Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible alternative to subclassing for extending functionality. Translating means adding some extra responsibilities to an object dynamically. In terms of adding functionality, the adornment mode is more flexible than generating subclasses. The following is my class diagram:

Component is to define an object interface that can dynamically add responsibilities to these objects, concretecomponent to define a specific object, or to add some responsibility to the object. Decorator, decorative abstract class, inherited the component from the outer class to extend the function of component class, but for component, it is not necessary to know the existence of Decorator, as for the concretedecorator is the specific decorative objects, The ability to add responsibilities to component. (Decoration mode: Allow me to smoke a cigarette, drink a cup of water)

Where did you go? Oh, I remember. Next I'll talk about advantages and disadvantages.

Self-analysis of decorative patterns

I, more balanced, what is balanced, is the advantages and disadvantages of half, the main advantages of two, as follows:

    • More flexible than static inheritance. For example, you can add and detach the method, with the decoration at run time to add and remove responsibilities.
    • You can avoid having too many features in a class at the top level of the hierarchy.

Main disadvantages:

    • Decorator is not the same as its component. Decorator is a transparent wrapper, and a decorated component differs from this component if the object is identified from the point of view. Therefore, you cannot rely on object identities.
    • Using the adornment mode produces many small objects. This is difficult for it to make a custom-made. And it's difficult to do wrong.

The realization of decoration mode

Light said do not practice false bashi, so, I will give you show off, this time take the noodles perm to when chestnut, the first is the abstract class of noodles:

1 public abstract class noodles{2     //abstract Method 3 public     abstract void Say (); 4}

Abstract class is relatively simple, only a say property, followed by the implementation of the specific noodle class:

1 public  class Saynoodles extends noodles{2     //abstract Method 3     @Override4 public     Void Say () {5         System.out.println ("I am noodles"); 6     }7}

He is basically the way to realize the abstract class, describing himself as a noodle, not something else. The next is more interesting, is the focus of abstract class, decoration, is generally decorated as an abstract class, because to decorate things not only one, may decorate a lot of things, such as a noodle can be perm first, in the hair, and then baked, and so on, write to a realization class is too bloated, is not conducive to expansion, etc., so it is best to use an abstract class, and then concrete implementation of the different decorative sub-class implementation, the following is a decorative abstract class:

1 public abstract class Decorator extends noodels{2     private noodels noodles = null; 3     //passing the modified object through the constructor 4     publ IC Decorator (noodels nood) 5     {6         this.noodles = Nood; 7     } 8  9     //Delegated to be decorated execution of the     @Override11     public void Say () {         this.noodles.say ();     }14 15}

Next is a realization to the noodles perm, big change instant noodles decoration class:

public class Concretedecorator extends decorator{2     //definition modified by 3 public     concretedecorator (noodles nood) {4         super (Nood); 5     } 6  7     //define your own method of modification 8  9     private void Dsay () {         System.out.println ("I have burnt my hair");     }12 13     //overriding parent class method: public     void Say () {         this.dsay ();         Super.say ();     }18 19}

1 public class Concretedecorator extends decorator{2     //definition modified by 3 public     concretedecorator (noodles nood) {4         Super (Nood); 5     } 6  7     //define your own method of modification 8  9     private void Dsay () {         System.out.println ("I have burnt my hair");     }12 13     //overriding parent class method: public     void Say () {         this.dsay ();         Super.say ();     }18 19}

Next you can test the effect of the decoration by testing the class:

1 public class Test{2 public     static void Main (string[] args) {3         noodels noodels = new Saynoodles (); 4         //Decoration , perm start 5         noodels = new Concretedecorator (noodels); 6         Noodels.say (); 7     }8}

Well, there's no way to go about decorating mode. Want to know what the post-style (this is the mode), and listen to tell ~ (the author by: Crouching Groove, unexpectedly rob my lines, don't mind me, I want to quietly, don't ask me quietly who)

Application Scenarios for decorative patterns

    • Adds responsibility to a single object in a dynamic, transparent manner without affecting other objects.
    • Handle the duties that can be revoked

Decorative mode of design mode (Java implementation)

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.