I, Decorator pattern structure
The decoration mode extends the object functions in a transparent way to the client. It is an alternative to the inheritance relationship.
There are two decorative modes: transparent and translucent. The difference between the two is that the interface of the decorative role is exactly the same as that of the abstract component role. The interface of the decoration role is wider than the interface of the abstract component role in translucent situations.
As shown in:
If there is only one concretedecorator class, there is no need to create a separate decorator class. Instead, you can combine the responsibilities of decorator and concretedecorator into one class.
II, Applicable scenarios of the decoration Mode
1. You need to expand the functions of a class or add additional responsibilities to a class.
2. You need to dynamically add functions to an object. These functions can be dynamically revoked.
3. A large number of functions are generated by the arrangement and combination of some basic functions, making inheritance relations unrealistic.
Advantages of the decoration mode:
1. The purpose of the relationship between the decoration mode and inheritance is to expand the object functions, but the decoration mode can provide more flexibility than inheritance.
2. By using different specific decorative classes and the arrangement and combination of these decorative classes, designers can create a combination of many different behaviors.
III, Relationship between factory methods and other models
Adapter Mode
The decoration mode and adaptation mode are both wrapper mode. The purpose of the adapter mode is to change the interface of the object to be considered without changing the performance of the object. The purpose of the decoration mode is to maintain the interface and increase the performance of the object to be considered.
Rule Mode
The decoration mode replaces the skin of a thing and keeps it in its heart. The rule mode is the opposite. It makes specific algorithms interchangeable while keeping the interface unchanged.
The implementation of the decoration mode requires the component class to be "light" as much as possible, while the policy mode requires the abstract strategy class to be "heavy" as much as possible ".
Merging Mode
The decoration mode is often used to expand the behavior of the compositing mode. It is difficult to extend the merging mode using the inheritance relationship. The decoration mode is an alternative to the inheritance relationship. The decoration mode dynamically adds new behaviors to the compositing mode.
4. Application of decoration mode in Java
Java
I/O
Two symmetry and two design modes of Java I/O Library
Two Symmetry
1. Output-input symmetry: for example, inputstream and outputstream occupy the root of the two parallel structures of the input and output of the byte stream: reader and writer occupy the root of the two parallel hierarchical structures of the input and output of the char stream.
2. byte-Char symmetry: inputstream and reader sub-classes are responsible for the input of byte and char streams respectively, and outputstream and writer sub-classes are responsible for the output of byte and char streams respectively, they form a parallel hierarchical structure.
Two design modes
1. decoration mode: in the hierarchical structure represented by inputstream, outputstream, reader, and writer, some stream processors can play a decorative role on another top-notch processor, form a new stream processor with improved functions.
2. In the hierarchical structure represented by inputstream, outputstream, reader, and writer, some stream processors adapt to other types of processors. This is the application of the adapter mode.
InputstreamIn the decoration mode:
The decorative mode in the inputstream type is translucent. In other words, the linked stream processor modifies the interface of the source stream processor while enhancing the functions of the source stream processor. For example, bufferedinputstream is a translucent decoration class because it provides additional methods ensureopen () and fill (). If the client holds an object of the inputstream type to reference in, if bufferedinputstream is used for decoration, as long as the client does not need to use methods like ensureopen and fill, the client has no problem. However, if the client must use these two methods, it must perform a downward type conversion. This type conversion means that the client must know that the reference it obtains is directed to an object of the bufferedinputstream type, this damages the original intention of using Decoration: the client is not allowed to notice that it uses only one decoration, not the original stream role.
Reader-type decoration mode:
InputstreamIn the adapter mode:
Bytearrayinputstream inherits the inputstream interface and encapsulates a byte array. In other words, it adapts a byte array interface to an inputstream stream processor interface.
Stringbufferinputstream inherits the inputstream type and holds a reference to the string object. This is an adapter mode that adapts a String object to an inputstream object.
From byteFlow to CharStream adaptation:
Inputstreamreader is an adapter from byte input to CHR input stream.
Outputstreamwriter is an adapter that flows from byte output to Char output stream.