This article is intended for your own understanding and may cause errors. If there is any error, please point it out.
In programming, we may encounter a similar situation: a function has many steps. Different steps do not have a certain order and can be combined, but different combinations will produce different results, if we need to write a program for each combination, we may be exhausted. In addition, it is not easy for us to change the combination in the future. Similar to the example in our daily life: We can match our clothes at will, but if we fix a combination, we will have great limitations when wearing clothes. This is similar to combining program steps in a program. However, our clothes are independent and there is not much dependency between them. Similarly, if we want to implement similar functions in the program, the decoration mode is a good choice.
Take clothes as an example. For example, if we are going to wear a big T-shirt, cross-pants, and broken sneakers, we may have written a large T-shirt, cross-pants, and broken sneakers in a program, but if we want to change the order, we have to write another program. The decoration mode solves such a problem. The decoration mode can dynamically add some responsibilities to some objects. Here we wear dynamic clothes. In this case, you may not understand the code.
Here there is a person's class that represents the person who needs to wear clothes. The dress below is a parent class that stores an example of the person who wears clothes, there are three sub-categories of clothing that represent different clothes.
[Java]
Package PersonDecorate;
Public class Person
{
Public Person (){
}
Private String name;
Public Person (String name ){
This. setName (name );
}
Public void Show (){
System. out. println ("Dress Up" + name );
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
}
[Java]
Package PersonDecorate;
Class Finery extends Person {
Protected Person component;
Public void Decorate (Person component ){
This. component = component;
}
@ Override
Public void Show (){
If (component! = Null ){
Component. Show ();
}
}
}
[Java] view plaincopy
Package PersonDecorate;
Class TShirts extends Finery {
Public void Show (){
System. out. println ("big t-shirt ");
Super. Show ();
}
}
[Java]
Package PersonDecorate;
Class BigTrousers extends Finery {
Public void Show (){
System. out. println ("Cross pants ");
Super. Show ();
}
}
[Java]
Package PersonDecorate;
Class Sneakers extends Finery {
Public void Show (){
System. out. println ("Broken sneakers ");
Super. Show ();
}
}
This is relatively simple. Several pieces of clothing inherit from the clothing class, and also inherit from the Decorate method of the clothing class. This method is actually set to the person wearing clothes, but at this time, the person wearing the clothes may already have some clothes. The Show method is rewritten in the classes of these clothes, and the show method of the parent class is also called. This aims to add some new logic on the basis of the original, previously, this is achieved by calling the Show method of the parent class. The new logic outputs the corresponding clothes in my program. You can also implement other logics.
The main class is given below:
[Java]
Package PersonDecorate;
Public class Test {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Person person = new Person ("SuperMan ");
System. out. println ("first type :");
// Create three pieces of clothing
Sneakers pqx = new Sneakers ();
BigTrousers kk = new BigTrousers ();
TShirts dtx = new TShirts ();
Pqx. Decorate (person); // at this time, the person has worn a broken sneakers.
Kk. Decorate (pqx); // at this time, the person has put on a pair of broken sneakers and cross-pants.
Dtx. Decorate (kk); // at this time, the person has put on a broken sneakers, cross-pants, large T-shirt
// This is only a combination, and the order can be changed at will.
Dtx. Show ();
}
}
The result is
[Java]
First:
Big T-shirt
Cross pants
Broken sneakers
SuperMan
Note the call sequence of these Show methods. For example, in our clothes class, whether the Show method outputs the result first or calls the super. Show method first, their sequence also determines the output result.
In addition, we can also make the Person class into a parent class, and then let the people we wear inherit from it, so that we can wear clothes for different roles.
I think the main purpose of the decoration mode is to store the actual operation objects in the decoration class (this program is the clothing class), and then add some business logic for it through the subclass, the Calling sequence of the class methods is chaotic here, but if you understand it, this design mode will also be thoroughly learned.
Author: mengxiangyue