大话设计模式--装饰模式
来源:互联网 发布:tensorflow 中文 编辑:程序博客网 时间:2024/05/16 11:13
装饰模式:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活。
Component是定义了一个对象接口,可以给这些对象动态地添加职责,ConcreteComponent是定义了一个具体的对象,也可以给这些对象添加一些职责。Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的,至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。
public abstract class Component { public abstract void Operation();}
public class ConcreteComponent extends Component { @Override public void Operation() { }}
public abstract class Decorator extends Component { protected Component component; public void setComponent(Component component) { this.component = component; } @Override public void Operation() { if (component != null) { component.Operation(); } }}
public class ConcreteDecoratorA extends Decorator { private String addedState; @Override public void Operation() { super.Operation(); addedState = "New State"; System.out.println("对具体对象A的操作"); }}
public class ConcreteDecoratorB extends Decorator { @Override public void Operation() { super.Operation(); AddedBehavior(); System.out.println("具体装饰对象B的操作"); } private void AddedBehavior() { }}
public class Main { public static void main(String[] args) { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(); ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(); decoratorA.setComponent(c); decoratorB.setComponent(decoratorA); decoratorB.Operation(); }}
阅读全文
0 0
- 大话设计模式-装饰模式
- 大话设计模式-装饰模式
- 大话设计模式-装饰模式
- 大话设计模式--装饰模式
- 大话设计模式----装饰模式
- 大话设计之装饰模式
- 大话设计模式之装饰设计模式
- 我也大话设计模式--装饰模式
- 大话设计模式----装饰模式笔记
- 大话设计模式(四)装饰模式
- 大话设计模式之-----装饰模式
- 大话设计模式-装饰者模式
- 大话设计模式之装饰模式
- 大话设计模式_装饰模式
- 大话设计模式——装饰模式
- 大话设计模式读书笔记3 装饰模式
- 大话设计模式之装饰模式
- 大话设计模式之装饰模式
- 数据库(第一范式,第二范式,第三范式)
- java基础:面向对象(this和super的区别和应用)
- Codeforces Round #432 (Div. 2)
- JAVA--信号量
- JavaScript性能优化
- 大话设计模式--装饰模式
- Duilib属性表
- 转载 http://www.cnblogs.com/jingqi/articles/7349530.html django urls.py 路由用法
- Activiti进阶(五)——流程执行历史记录
- Maven搭建Maven环境及导入jar,改目录
- Python调用(运行)外部程序
- 15:银行利息
- ABAP学习系列笔记—R/3 ERP系统(一)
- iOS-Storyboad动态刷新