Article Structure – What are decorator patterns – Meaning – Examples – explanations – illustrations

What is the decorator pattern

If you have a cake, if you just put cream on it, then the cake is a cream cake, then you add strawberries and you have strawberry cake, and then you have chocolate and you write happy birthday on it, and you have a birthday cake. So whether it’s a strawberry cake ora birthday cake, the theme is cake, and the cake is decorated on top of the cake. The program object is very similar to the cake, and you can always add new functions to it. This design pattern is called the Decorator pattern.

meaning

Operation on a single object can enhance or weaken the function, and flexible requirements of the product can be added or modified

The instance

Note: from the beginning of this chapter do not want to paste code, feel that the code affects the readability of the article

Project structure analysis

We can analyze it from the current project structure: The top class is an abstract class called Disply. The abstract class has two implementation classes: one for StringDiPlay() and the other for Border(). It is obvious that Border means Border, and String means String. That’s a string; And under the Border there is a concrete implementation class, there are full Border, up and down Border, test Border three modification class

Dispalay () class

This class provides several abstract methods. As a top-level class, it provides methods to get horizontal characters, vertical characters, and rows and strings, with a string that prints or displays all rows and columns


The Border () class

As a border description class, it’s an initial implementation of level 2, but it’s a more detailed implementation, and it provides fields and methods that are protected methods that all subclasses can use

FullBorder() FullBorder class resolution

This class is a layer 3 or layer 2 implementation of the Border and Display interface. This class has all the functions of the Border and Display interface. This class gets rows and columns by +1 on the edge, so it also wraps a row on the edge. This method provides its own enclosing methods on the first and last lines (since the methods it provides are +1) So you can provide 0 and +1.) And you’re left with a getRowText() that’s actually called Display because the class that’s passed in is StringBorder and the parent of that class is Display so when it’s not the first and last line it just prints a character that’s passed in So why is -1 because the other types of this method are wrapped around each other, and the length of this method is the length of the string passed in, so -1 is used for consistency

SideBorder() sidebar class parsing

This class is an implementation of level 3 or level 2. It is a border test class so the top and bottom are either getRows(); The getRowText method then concatenates the string to be concatenated on both sides of the original string. Then add the string you passed in to both sides when the top-level DisPlay class calls show

UpDownBorder() upper and lower border class resolution

This class is a layer 3 or layer 2 implementation. It is the top border class, so when we get the length of getCloumns(), we just get the length of the string, but when we get the length of the top and bottom lines, we do the +1 operation on both sides, so we do the +1 operation on both sides. The getRowText() method provides a judgment operation, that is, it executes its own method on the first and last lines, and prints or concatenates the string passed to it according to the length of the row, instead of the first and last lines

Test this with the main method

B1, when the top-level class show() method is called on the first layer, the default is used to print it b2, and on the second layer, after getting the arguments of b1, select up and down concatenation – this operation becomes up and down – to print B3, and on the third layer, get the arguments of B2, at which point B2 is no longer the first string. It’s the concatenated string because b2 is the mateLine method that parsed the concatenation of the top and bottom border, so B3 gets the second printed argument, which is the base argument for display, to concatenate the sides and now we see the third printed method

Explanation + diagram

Component is the core class that adds functionality. In this example, the ConcreteComponent class is an entity class that implements the Custom interface for the Component role. In this case, the StringDisPlay class acts as the Decorator. This class has a Component concrete entity class as a character of the class. ConcerteDecorator is represented by FullBorder() during the demonstration. SideBorder() UpDownBorder() three classes to play