Let you easily understand the design pattern ------ Decorator Pattern

catalog What is decorator mode? Decorator structure How to achieve Test Demo test result ending What is agent mode? ...

catalog

What is decorator mode?

In fact, the decorator mode is to extend the object of an existing function without affecting the structure of the object itself. Just as we wear clothes, our body is an object. I think my body looks better, so I put on some beautiful and generous clothes (without changing the original structure), rather than having different patterns (modifying the original structure) on the body , so the decorator mode is structural.

Decorator structure

Let's look at the class diagram of the decorator:

It doesn't seem too hard to understand:

  • The Person class is the object we need to decorate. According to the above example, it is our body.
  • Decorator is the class of decoration. The main function of this class is to let subclasses decide what functions to decorate. The decorator class is not required.
  • Concretedecorator a, concretedecorator B and concretedecorator C are all subclasses of Decorator, representing specific decoration classes, such as clothes and pants in the above examples.
How to achieve

If the theory is good, we should see how to implement it. First, the Person class:

public class Person { private String name; public Person(){} public Person(String name){ this.name = name; } public void Show(){ System.out.println("Dressed up object:" + this.name); } }

Decorator class:

public class Decorator{ protected Person person; public Decorator(Person person){ this.person = person; } @Override public void Show() { if(person != null) person.Show(); } }

Classes concretedecorator a, concretedecorator B, and concretedecorator C:

class ConcreteDecoratorA extends Decorator{ public ConcreteDecoratorA(Person person) { super(person); } @Override public void Show() { super.person.Show(); System.out.println("Put on your clothes"); } } class ConcreteDecoratorB extends Decorator{ public ConcreteDecoratorB(Person person) { super(person); } @Override public void Show() { super.person.Show(); System.out.println("Put on your pants"); } } class ConcreteDecoratorC extends Decorator{ public ConcreteDecoratorC(Person person) { super(person); } @Override public void Show() { super.person.Show(); System.out.println("Put on socks"); } }
Test Demo
public class test { public static void main(String[] args) { Person person = new Person("My body"); ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(person); ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(concreteDecoratorA); ConcreteDecoratorC concreteDecoratorC = new ConcreteDecoratorC(concreteDecoratorB); concreteDecoratorC.Show(); } }
test result

ending

At this time, the problem comes!!!!!!!

A small partner put forward: listen to the brother introduced the decorator mode, why do you think it's similar to the agent mode? Both of them extend the original function without affecting its own structure. At this time, we will briefly introduce the agent mode!

What is agent mode?

The proxy mode is to provide a proxy for other objects to control the access of this object. Generally, the proxy mode has three roles: the agent, the agent and the caller. Similarly, the proxy mode is also a structural mode. For example, there are three people of a, B and C. A likes C. he wants to send a rose as a sign, but he is embarrassed (it's very normal), so he entrusts B to send the rose to C.

Then in this process, a is the agent with the function of passing roses; B is the agent with the function of passing roses; C is the caller with the function of calling roses. From the beginning to the end, the function of transmitting rose is the function of a, not b. even if C finally accepts rose, it is also the rose of A.

As for their previous differences, I will explain them in detail in the next chapter. If you like, you can like them.

9 June 2020, 04:24 | Views: 9179

Add new comment

For adding a comment, please log in
or create account

0 comments