A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 〃Mr.Zぐ 中级黑马   /  2013-5-31 18:54  /  1793 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package decorator;
public class Demo {     
   
    public static void main(String[] args)
    {     
        Component component = new ConcreteComponent();     
        component.operation();     
        System.out.println("----------------------------------------");     
        component = new BeforeDecorator(new ConcreteComponent());     
        component.operation();  
      }
}
——————————————————————————————————
/**   
* 业务接口   
*/     
public interface Component {     
     
    void operation();     
}  
——————————————————————————————
/**   
* 具体业务类.   
*/     
public class ConcreteComponent implements Component {     
     
    public void operation() {     
        System.out.println("I'm "+this.getClass().getName());     
    }     
     
}  
——————————————————————————————————————
/**   
* 在业务执行前加点额外的操作.   
*/     
public class BeforeDecorator extends Decorator {     
     
    public BeforeDecorator(Component component) {     
        super(component);     
    }     
     
    public void operation() {     
        before();     
        super.operation();     
    }     
     
    private void before() {     
        System.out.println("before: I'm "+this.getClass().getName());     
    }     
     
}
以上代码补全
/**   
* 装饰类.   
*/     
public class Decorator implements Component {     
     
    private Component component;     
     
    public Decorator(Component component) {     
        this.component = component;     
    }     
     
    public void operation() {     
        component.operation();     
    }     
     
}   

BeforeDecorator 类的 super.operation();     
为什么会输出I'm decorator.ConcreteComponent

评分

参与人数 1技术分 +1 收起 理由
曹睿翔 + 1 很给力!

查看全部评分

2 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马