装饰模式(学习笔记)

  1. 意图

  动态的给一个对象添加一些额外的职责。就增长功能来讲,Decorator模式相比生成子类更为灵活java

  2. 动机

  在某些状况下,咱们可能会“过分的使用继承来扩展对象的功能”。继承是静态的,没法在运行时更改已有对象的行为只能使用由不一样子类建立的对象来替代当前的整个对象;而且随着子类的增多(扩展功能的增多),各类子类的组合(扩展功能的组合)会致使子类的膨胀app

  3. 适用性

  • 在不影响其余对象的状况下,以动态的、透明的方式给单个对象添加指责
  • 当不能采用生成子类的方法进行扩充时。一种状况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增加。另外一种状况是,类定义被隐藏,或类定义不能用于生成子类
  • 处理一些能够撤销的职责

  4. 结构

 

  5. 效果

  1) 比静态继承更加灵活ide

  2) 能够在运行时,添加和删除对象功能函数

  3) 避免在层次结构高层的类有太多的特征学习

  4) 有不少小对象。 对了解系统的人来讲,很容易对它们进行定制,可是很难学习这些系统,排错也很困难this

  6. 代码实现

   decorators/DataSource.java: 定义了读取和写入操做的通用数据接口加密

package decorator.decorators;

/**
 * @author GaoMing
 * @date 2021/7/12 - 16:48
 */
public interface DataSource {
    void writeData(String data);

    String readData();
}

  decorators/FileDataSource.java: 简单数据读写器spa

package decorator.decorators;

import java.io.*;

/**
 * @author GaoMing
 * @date 2021/7/12 - 16:49
 */
public class FileDataSource implements DataSource{
    private String name;

    public FileDataSource(String name) {
        this.name = name;
    }

    @Override
    public void writeData(String data) {
        File file = new File(name);
        try (OutputStream fos = new FileOutputStream(file)) {
            fos.write(data.getBytes(), 0, data.length());
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    @Override
    public String readData() {
        char[] buffer = null;
        File file = new File(name);
        try (FileReader reader = new FileReader(file)) {
            buffer = new char[(int) file.length()];
            reader.read(buffer);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        return new String(buffer);
    }
}

  decorators/DataSourceDecorator.java: 抽象基础装饰3d

package decorator.decorators;

/**
 * @author GaoMing
 * @date 2021/7/12 - 16:51
 */
public class DataSourceDecorator implements DataSource{
    private DataSource wrappee;

    DataSourceDecorator(DataSource source) {
        this.wrappee = source;
    }

    @Override
    public void writeData(String data) {
        wrappee.writeData(data);
    }

    @Override
    public String readData() {
        return wrappee.readData();
    }
}

  decorators/EncryptionDecorator.java: 加密装饰代理

package decorator.decorators;

import java.util.Base64;

/**
 * @author GaoMing
 * @date 2021/7/12 - 16:53
 */
public class EncryptionDecorator extends DataSourceDecorator{
    public EncryptionDecorator(DataSource source) {
        super(source);
    }

    @Override
    public void writeData(String data) {
        super.writeData(encode(data));
    }

    @Override
    public String readData() {
        return decode(super.readData());
    }

    private String encode(String data) {
        byte[] result = data.getBytes();
        for (int i = 0; i < result.length; i++) {
            result[i] += (byte) 1;
        }
        return Base64.getEncoder().encodeToString(result);
    }

    private String decode(String data) {
        byte[] result = Base64.getDecoder().decode(data);
        for (int i = 0; i < result.length; i++) {
            result[i] -= (byte) 1;
        }
        return new String(result);
    }
}

  decorators/CompressionDecorator.java: 压缩装饰

package decorator.decorators;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

/**
 * @author GaoMing
 * @date 2021/7/12 - 16:54
 */
public class CompressionDecorator extends DataSourceDecorator{
    private int compLevel = 6;

    public CompressionDecorator(DataSource source) {
        super(source);
    }

    public int getCompressionLevel() {
        return compLevel;
    }

    public void setCompressionLevel(int value) {
        compLevel = value;
    }

    @Override
    public void writeData(String data) {
        super.writeData(compress(data));
    }

    @Override
    public String readData() {
        return decompress(super.readData());
    }

    private String compress(String stringData) {
        byte[] data = stringData.getBytes();
        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream(512);
            DeflaterOutputStream dos = new DeflaterOutputStream(bout, new Deflater(compLevel));
            dos.write(data);
            dos.close();
            bout.close();
            return Base64.getEncoder().encodeToString(bout.toByteArray());
        } catch (IOException ex) {
            return null;
        }
    }

    private String decompress(String stringData) {
        byte[] data = Base64.getDecoder().decode(stringData);
        try {
            InputStream in = new ByteArrayInputStream(data);
            InflaterInputStream iin = new InflaterInputStream(in);
            ByteArrayOutputStream bout = new ByteArrayOutputStream(512);
            int b;
            while ((b = iin.read()) != -1) {
                bout.write(b);
            }
            in.close();
            iin.close();
            bout.close();
            return new String(bout.toByteArray());
        } catch (IOException ex) {
            return null;
        }
    }
}

  Demo.java: 客户端代码

package decorator;

import decorator.decorators.*;

/**
 * @author GaoMing
 * @date 2021/7/12 - 17:03
 */
public class Demo {
    public static void main(String[] args) {
        String salaryRecords = "Name,Salary\nJohn Smith,100000\nSteven Jobs,912000";
        DataSourceDecorator encoded = new CompressionDecorator(
                new EncryptionDecorator(
                        new FileDataSource("out/OutputDemo.txt")));
        encoded.writeData(salaryRecords);
        DataSource plain = new FileDataSource("out/OutputDemo.txt");

        System.out.println("- Input ----------------");
        System.out.println(salaryRecords);
        System.out.println("- Encoded --------------");
        System.out.println(plain.readData());
        System.out.println("- Decoded --------------");
        System.out.println(encoded.readData());
    }
}

  运行结果

- Input ----------------
Name,Salary
John Smith,100000
Steven Jobs,912000
- Encoded --------------
Zkt7e1Q5eU8yUm1Qe0ZsdHJ2VXp6dDBKVnhrUHtUe0sxRUYxQkJIdjVLTVZ0dVI5Q2IwOXFISmVUMU5rcENCQmdxRlByaD4+
- Decoded --------------
Name,Salary
John Smith,100000
Steven Jobs,912000 

  7. 与其余模式的关系

  • 适配器模式能够对已有对象的接口进行修改,装饰模式则能在不改变对象接口的前提下强化对象功能。此外,装饰还支持递归组合,适配器则没法实现
  • 适配器能为被封装对象提供不一样的接口,代理模式能为对象提供相同的接口,装饰则能为对象提供增强的接口
  • 装饰可以让你更改对象的外表,策略模式则让你可以改变其本质
  • 装饰和代理有着类似的结构,可是其意图却很是不一样。这两个模式的构建都基于组合原则,也就是说一个对象应该将部分工做委派给另外一个对象。二者之间的不一样之处在于代理一般自行管理其服务对象的生命周期,而装饰的生成则老是由客户端进行控制

  8. 已知应用  

  • java.io.InputStream、Output­Stream、Reader 和 Writer 的全部代码都有以自身类型的对象做为参数的构造函数

 (转载) 

  • java.util.Collections;checked­XXX()、synchronized­XXX() 和 unmodifiable­XXX() 方法
  • javax.servlet.http.HttpServletRequestWrapper 和 Http­Servlet­Response­Wrapper

  识别方法: 装饰可经过以当前类或对象为参数的建立方法或构造函数来识别

相关文章
相关标签/搜索