设计模式之 -- Decorator pattern (修饰模式)
来源:互联网 发布:泰州师说网络培训登录 编辑:程序博客网 时间:2024/06/05 02:53
As an example, consider a window in a windowing system. To allow scrolling of the window's contents, we may wish to add horizontal or vertical scrollbars to it, as appropriate. Assume windows are represented by instances of the Window class, and assume this class has no functionality for adding scrollbars. We could create a subclass ScrollingWindow that provides them, or we could create a ScrollingWindowDecorator that adds this functionality to existing Window objects. At this point, either solution would be fine.
Now let's assume we also wish the option to add borders to our windows. Again, our original Window class has no support. The ScrollingWindow subclass now poses a problem, because it has effectively created a new kind of window. If we wish to add border support to all windows, we must create subclasses WindowWithBorder and ScrollingWindowWithBorder. Obviously, this problem gets worse with every new feature to be added. For the decorator solution, we simply create a new BorderedWindowDecorator—at runtime, we can decorate existing windows with the ScrollingWindowDecorator or the BorderedWindowDecorator or both, as we see fit.
Another good example of where a decorator can be desired is when there is a need to restrict access to an object's properties or methods according to some set of rules or perhaps several parallel sets of rules (different user credentials, etc.) In this case instead of implementing the access control in the original object it is left unchanged and unaware of any restrictions on its use, and it is wrapped in an access control decorator object, which can then serve only the permitted subset of the original object's interface.
- 设计模式之 -- Decorator pattern (修饰模式)
- python: 设计模式(design pattern)之修饰器模式(decorator)
- 设计模式之二(decorator pattern)
- 设计模式之Decorator Pattern
- 设计模式 - 修饰模式 (Decorator pattern) 的 Java 实现 [Preview]
- 设计模式之装饰模式(Decorator Pattern)
- 设计模式之装饰模式--- Pattern Decorator
- 设计模式笔记之装饰者模式(Decorator Pattern)
- C#设计模式之装饰者模式(Decorator Pattern)
- 设计模式总结之Decorator Pattern(装饰者模式)
- 设计模式--Decorator Pattern
- 设计模式-装饰模式(Decorator Pattern)
- 设计模式笔记之Decorator Pattern
- 装饰设计模式(decorator design pattern)
- 设计模式—Decorator Pattern
- 设计模式系列之四:装饰者模式(Decorator Pattern)
- 设计模式之装饰者模式Decorator Pattern
- 设计模式(二)之装饰模式(Decorator Pattern)
- 从一直没实现的计划想到的
- mini2440的LEDS驱动程序和测试程序详解
- 页面抓取特定链接
- NetSNS二次开发系列1--文章系列简介
- 编程修养(2)
- 设计模式之 -- Decorator pattern (修饰模式)
- 浅谈如何利用__doPostBack()函数做自己灵活的AJAX.NET应用
- mysql修改表结构alter,转自:http://waryist.javaeye.com/blog/104739 waryist的blog
- 智能卡的报文鉴别码和加密代码
- 忘记xp下db2管理员密码怎么办?
- vim配置成ide[zt]
- 详解Windows、Linux中“可信路径”安全机制
- 设计模式之 -- Facade pattern (外观模式)
- 使用反射调用类静态方法