Detailed description of the Java decorator mode and detailed description of the java decorator Mode
Reprinted please indicate the source: http://blog.csdn.net/zhaoyanjun6/article/details/56488020
Preface
In the previous articles, the commonIORelevant knowledge, found in the learning processIOThe package uses a large number of decorators for thorough learning.IOToday, we will unveil the decoration device model.
In order to understand the essence of the decoration device model, I have read a lot of materials and found that many articles are either bitter or the examples are inappropriate.
In fact, we can understand the decorator mode in this way. Let's take your own example. You can think of yourself as a decoration object. Your shoes, your clothes, your coats, your watches, your hats, and so on are all your decorations. The relationship between you and these decorations is the relationship between decoration and decoration.
Instance display
Now we can use the code method to understand this concept.
First, we found that both naked people and your shoes and hats have the display function. We call it the show method.
We define an interface with the display function, that is, show (),
Package com. user;/***** define interface * @ author T **/public interface AbstractPerson {// void show ();}
Now we should define a naked self, Me class
Package com. user;/*** defines a specific person, that is, the decorated person * @ author T **/public class Me implements AbstractPerson {@ Override public void show () {System. out. println ("I didn't wear anything, I showed naked ");}}
Next, we should define the abstract parent category of shoes, hats, watches, and so on. In fact, the abstract parent class has a constructor. The parameters in the constructor are abstract humans. The usage here is clever. This is also an essential step to implement the decoration function.
Package com. user;/*** define abstract ornament * @ author T **/public abstract class AbstractClothes implements AbstractPerson {AbstractPerson abstractPerson; public AbstractClothes (AbstractPerson abstractPerson AbstractPerson) {this. abstractPerson = abstractPerson;} @ Override public void show () {abstractPerson. show ();}}
As defined below, the Hat ornament Hat class inherits the AbstractClothes class.
Package com. user;/*** Hat ornament * @ author T **/public class Hat extends AbstractClothes {public Hat (AbstractPerson abstractPerson) {super (abstractPerson );} @ Override public void show () {super. show (); say ();} public void say () {System. out. println ("show me a hat ");}}
Define Shoes for shoe decoration and inherit from AbstractClothes
Package com. user;/*** shoe ornament * @ author T **/public class Shoes extends AbstractClothes {public Shoes (AbstractPerson abstractPerson) {super (abstractPerson );} @ Override public void show () {super. show (); say ();} public void say () {System. out. println ("show a pair of shoes ");}}
Create Test
Package com. user; public class Test {public static void main (String [] args) {// create Me me = new Me (); // a naked person is decorated with a Hat and has the ability to show a hat. Hat = new hat (me); // a person with a Hat is decorated with a shoe, shoes shoes = new Shoes (hat); shoes. show ();}}
Running result:
I don't wear anything. I'm naked. I'm presenting a hat. I'm presenting a pair of shoes.
Class diagram of the decoration Mode
After learning a small example, we try to summarize the class diagram of the decoration device mode.
Decorator pattern class diagram:
- Component abstract Component role: the real object and decoration object have the same interface. In this way, client objects can interact with decorative objects in the same way as real objects.
- ConcreteCompoent specific build role (real object): defines a class that will receive additional responsibilities.
- Decorator: Hold a reference to an abstract component. The decoration object accepts requests from all clients and forwards these requests to real objects. In this way, new functions can be added before and after real object calls.
- ConcreteDecorate: adds new functions to component objects.
Application of installation mode in Java I/O Library
IO stream Implementation Details:
- Component abstract Component role: InputStream, OutputStream, Reader, and Writer in the io stream
- The specific component role of ConcreteComponent: FileInputStream and FileOutputStream In the io stream
- Decorate: Hold the reference of the abstract component, FilterInputStream, FilterOutputStream
- ConcreteDecorate: responsible for adding new responsibilities to component objects, BufferedInputStream, BufferedOutputStream, etc.
Advantages
- The extended object function is more flexible than inheritance, and does not cause a sharp increase in the number of classes.
- You can decorate an object multiple times to create a combination of different behaviors to obtain more powerful objects.
- The specific component classes and decoration classes can be changed independently. You can add new child classes and decoration classes as needed.
Disadvantages
- Produce many small objects. A large number of small objects occupy the memory, which affects performance to a certain extent.
- The decoration mode is prone to errors and debugging and troubleshooting is troublesome.
Summary
- Decorate is also called Wrapper)
- The decoration mode reduces the Coupling Degree of the system, dynamically increases or deletes the responsibility of objects, and independently changes the specific building class and decoration class to be decorated, to add new build classes and decoration classes.
References:
Java Design Pattern learning 09 -- decoration pattern
Java and mode: Decorator Mode