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