Decorator Mode of Design Mode (4)

Decorator mode

concept

  1. Decorator Pattern allows adding new features to an existing object without changing its structure. This type of design pattern belongs to the structural pattern, which serves as a wrapper for the existing class.

  2. This mode creates a decorative class to wrap the original class and provides additional functions while keeping the class method signature intact.

Features (mode definition)

  • Component (abstract component): It is the common parent class of decorative and concrete components.
  • ConcreteComponent (concrete component): It is a subclass of abstract component objects, used to define concrete component objects.
  • Decorator (abstract decoration class): Holds a reference to a specific component role and defines an interface consistent with the abstract component role.
  • ConcreteDecorator (concrete decoration class): implements the abstract decoration class, which is responsible for adding new responsibilities to the component.

scenes to be used

  1. Extend the functionality of a class.
  2. Dynamic increase function, dynamic cancellation.

Pros and cons

  • Advantages: The
    decorated class and the decorated class can be extended independently and will not be coupled with each other. The decoration mode is an alternative mode of inheritance, and the decoration mode can dynamically extend the function of an implementation class.

  • Disadvantages:
    More classes will appear, making the program more complicated.

The structure of the pattern

    Please refer to the diagram (picture from the Internet):

  1. Shape represents an abstract component (equivalent to GatewayComponent in this article ).
  2. Circle and Reactangle represent concrete components (equivalent to IpOrOriginBlackDecorator in this article ).
  3. ShapeDecorator represents an abstract decoration class (equivalent to AbstractDecorator in this article ).
  4. RedShapeDecorator represents a specific decoration class (equivalent to LogDecorator and AccessControlDecorator in this article ).

Insert picture description here

Development steps

  • Define abstract components
package com.lee.decorate.service;

/**
 * @author zfl_a
 * @Desc 网关组件-抽象构件(是装饰类和具体构件的公共父类)
 * @date 2020/8/14
 * @project springboot_design_pattern
 */
public abstract class GatewayComponent {
    
    

    /**
     * 定义公共的行为标准
     */
    public abstract void accessManager();
}

  • Define specific components
package com.lee.decorate.service.impl;

import com.lee.decorate.service.GatewayComponent;

/**
 * @author zfl_a
 * @Desc 具体附件:它是抽象构件对象的子类,用来定义具体的构件对象(被装饰者)
 * @date 2020/8/14
 * @project springboot_design_pattern
 */
public class IpOrOriginBlackDecorator extends GatewayComponent {
    
    

    @Override
    public void accessManager() {
    
    

        System.out.println("【1、被装饰者】主要负责IP黑名单验证");
    }
}


  • Define abstract decoration class
package com.lee.decorate.service;

import com.lee.decorate.service.GatewayComponent;

/**
 * @author zfl_a
 * @Desc 抽象装饰者:持有对具体构件角色的引用并定义与抽象接口角色一致的接口
 * @date 2020/8/14
 * @project springboot_design_pattern
 */
public class AbstractDecorator extends GatewayComponent {
    
    

    public GatewayComponent gatewayComponent;

    public AbstractDecorator() {
    
    
    }

    public AbstractDecorator(GatewayComponent gatewayComponent) {
    
    
        this.gatewayComponent = gatewayComponent;
    }

    @Override
    public void accessManager() {
    
    
        if (gatewayComponent != null) {
    
    
            gatewayComponent.accessManager();
        }
    }
}


  • Define a specific decoration class

      LogDecorator

package com.lee.decorate.service.impl;

import com.lee.decorate.service.AbstractDecorator;
import com.lee.decorate.service.GatewayComponent;

/**
 * @author zfl_a
 * @Desc 具体装饰者,实现抽象装饰者的角色,负责对具体构件添加额外的功能
 * @date 2020/8/14
 * @project springboot_design_pattern
 */
public class LogDecorator extends AbstractDecorator {
    
    

    public LogDecorator(GatewayComponent gatewayComponent) {
    
    
        super(gatewayComponent);
    }

    @Override
    public void accessManager() {
    
    
        super.accessManager();
        System.out.println("【2、日志】主要集成了日志功能");
    }
}


      AccessControlDecorator

package com.lee.decorate.service.impl;

import com.lee.decorate.service.AbstractDecorator;
import com.lee.decorate.service.GatewayComponent;

/**
 * @author zfl_a
 * @Desc 具体装饰者,实现抽象装饰者的角色,负责对具体构件添加额外的功能
 * @date 2020/8/14
 * @project springboot_design_pattern
 */
public class AccessControlDecorator extends AbstractDecorator {
    
    

    public AccessControlDecorator(GatewayComponent gatewayComponent) {
    
    
        super(gatewayComponent);
    }

    @Override
    public void accessManager() {
    
    
        super.accessManager();
        System.out.println("【3、权限检查】主要负责判断是否开启了权限和动态权限加载及匹配工作");
    }
}

  • Define factory test class
package com.lee.decorate.factory;

import com.lee.decorate.service.GatewayComponent;
import com.lee.decorate.service.impl.AccessControlDecorator;
import com.lee.decorate.service.impl.IpOrOriginBlackDecorator;
import com.lee.decorate.service.impl.LogDecorator;

/**
 * @author zfl_a
 * @Desc 工厂
 * @date 2020/8/14
 * @project springboot_design_pattern
 */
public class GatewayFactory {
    
    

    public GatewayComponent getGatewayCompoent(){
    
    
        AccessControlDecorator accessControlDecorator = new AccessControlDecorator(new LogDecorator(new IpOrOriginBlackDecorator()));
        return accessControlDecorator ;
    }

    public static void main(String[] args) {
    
    
        // 测试
        GatewayComponent gatewayCompoent = new GatewayFactory().getGatewayCompoent();
        gatewayCompoent.accessManager();
    }
}


The test results are as follows:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37640410/article/details/108614038