Java Design pattern — Decorator pattern

Let’s look at a picture


10-55-59.jpg

Component is an abstract construct. It is an excuse or abstract class that defines our core object, the original object.

But we have to be careful

In the decorator pattern, there must be a basic, core, primitive interface or abstract class that acts as the Component abstraction. ConcreteComponent ConcreteComponent ConcreteComponent ConcreteComponent ConcreteComponent is the core, primitive, and basic implementation of an interface or abstract class. 3. A Decorator Decorator role is usually an abstract class. An implementation interface or abstract method does not have an abstract method in it. It must have a private variable in its property pointing to the Component abstract. 4. What do we mean by concrete decorator roles? We can write specific methods in these classes. ConcreteDecoratorA and ConcreteDecoratorB are concrete decorator classes where you decorate your core, original, and basic into something else.

So that’s about it, so let’s move on to examples

When we take an exam, I generally rely on more than 40, and the class only has more than 40, then we take an exam, but also let parents sign. This can worry bad ME, but also good tact OF I came up with a way, is to say our highest score first, also did not have much higher than me, and then after dad read the report card, tell him I ranked 38 in the class, this is also the truth, why? Nearly ten students dropped out! I’m not going to talk about that. I don’t know if the school didn’t consider clearly when the report card was first issued. It didn’t write down the total number of students and the ranking. Anyway, I made a loophole.

So let’s look at this picture


13-15-53.jpg

The diagram shows an abstract class for the report card, which can be analyzed according to the previous diagram.

First, we define an abstract class named schoolReport.class

Public abstract class SchoolReprt {public abstract void report(); Public abstract void sign(String name); }Copy the code

2. Then we define a fourth grade report card FouthGradeSchoolReport integrated with the SchoolReport class, which is the concrete implementation class

Public class FouthGradeSchoolReport extends SchoolReprt {public void report() {// TODO auto-generated Method stub system.out.println (" dear XXX parents: "); System.out.println("........" ); System.out.println(" language: 62 math: 65 PE: 98 Natural: 63"); System.out.println("... ") ); System.out.println(" parent signature: "); } // Parent signature public void sign(String name) {// TODO auto-generated method Stub system.out.println (" parent signature: "+name); }}Copy the code

3. We can write Decorator classes to inherit from the SchoolReport class. These classes are the methods that decorate the SchoolReport class

Public abstract class extends SchoolReprt {private SchoolReprt sr; Public Decorator(SchoolReprt sr){this.sr = sr; } @override public void report() {// TODO auto-generated method stub this.sr.report(); } @override public void sign(String name) {// TODO auto-generated method stub this.sr.sign(name); }}Copy the code

4. We use multiple classes to decorate specific roles

Public class HighSCoreDecorator extends Decorator{public HighSCoreDecorator(SchoolReprt sr) {super(sr); // TODO auto-generated constructor stub} private void reportHighScore(){system.out.println (" Math 78, nature 80"); } // I'm going to tell dad my highest grade before he sees it, or he'll swing a broom and hit me. @override public void report() {// TODO auto-generated method stub this.reporthighScore (); super.report(); }} public class extends Decorator extends Decorator{public SortDecorator(SchoolReprt sr) {super(sr); Private void reportSort(){system.out.println (); // TODO auto-generated constructor stub} private void reportSort(){system.out.println (); } @override public void report() {// TODO auto-generated method stub super.report(); this.reportSort(); }}Copy the code

Finally, we begin to implement our Father, the master method

Public class Father {public static void main(String[] args) {public static void main(String[] args) { Sr = new FouthGradeSchoolReport(); Sr= new HighSCoreDecorator(sr); Sr = new SortDecorator(sr); Sr.report (); Sr. sign(" your dad "); }}Copy the code

The result is:

This exam Chinese is 75, math is 78, natural is 80 dear XXX parents:........ Chinese: 62 Math: 65 PE: 98 Nature: 63...... Parent signature: My ranking is 38. Parent signature: Your fatherCopy the code

So let’s analyze the use of this decorator class

Advantages:

● Decorator and decorator classes can develop independently without coupling to each other. In other words, the Component class doesn't know about the Decorator class, which extends the functionality of the Component class from outside, and the Decorator doesn't know about the body's components. ● Decoration is an alternative to inheritance. If we look at the Decorator class, no matter how many layers we decorate, the returned object is still Component and implements the IS-A relationship. ● Decorator patterns can dynamically extend the functionality of an implementation class. Needless to say, decorator patterns are defined as such.Copy the code

Disadvantages:

It's enough to keep in mind one thing about decorating patterns: multi-layer decorating is complex. Why is it complicated? If you think about it, it's like peeling an onion, and you peel it to the end and you find that there is a problem with the innermost layer of decoration, imagine the workload, so try to reduce the number of decoration classes in order to reduce the complexity of the system.Copy the code

Scenarios used

● Need to extend the functionality of a class, or add additional functionality to a class. ● You need to add functions to an object dynamically, and these functions can be undone dynamically. ● Need for a group of brother classes for modification or add functions, of course, is the preferred decoration mode.Copy the code