The Decorator dynamically adds some additional responsibilities to an object. In addition, the decoration mode is more flexible than the subclass generation mode. Decoration mode: The decoration mode dynamically adds more functions to existing functions. When the system requires new functions, it adds new code to the old class. These newly added codes usually describe the core responsibilities or main actions of the original class by adding new fields, new methods, and new logic to the main class, thus increasing the complexity of the main class, these new things are just to meet the needs of some special behaviors that can only be executed under certain circumstances. In the decoration mode, each function to be decorated is placed in a separate class and the class is used to wrap the object to be decorated. Therefore, when special behavior is required, the Customer Code can then be run as needed. In order to use the decoration function to package objects, the decoration mode is to remove the decoration function from the class, in this way, the original class can be simplified, the core responsibilities of the class can be effectively separated from the decorative functional area, and the repeated decorative logic Component in the related class can be removed to define an object interface, you can dynamically add roles to these objects. ConcreteComponent defines a specific object. You can also add some responsibilities to this object. The Decorator and decorative abstract class inherit Component's function of extending the Component class from the external class, however, for Component, you do not need to know the existence of the Decorator. As for ConcreteDecorator, you can add responsibilities to Component for specific decoration objects. Public interface Component {public void Operation ();} public class Decorator implements Component {protected Component component; public void setComponent (Component component) {this. component = component ;}@ Override public void Operation () {if (component! = Null) {component. operation () ;}} public class ConcreteComponent implements Component {@ Override public void Operation () {System. out. println ("actions on specific objects") ;}} public class ConcreteDecoratorA extends Decorator {private String addedState; @ Override public void Operation () {super. operation (); addedState = "New State"; System. out. println ("actions on decoration object A") ;}} public class ConcreteDecoratorB extends De Corator {private String addedState; @ Override public void Operation () {super. operation (); AddedBehavior (); System. out. println ("operations on decoration object B");} private void AddedBehavior () {}} public class DetoratorMain {public static void main (String [] args) {ConcreteComponent c = new ConcreteComponent (); ConcreteDecoratorA d1 = new ConcreteDecoratorA (); ConcreteDecoratorB d2 = new ConcreteDecoratorB (); d1.set Component (c); d2.setComponent (d1); d2.Operation () ;}} Demo: public class Person {public Person () {}; private String name; public Person (String name) {this. name = name;} public void show () {System. out. println ("Dress Up" + name) ;}} public class Finery extends Person {protected Person component; public void Decorate (Person component) {this. component = component ;}@ Override public void show () {if (compo Nent! = Null) {component. show () ;}} public class BigTrouser extends Finery {@ Override public void show () {System. out. println ("Pants"); super. show () ;}} public class Sneakers extends Finery {@ Override public void show () {System. out. println ("Broken sneakers"); super. show () ;}} public class TShirts extends Finery {@ Override public void show () {System. out. println ("big t-shirt"); super. show () ;}} public class DecoratorMain {public static void main (String [] args) {Person daixu = new Person ("to be continued"); System. out. println ("first decoration"); Sneakers pqx = new Sneakers (); BigTrouser kk = new BigTrouser (); TShirts dtx = new TShirts (); pqx. decorate (daixu); kk. decorate (pqx); dtx. decorate (kk); dtx. show ();}}