被装饰对象 调用 装饰对象的同一个接口方法,实现被装饰对象的方法
//定义共用接口
public interface ILog {
public void log(String message);
}
public class UserLog implements ILog{
@Override
public void log(String message) {
// TODO Auto-generated method stub
System.out.println("装饰对象的方法"+message);
}
}//
public class Decorator implements ILog{
private ILog usrLog;public Decorator(ILog usrLog) {
super();
this.usrLog = usrLog;
}@Override
public void log(String message) {
// TODO Auto-generated method stub
System.out.println("被装饰前");
this.usrLog.log(message);
System.out.println("被装饰后");
}
}
public class Demo {
public static void main(String[] args) {
Decorator decorator = new Decorator(new UserLog());
decorator.log("hello Decorator model");
}
}