1 装饰器模式介绍
在软件设计中,装饰器模式是一种用于替代继承的技术,它通过一种无须定义子类的方式给对象动态的增加职责,使用对象之间的关联关系取代类之间的继承关系.
2 装饰器模式原理
//抽象构件类
public abstract class Component{public abstract void operation();
}
//具体构建类
public class ConcreteComponent extends Component {@Overridepublic void operation() {//基础功能实现(复杂功能通过装饰类进行扩展)}
}
/*** 抽象装饰类-装饰者模式的核心**/
public class Decorator extends Component{//维持一个对抽象构件对象的引用private Component component;//通过构造注入一个抽象构件类型的对象public Decorator(Component component) {this.component = component;}public void operation() {//调用原有的业务方法,并没有真正的进行装饰,而是提供了一个统一的接口,将装饰的过程交给子类完成component.operation();}
}
/*** 具体装饰类**/
public class ConcreteDecorator extends Decorator {public ConcreteDecorator(Component component) {super(component);}@Overridepublic void operation() {super.operation(); //调用原有的业务方法add(); //调用新增的方法}//新增业务方法public void add(){//......}
}
3 装饰器模式应用实例
导入IO工具类
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version>
</dependency>
/*** 抽象的文件读取接口**/
public interface DataLoader {String read();void write(String data);
}
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();}}
}
/*** 抽象装饰者类**/
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);}
}
import java.io.UnsupportedEncodingException;
import java.util.Base64;/*** 具体装饰者类-对文件内容进行加密和解密**/
public class EncryptionDataDecorator extends DataLoaderDecorator {public EncryptionDataDecorator(DataLoader dataLoader) {super(dataLoader);}@Overridepublic String read() {return decode(super.read());}@Overridepublic 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;}
}
4测试
public class TestDecorator {public static void main(String[] args) {String info = "name:tom,age:15";DataLoaderDecorator decorator = newEncryptionDataDecorator(new BaseFileDataLoader("demo.txt"));decorator.write(info);String data = decorator.read();System.out.println(data);}
}
5装饰器模式总结