欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

结构模式之装饰器模式

程序员文章站 2022-05-04 19:24:09
...
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();
	}

}