Learning this model is more difficult than the previous ones. It takes only half a day to understand it. Okay, maybe it's slow.
Modifier mode: The decoration mode dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object.
In the book "big talk design pattern", you need to dress up as an example to learn about the decoration pattern. My understanding of this pattern is as follows: it is to re-process an existing algorithm without changing the original code.
Well, it's better to understand it through code. The annotations in my code are very important. They are all summarized in the process of writing. It's critical to understand the decoration mode.
1. People must dress up and wear clothes, so the first target is people.
/*
* Decoration Mode
* Objects to be decorated
*/
PublicclassPerson {
PrivateStringname;
PublicPerson (){}
PublicPerson (string name ){
This. Name = Name;
}
Public VoidShow (){
System.Out. Println ("decorated" + name );
}
}
2. Decoration: I want to use clothes to decorate myself. All the decoration classes that need to be defined as the parent class of a specific decoration.
/*
* Clothing decoration category
*/
Public ClassFineryExtendsPerson {
PrivatePersonperson;
// Who to dress up
PublicvoidDecorator (person ){
This. Person = person;
}
@ Override
PublicvoidShow (){
//TodoAuto-generated method stub
If(Person! =Null){
Person. Show (); // The actual method in person is executed. Why does the finery class need to be added? Because people are decorated with clothes, there are many kinds of clothes (subclass)
}
}
PublicPerson getperson (){
ReturnPerson;
}
PublicvoidSetperson (person ){
This. Person = person;
}
}
3. T-shirts. The decorations are T-shirts.
/*
* The specific class, T-shirt, should be decorated to people
*/
PublicclassTshirtsExtendsFinery {
PublicvoidShow (){
System.Out. Println ("wear a T-shirt! "); // In actual development, this should be the process of processing human data.
Super. Show (); // process of processing the parent class
}
}
4. Pants subclass
/*
* The specific class and trousers should be decorated on people.
*/
PublicclassBigtrouserExtendsFinery {
PublicvoidShow (){
System.Out. Println ("put on your pants! ");
Super. Show ();
}
}
5. Client call class
PublicclassDecoratorclient {
/**
*@ ParamARGs
*/
PublicstaticvoidMain (string [] ARGs ){
//TodoAuto-generated method stub
Personperson =NewPerson ("I ");
Tshirtstshirts =NewTshirts ();
Bigtrousertrouser =NewBigtrouser ();
// Decoration Process
Tshirts. decorator (person );
Trouser. decorator (tshirts );
Trouser. Show ();
}
}
The output is as follows:
Put on your pants!
Wear a T-shirt!
Decorated by me
Analysis result:
When the program runs to trouser. Show (), print: put on pants !, This is okay. Another method in this method is super. Show (). The super (parent class) is a tshirts object. Why? Please refer to trouser. decorator (tshirts); this is to turn the pants parent class into a T-shirt, so the super. Show () method actually executes the system in the T-shirt class.Out. Println ("wear a T-shirt! ");Super. Show (); in these two steps, print "put on a T-shirt" and executeSuper. Show (). Who is super? According to tshirts. decorator (person); you can see that it is the person himself. This will execute the show method in the person class: system. Out. println ("decorated" + name); that is, print out the decorated me. After the introduction, it is hard for me to understand. Leave a message if you have any questions. Thank you!
Attached the structure of the decoration Mode
Summary: The decoration mode dynamically adds more functions to existing functions. When the system needs to update the function, it adds new code to the old class. These new codes usually describe the core responsibilities or main behaviors of the original class, for example, wearing a T-shirt or trousers to decorate yourself, but the problem with this approach is that they add new fields, new methods, and new logic to the main class, this increases the complexity of the main class.
Attachment Source Code address: http://download.csdn.net/detail/jzhf2012/8086335
<3> reading "big talk design mode": Decorative Mode