Use the decoration mode to view the interface, and the decoration Mode Interface
Decorator)Intention
Decoration Mode
Dynamically add some additional responsibilities to an object. The Decorator mode is more flexible than the subclass generation function.
Applicability
- Add roles to a single object dynamically and transparently without affecting other objects.
- Handle unrecoverable responsibilities.
- When the subclass generation method cannot be used for expansion. One case is that there may be a large number of independent extensions. To support each combination, a large number of subclasses will be generated, resulting in explosive growth of the number of subclasses. Another scenario is that the class definition is hidden, or the class definition cannot be used to generate a subclass.
- Using the decoration mode, we can see that the interface is implemented in the called interface,
- The interface method is implemented where the interface is implemented, then, the interface implementation object is received at the place where the interface is called, and then the methods in the interface can be directly implemented in the class that calls the interface (the method in the interface ).
- Java code of the abstract component role
- Package decorator;
- /**
- * Common method interface between the decorator and the original component (Abstract component role)
- * @ Author mouca. he
- *
- */
- Public interface InterfaceComponent {
- /**
- * Component method say ()
- *
- */
- Public void say ();
- }
Java code of the specific component role
- Package decorator;
- /**
- * Original component (specific component role)
- * @ Author mouca. he
- *
- */
- Public class Component implements InterfaceComponent {
- Public void say (){
- // TODO automatically generates method stubs
- System. out. println ("Component. say (): Method of the original Component! ");
- }
- }
Java code of the abstract modifier role
- Package decorator;
- /**
- * Abstract Modifier
- * @ Author mouca. he
- *
- */
- Public abstract class AbstractDecorator implements InterfaceComponent {
- Private InterfaceComponent component;
- Public AbstractDecorator (InterfaceComponent component ){
- This. component = component;
- }
- /**
- * Component Method Pre-processing method
- *
- */
- Protected void preSay (){};
- /**
- * Post-processing method for component method execution
- *
- */
- Protected void afterSay (){};
- Public void say (){
- PreSay ();
- Component. say ();
- AfterSay ();
- };
- }
Specific modifier 2 java code
- Package decorator;
- /**
- * Decorator 2
- * @ Author mouca. he
- *
- */
- Public class DecoratorTwo extends actdecorator {
- Public DecoratorTwo (InterfaceComponent component ){
- Super (component );
- // TODO automatically generates the constructor stub
- }
- /**
- * Reload the template class preSay () method as needed
- */
- Protected void preSay (){
- System. out. println ("DecoratorTwo. preSay (): preSay () method of modifier 2! ");
- }
- /**
- * Overload the template class afterSay () method as needed
- */
- Protected void afterSay (){
- System. out. println ("DecoratorTwo. afterSay (): The afterSay () method of modifier 2! ");
- }
- }
Modifier 1 java code
- Package decorator;
- /**
- * Decorator 1
- * @ Author mouca. he
- *
- */
- Public class DecoratorOne extends actdecorator {
- Public DecoratorOne (InterfaceComponent component ){
- Super (component );
- // TODO automatically generates the constructor stub
- }
- /**
- * Reload the template class preSay () method as needed
- */
- Protected void preSay (){
- System. out. println ("DecoratorOne. preSay (): preSay () method of modifier 1! ");
- }
- /**
- * Overload the template class afterSay () method as needed
- */
- Protected void afterSay (){
- System. out. println ("DecoratorOne. afterSay (): The afterSay () method of modifier 1! ");
- }
- /**
- * Test Method
- * @ Param args
- */
- Public static void main (String [] args ){
- // TODO automatically generates method stubs
- InterfaceComponent interfaceComponent = new DecoratorTwo (new DecoratorOne (new Component ()));
- InterfaceComponent. say ();
- /*
- * Console output:
- * DecoratorTwo. preSay (): The preSay () method of DecoratorTwo!
- * DecoratorOne. preSay (): The preSay () method of DecoratorOne 1!
- * Component. say (): Method of the original Component!
- * DecoratorOne. afterSay (): The afterSay () method of modifier 1!
- * DecoratorTwo. afterSay (): The afterSay () method of DecoratorTwo!
- */
- }
- }
4. Advantages and Disadvantages
Advantages: 1) provide more flexibility than inheritance 2) use different decorative combinations to create combinations of different behaviors 3) Reduce the number of classes required
Disadvantages: 1) Flexibility leads to greater errors. 2) more objects are generated, which makes error detection difficult.