• 13 结构性模式-装饰器模式


    1 装饰器模式介绍

    在这里插入图片描述
    在软件设计中,装饰器模式是一种用于替代继承的技术,它通过一种无须定义子类的方式给对象动态的增加职责,使用对象之间的关联关系取代类之间的继承关系.

    2 装饰器模式原理

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    //抽象构件类
    public abstract class Component{
    	public abstract void operation();
    }
    
    • 1
    • 2
    • 3
    • 4
    //具体构建类
    public class ConcreteComponent extends Component {
    	@Override
    	public void operation() {
    	//基础功能实现(复杂功能通过装饰类进行扩展)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    /**
     * 抽象装饰类-装饰者模式的核心
     **/
    public class Decorator extends Component{
    
        //维持一个对抽象构件对象的引用
        private Component component;
    
        //通过构造注入一个抽象构件类型的对象
        public Decorator(Component component) {
            this.component = component;
        }
    
        public void operation() {
            //调用原有的业务方法,并没有真正的进行装饰,而是提供了一个统一的接口,将装饰的过程交给子类完成
            component.operation();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    /**
     * 具体装饰类
     **/
    public class ConcreteDecorator extends Decorator {
    
        public ConcreteDecorator(Component component) {
            super(component);
        }
    
        @Override
        public void operation() {
            super.operation(); //调用原有的业务方法
            add(); //调用新增的方法
        }
    
        //新增业务方法
        public void add(){
            //......
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    3 装饰器模式应用实例

    在这里插入图片描述
    在这里插入图片描述
    导入IO工具类

    <dependency>
    	<groupId>commons-io</groupId>
    	<artifactId>commons-io</artifactId>
    	<version>2.6</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    /**
     * 抽象的文件读取接口
     **/
    public interface DataLoader {
        String read();
        void write(String data);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.IOException;
    
    /**
     * 具体组件: 抽象文件读取接口的实现类
     **/
    public class BaseFileDataLoader implements DataLoader{
    
        private String filePath;
    
        public BaseFileDataLoader(String filePath) {
            this.filePath = filePath;
        }
    
        //读
        public String read() {
            try {
                String result = FileUtils.readFileToString(new File(filePath), "utf-8");
                return result;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        //写
        public void write(String data) {
            try {
                FileUtils.writeStringToFile(new File(filePath),data,"utf-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    /**
     * 抽象装饰者类
     **/
    public class DataLoaderDecorator  implements DataLoader{
    
        private DataLoader dataLoader;
    
        public DataLoaderDecorator(DataLoader dataLoader) {
            this.dataLoader = dataLoader;
        }
    
        public String read() {
            return dataLoader.read();
        }
    
        public void write(String data) {
            dataLoader.write(data);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    import java.io.UnsupportedEncodingException;
    import java.util.Base64;
    
    /**
     * 具体装饰者类-对文件内容进行加密和解密
     **/
    public class EncryptionDataDecorator extends DataLoaderDecorator {
    
        public EncryptionDataDecorator(DataLoader dataLoader) {
            super(dataLoader);
        }
    
        @Override
        public String read() {
            return decode(super.read());
        }
    
        @Override
        public void write(String data) {
            super.write(encode(data));
        }
    
        //加密操作
        public String encode(String data){
    
            try {
                Base64.Encoder encoder = Base64.getEncoder();
                byte[] bytes = data.getBytes("utf-8");
    
                String result = encoder.encodeToString(bytes);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        //解密操作
        public String decode(String data){
            try {
                Base64.Decoder decode = Base64.getDecoder();
                String result = new String(decode.decode(data),"utf-8");
    
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    4测试
    public class TestDecorator {
    	public static void main(String[] args) {
    		String info = "name:tom,age:15";
    		DataLoaderDecorator decorator = new
    		EncryptionDataDecorator(new BaseFileDataLoader("demo.txt"));
    		decorator.write(info);
    		String data = decorator.read();
    		System.out.println(data);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    5装饰器模式总结

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    华为机试 - 滑动窗口最大和
    MFC Windows 程序设计[133]之编辑框的初探
    Android codec2 编码 -- 基于录屏
    UVMbasic
    win10系统下WPS工具显示灰色全部用不了,提示登录
    Leetcode Algo Day2
    【JAVASE】日期时间类
    GPU架构与计算入门指南
    mmap()
    ASP.NET Web 应用 Docker踩坑历程
  • 原文地址:https://blog.csdn.net/weixin_39563769/article/details/134027727