设计模式——装饰模式

被装饰对象 调用 装饰对象的同一个接口方法,实现被装饰对象的方法


//定义共用接口

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");
     }
}

posted @ 2020-03-25 18:35  小海_macro  阅读(79)  评论(0编辑  收藏  举报