The JDK provides programmers with a large number of class libraries, in order to maintain the reusability, scalability and flexibility of the class library, which uses a large number of design patterns, this article will introduce the decorator mode used in the JDK I/O package, and use this pattern to implement a new output stream class.
Introduction to Decorator Mode
Decorator mode is also known as the wrapper (wrapper), its main purpose is to give an object dynamically add some additional responsibilities. It is more flexible than generating subclasses.
Sometimes we need to add new features to an object instead of the entire class, such as adding a scroll bar to a text area. We can use the inheritance mechanism to implement this, but this approach is not flexible enough to control the way and timing of the text area plus scrollbars. And when you need to add more functionality to the text area, such as borders, you need to create new classes, and when you need to combine them, it will certainly cause class explosions.
We can use a more flexible method, which is to embed the text area in the scroll bar. And the class of this scroll bar is equivalent to a decoration of the text area. This adornment (scroll bar) must inherit from the same interface as the decorated component (text area), so that the user does not have to care about the implementation of the decoration, because it is transparent to them. Decoration forwards the user's request to the appropriate component (that is, the associated method is invoked), and may do some extra action (such as adding a scroll bar) before and after forwarding. In this way, we can embed different decorations on the text area according to the combination, so as to add any number of functions. This dynamic method of adding functionality to an object does not cause a class explosion, but also has more flexibility.
The above method is the decorator mode, which dynamically adds new functionality by adding decorations to the object. The following is a UML diagram of the decorator schema:
Component is a common parent class for components and decorations that defines the methods that subclasses must implement.
Concretecomponent is a concrete component class that can add new functionality by adding decorations to it.
Decorator is the public parent class for all decorations, which defines the methods that all decorations must implement, and it also holds a reference to component to forward the user's request to the component and may perform additional actions before and after the request is forwarded.
Concretedecoratora and Concretedecoratorb are concrete decorations that can be used to decorate specific component.
Decorator mode in Java IO package
The decorator mode is used in the java.io package provided by the JDK to encapsulate various input and output streams. The following is an example of Java.io.OutputStream and its subclasses to discuss the use of decorator patterns in IO.
Let's start by looking at a piece of code to create an IO stream:
The following is a code fragment:
try {
OutputStream out = new DataOutputStream(new FileOutputStream("test.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}