First, the basic concept
1, the decorative pattern refers to the case without changing the original class file and use inheritance, dynamic function expansion of an object.
2, decorative object contains a reference to a real object, it is critical.
3, the role:
A: Role abstract component (Component): definition of an object interface or abstract class can be dynamically added to the responsibility of these objects.
B: the role of specific components (ConcreteComponent): real objects are dynamically added responsibilities.
C: abstract decorative role (Decorator): implements the Component interface used to extend the functionality of the Component class, but for the Component, it is without knowing of the existence of Decorator.
D: Specific decorative role (ConcreteDecorator): dynamically add responsibilities object.
E: Client (Client)
Second, Examples (of a simple implementation, the C is omitted)
A: Role abstract component (Component):
. 1 Package comm.pattern.struct.decorator; 2 . 3 / ** . 4 * . 5 * @Title: Component.java . 6 * @package: comm.pattern.struct.decorator . 7 * @Description: abstract construct, Yang Guo analog 8 * @ author yangzhancheng . 9 * @ 16 March 2020: 12:00:38 aM 10 * . 11 * / 12 is public interface the Component { 13 is public void Operation (); 14 }
B: the role of specific components (ConcreteComponent):
. 1 Package comm.pattern.struct.decorator; 2 . 3 public class ConcreteComponent the implements the Component { . 4 . 5 public void Operation () { . 6 of System.out.print ( "Yang Guo, too name, change of the word, is Jin Yong" Condor Crazy "hero;" ); 7 } . 8 . 9 }
C: abstract decorative role (Decorator):
Omitted.
D: Specific decorative role (ConcreteDecorator):
D-1:
1 package comm.pattern.struct.decorator; 2 3 /** 4 * 5 * @Title: Decorator.java 6 * @Package: comm.pattern.struct.decorator 7 * @Description: 装饰类 8 * @author yangzhancheng 9 * @2020年3月16日:上午12:05:54 10 * 11 */ 12 class DecoratorA implements Component 13 { 14 private Component component; 15 16 public DecoratorA(Component component) 17 { 18 is the this .component = Component; . 19 } 20 is 21 is public void Operation () 22 is { 23 is component.operation (); 24 of System.out.print ( "tomb he had to be in the living dead, Yunvxinjing practiced;" ); 25 } 26 }
D-2:
1 package comm.pattern.struct.decorator; 2 3 /** 4 * 5 * @Title: DecoratorB.java 6 * @Package: comm.pattern.struct.decorator 7 * @Description: 装饰类 8 * @author yangzhancheng 9 * @2020年3月16日:上午12:10:12 10 * 11 */ 12 class DecoratorB implements Component { 13 14 private Component component; 15 16 public DecoratorB(Component component) { 17 this= .component Component; 18 is } . 19 20 is public void Operation () { 21 is component.operation (); 22 is of System.out.print ( "He had a brother carving" ); 23 } 24 }
E: Client (Client)
. 1 Package comm.pattern.struct.decorator; 2 . 3 / ** . 4 * . 5 * @Title: Client.java . 6 * @package: comm.pattern.action.templateMethod . 7 * @Description: This document describes what 8 * @ author yangzhancheng . 9 * @ 1 March 2020: PM 4:47:17 10 * . 11 * / 12 is public class Client { 13 is 14 public static void main (String [] args) { 15 16 the Component Component = new new ConcreteComponent () ; 17 component.operation(); 18 19 System.out.println("\n---------------------------------"); 20 Component partyComponentDec = new DecoratorA(component); 21 partyComponentDec.operation(); 22 23 System.out.println("\n---------------------------------"); 24 Component allComponent = new DecoratorB(new DecoratorA(new ConcreteComponent())); 25 allComponent.operation(); 26 } 27 28 }
Run output
Yang Guo, were over, change of character, Jin Yong is the "evil" of the hero; ---------------------------- ----- Yang Guo, were over, change of character, Jin Yong is the "evil" of the hero; he stayed in the tomb of the living dead, practiced Yunvxinjing; ---------- ----------------------- Yang Guo, were over, change of character, Jin Yong is the "evil" of the hero; his tomb in the Land of the Dead He stayed, practiced Yunvxinjing; he had an eagle brother
Third, the summary
IO operation flow is typical of decorative patterns. Subsequent posted source code.