package com.eric.design.decorator;
//单向适配器模式就是在主模块上接入另外一个模块,用继承法
//装饰器模式特点:主模块注入到扩展模块中
interface IRead{
public void read();
}
class ReadStrFromFile implements IRead{//主模块
public void read(){
System.out.println("ReadStrFromFile.read()");
}
}
class ReadStrFromNet implements IRead{//主模块
public void read(){
System.out.println("ReadStrFromNet.read()");
}
}
class Convert{//扩展模块
private IRead iRead;
public void setIRead(IRead iRead){
this.iRead = iRead;
}
public void convert(){
iRead.read();
}
}
public class Decorator {
public static void main(String[] args) {
// TODO Auto-generated method stub
Convert convert = new Convert();
//convert.setIRead(new ReadStrFromFile());
convert.setIRead(new ReadStrFromNet());
convert.convert();
}
}
结构模式之装饰器模式
最新推荐文章于 2024-09-03 20:17:53 发布