[Design Mode]-Decorator Mode

1. Introduction to the decorator mode

1. What is the decorator pattern

Decorator Pattern is to achieve a design pattern that can add new functions to an existing object without changing its structure. This design pattern is a structural design pattern. Under normal circumstances, we want to expand the functions or responsibilities of an existing class will be implemented by inheritance, but this method will increase with the increase of inheritance levels, there will be more and more static features, and the subclasses will be very expanded, so We need a way to reduce the problem of subclass expansion caused by inheritance. This is the decorator pattern.

2. The business scenario of the decorator pattern

The decorator pattern is commonly used in two situations:

  1. Used to expand the function or responsibility of a class
  2. It is used to expand the function of the object. This expansion is pluggable and reversible.

A classic example of the decorator pattern is Monkey King's Seventy-two Changes. When Sun Wukong didn't change his mind, he could speak of the master. When Monkey King became a bee, although he could still speak of the master, the language he used at this time became the buzzing of the bee. Although it became a bee, the bee was essentially the Monkey King. Below we use code to implement the following logic.

Second, the realization of the decorator pattern

1. Code Implementation

(1) Create physical interface

public interface MonkeySun {
    
    

	void say();

}

(2) Create an implementation class

public class MonkeyKing implements MonkeySun {
    
    
// 齐天大圣
	@Override
	public void say() {
    
    
		// 叫师傅
		System.out.println("唐sensi!");
	}

}

(3) Create abstract decoration class

public abstract class MonkeySunDecorator implements MonkeySun {
    
    

	protected MonkeySun monkeySun;

	// 强制子类实现有参构造
	public MonkeySunDecorator(MonkeySun monkeySun) {
    
    
		super();
		this.monkeySun = monkeySun;
	}

	abstract void sayDecorator();

	// 默认采用已有方法,这样在使用的时候就可以使用未被装饰的API
	@Override
	public void say() {
    
    
		monkeySun.say();

	}

}

(4) Create a decoration implementation class

public class MonkeyBeeDecorator extends MonkeySunDecorator {
    
    

	public MonkeyBeeDecorator(MonkeySun monkeySun) {
    
    
		super(monkeySun);
		// TODO Auto-generated constructor stub
	}

	@Override
	void sayDecorator() {
    
    
		super.say();
		System.out.println("嗡嗡嗡");
		
	}

}

(5) Test code

public class Test {
    
    

	public static void main(String[] args) {
    
    
		MonkeySunDecorator monkeyBeeDecorator = new MonkeyBeeDecorator(new MonkeyKing());
		monkeyBeeDecorator.say();
		monkeyBeeDecorator.sayDecorator();
	}
}

2. Implementation analysis of the decorator pattern

As shown in the figure: The
Insert picture description here
decorator mode is actually divided into two roles:

  1. Object to be decorated: Here we can understand it as a product that needs to be decorated.
  2. Decorator: The decorator here is divided into two parts. The first part is the parent class of the decorator. We usually use an abstract class to describe it. This abstract class usually implements the interface of the decorated person and passes the decorated person as a local variable through construction parameters. There may be a question that some students do not understand, why it is necessary to implement the interface and also pass it as a parameter to maintain the reference. Here is an answer for everyone: The implementation of the interface is because the decorator is essentially the previous object. Using the form of implementing the interface can enable the business party to use the original interface API without using the decoration method, and it is also passed as a parameter In order to get the API related information of the object before decoration.

Three, the characteristics of the decorator mode

1. The advantages and disadvantages of the decorator pattern

advantage:

  1. Decoration class and decorated class can be developed independently without coupling

Disadvantages:

  1. Multi-layer decoration improves code complexity

2. Reference of decorator pattern in JDK

The construction of the IO structure in the JDK is based on the decorator pattern. Interested students can read the source code of the IO class to learn.

Okay, this concludes today's content. If you have any questions, you can privately message me or leave a message in the comment area, and I will answer you as soon as possible. Friends who feel rewarded, please remember one-click three consecutive times, pay attention to bloggers, don’t get lost, and refuse to be a prostitute~

Guess you like

Origin blog.csdn.net/xiaoai1994/article/details/112858199