Simply speaking, decorative mode

Source: Internet
Author: User
Tags ming

This is simply a decorative pattern, because the pattern is not well understood. Can't write a deeper piece of content for the time being.

What is decorative mode? To draw up a scene where one needs to dress up, we can write a person class, define n methods for it, wear short sleeves, wear leather shoes, and so on. To add a new decoration, we add a new method to the person class, but this violates the "method-closed" principle. How to write better and more flexible code? We use decorative patterns to implement this scenario. Xiao Ming wears short sleeves first, then wears leather shoes.

Wearing short sleeves, leather shoes and so on, we put these decorative methods simply abstracted out into a decorative class, different decorative inheritance implementation of this class. A person is defined as an interface, xiaoming is a concrete person who inherits the "Human" interface. At the same time the adornment class aggregates and realizes "the human" interface, this is also the question which I temporarily cannot understand deeply. A bit around the mouth, we still draw the UML class diagram first.

After the class structure is clear, we implement the code according to the class structure.

First, we define the interface of component abstract artifacts (decorative decorations are abstract artifacts).

1  PackageDay_6_decorator;2 3 /**4 * Abstract Artifacts5  * @authorTurbo6  *7 * September 9, 20168  */9  Public InterfaceComponent {//personTen     voidoperation (); One}

Implement this component abstract artifact to create a concretecomponent concrete component.

1  PackageDay_6_decorator;2 3 /**4 * Concrete Components5  * @authorTurbo6  *7 * September 9, 20168  */9  Public classConcretecomponentImplementsComponent {//XiaomingTen  One     /*(Non-javadoc) A * @see day_6_decorator.component#operation () -      */ - @Override the      Public voidoperation () { -SYSTEM.OUT.PRINTLN ("Operation of the specific object"); -     } -  +}

Defines the adornment class.

1  PackageDay_6_decorator;2 3 /**4 * Decoration class5  * @authorTurbo6  *7 * September 9, 20168  */9  Public classDecoratorImplementsComponent {Ten     PrivateComponent Component; One      A      Public voidsetcomponent (Component Component) { -          This. Component =component; -     } the     /*(Non-javadoc) - * @see day_6_decorator.component#operation () -      */ - @Override +      Public voidoperation () { -         if(Component! =NULL){ + component.operation (); A         } at     } -  -}

Define the specific decorative class Concretedecoratea.

1  PackageDay_6_decorator;2 3 /**4 * Specific decoration Class A5  * @authorTurbo6  *7 * September 10, 20168  */9  Public classConcretedecoratoraextendsDecorator {Ten     PrivateString addedstate;//unique features of this class One      A @Override -      Public voidoperation () { -         Super. Operation (); theAddedstate = "New State"; -System.out.println ("Wear short sleeves"); -     } -      +}

Define the specific decorative class CONCRETEDECORATEB.

1  PackageDay_6_decorator;2 3 /**4 * Concrete Decoration Class B5  * @authorTurbo6  *7 * September 10, 20168  */9  Public classConcretedecoratorbextendsDecorator {Ten  One @Override A      Public voidoperation () { -         Super. Operation (); - Addedbehavior (); the     } -  -     /** -      *  +      */ -     Private voidAddedbehavior () { +System.out.println ("Wear Shoes"); A     } at      -}

Client code.

1  PackageDay_6_decorator;2 3 /**4  * @authorTurbo5  *6 * September 10, 20167  */8  Public classMain {9 Ten     /** One      * @paramargs A      */ -      Public Static voidMain (string[] args) { -Concretecomponent C =Newconcretecomponent (); theConcretedecoratora D1 =NewConcretedecoratora (); -Concretedecoratorb D2 =NewConcretedecoratorb (); -          - d1.setcomponent (c); + d2.setcomponent (d1); - d2.operation (); +     } A  at}

Output results.

So we used two decoration class to decorate Xiao Ming, and can change the order of decoration at will. Temporarily still can not talk about the decoration mode, I understand more thoroughly to understand, but this decoration mode and Java dynamic Proxy mechanism is somewhat similar. The study will also be studied carefully afterwards.

Simply say decorative mode

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.