This article describes the principles and usage of decorative modes of Java design patterns. Share it for your reference, as follows:
Decorative mode can dynamically extend the functionality of an object without changing the original file and using inheritance. It wraps the real object by creating a wrapper object, that is, decoration. The design of IO in JDK uses the decorative mode, which can expand the function by wrapping the node flow through filtering flow.
The composition of the character in the decorative mode:
① Abstract component role: Give an abstract interface to standardize objects that are prepared to receive processing functions. (InputStream, OutputStream)
② Concrete Component role: defines a class that will receive additional functions. (Node Flow)
③ Decorator role: holds an instance of a component object and implements an interface consistent with the abstract component interface. (FilterInputStream, FilterOutputStream)
④ Concrete Decorator role: Responsible for adding additional functions to component objects. (Filter flow with specific additional functions, BufferedInputStream, DataInputStream, etc.)
Here is a simple example of a decorative pattern:
1. Abstract component role : define an interface Component
package com.tydic.decorator;//Abstract component role public interface Component { public void doSomething();}2. Specific construction roles : You need to implement abstract component roles, and you can add some responsibilities to this object.
package com.tydic.decorator;/** * Concrete role construction, implement abstract role construction* @author Administrator * */public class ConcreteComponent implements Component { @Override public void doSomething() { System.out.println("Function A"); }} 3. Decorate role : Hold an object to build a reference to the role and implement abstract component roles. The abstract component role is implemented because the type cannot be changed after adding functions, just like FilterInputStream is still an input stream, which still has the characteristics of an input stream. The reference to an object building role is because in order to add functions, you must hold a reference to the component role to which the function is to be attached.
package com.tydic.decorator;/** * Decorate the role, hold a reference to a component role, and implement the component role* If you want to add a function, you must implement the component role. To add a function, you must hold a reference to the component role to be attached. This is why you must hold a reference to the component role* @author Administrator * */public class Decorator implements Component { private Component component;//This is the component role to be attached, which can be uploaded in when instantiated public Decorator(Component component) { this.component = component; } @Override public void doSomething() { component.doSomething(); }}4. Specific decorative role : Decorative roles need to be inherited and the functions to be attached are given
package com.tydic.decorator;/** * For specific decorative role 1, the decorative role needs to be inherited and the functions to be attached are given* @author Administrator * */public class ConcreteDecorator1 extends Decorator { public ConcreteDecorator1(Component component) { super(component); } @Override public void doSomething() { super.doSomething(); this.doAnothing();//Add functions on the basis of the original functions of the specific component roles passed} //Additional functions public void doAnothing() { System.out.println("Function B"); }} package com.tydic.decorator;/** * For specific decorative role 2, the decorative role needs to be inherited and the functions to be attached are given* @author Administrator * */public class ConcreteDecorator2 extends Decorator { public ConcreteDecorator2(Component component) { super(component); } @Override public void doSomething() { super.doSomething(); this.doAnothing();//Additional functions public void doAnothing() { System.out.println("Function c"); }}5. Write client code
package com.tydic.decorator;public class Client { public static void main(String[] args) { Component component = new ConcreteComponent();//Construct the role Component component2 = new ConcreteDecorator1(component);//Decorate the component component Component component3 = new ConcreteDecorator2(component2);//Decorate the component component3.doSomething(); }}Summarize:
The decorative mode can use the combination method and dynamically expand objects at runtime without inheritance. This is something that inheritance cannot do. Inheritance is static, an extension to the class.
Advantages and disadvantages of decorative mode:
Advantages: 1. Extend the function of the object, which is more flexible than inheritance. 2. By using different specific decorative categories and the arrangement and combination of these decorative categories, designers can create many combinations of different behaviors.
Disadvantages: It will make the program more complicated.
For more Java-related content, readers who are interested in this site can view the topics: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.