关于装饰(Decorator)模式的一个问题

slg_sparkler 2006-11-02 12:25:48
大家对装饰模式了解么?看了这个模式后有点模糊,请明白人指教!
我对装饰模式的理解:
具体装饰角色通过抽象接口和具体构件角色形成关联,具体装饰角色通过修改和扩展具体构件角色的行为来达到对具体构件角色的扩展! 感觉这个和继承差不多阿,那为什么装饰模式更好呢?

我看一本书上说:装饰模式允许系统动态地决定增加一个需要的装饰或者去掉一个不需要的装饰,而继承却不能,何以理解?
...全文
340 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
weimenren 2006-11-27
  • 打赏
  • 举报
回复
看书的时间认真一点,使用的时间仔细一点

Composite versus Decorator versus Proxy

Composite (163) and Decorator (175) have similar structure diagrams, reflecting the fact that both rely on recursive composition to organize an open-ended number of objects. This commonality might tempt you to think of a decorator object as a degenerate composite, but that misses the point of the Decorator pattern. The similarity ends at recursive composition, again because of differing intents.

Decorator is designed to let you add responsibilities to objects without subclassing. It avoids the explosion of subclasses that can arise from trying to cover every combination of responsibilities statically. Composite has a different intent. It focuses on structuring classes so that many related objects can be treated uniformly, and multiple objects can be treated as one. Its focus is not on embellishment but on representation.

These intents are distinct but complementary. Consequently, the Composite and Decorator patterns are often used in concert. Both lead to the kind of design in which you can build applications just by plugging objects together without defining any new classes. There will be an abstract class with some subclasses that are composites, some that are decorators, and some that implement the fundamental building blocks of the system. In this case, both composites and decorators will have a common interface. From the point of view of the Decorator pattern, a composite is a ConcreteComponent. From the point of view of the Composite pattern, a decorator is a Leaf. Of course, they don't have to be used together and, as we have seen, their intents are quite different.

Another pattern with a structure similar to Decorator's is Proxy (207). Both patterns describe how to provide a level of indirection to an object, and the implementations of both the proxy and decorator object keep a reference to another object to which they forward requests. Once again, however, they are intended for different purposes.

Like Decorator, the Proxy pattern composes an object and provides an identical interface to clients. Unlike Decorator, the Proxy pattern is not concerned with attaching or detaching properties dynamically, and it's not designed for recursive composition. Its intent is to provide a stand-in for a subject when it's inconvenient or undesirable to access the subject directly because, for example, it lives on a remote machine, has restricted access, or is persistent.

In the Proxy pattern, the subject defines the key functionality, and the proxy provides (or refuses) access to it. In Decorator, the component provides only part of the functionality, and one or more decorators furnish the rest. Decorator addresses the situation where an object's total functionality can't be determined at compile time, at least not conveniently. That open-endedness makes recursive composition an essential part of Decorator. That isn't the case in Proxy, because Proxy focuses on one relationship—between the proxy and its subject—and that relationship can be expressed statically.

These differences are significant because they capture solutions to specific recurring problems in object-oriented design. But that doesn't mean these patterns can't be combined. You might envision a proxy-decorator that adds functionality to a proxy, or a decorator-proxy that embellishes a remote object. Although such hybrids might be useful (we don't have real examples handy), they are divisible into patterns that are useful.

czlc 2006-11-27
  • 打赏
  • 举报
回复
对于功能A,B,C,具体构件角色D来说。
要实现为D添加A,B,C3个功能,必须继承3层,而且如果只有一个继承体系的话,它的顺序也就固定了,(A->B->C->D)。
而使用装饰模式可以动态new(A(new B(C)))包裹多层,任意组合。
yun15291li 2006-11-22
  • 打赏
  • 举报
回复
我也觉得IO没学好。
GODProbe 2006-11-20
  • 打赏
  • 举报
回复
使用装饰模式是为了不用产生太多的类——这个有道理。
有时候感觉装饰模式像一个方法组装器,又类似于门面(对象方法组装器)
讨论模式应该结合具体应用情况,有时候应用业务逻辑的建模不适合用继承关系,又符合装饰模式的前提(层层封装、互相解耦),那才是装饰模式的地方。
其实TMD说白了单独看装饰、门面、代理...这些模式有什么区别啊,都差不多!
zhaoliangsuper 2006-11-17
  • 打赏
  • 举报
回复
使用装饰模式是为了不用产生太多的类,但是它会产生很多的对象,java的IO里面就有好多的装饰模式,譬如说你要实现一个东西,用到了三个东西,这些东西都是继承于同一个类,其中有两个部分需要动态的变化的这个时候用装饰模式就不错,你可以再看看模版模式应该就会理解的更多了
synico 2006-11-06
  • 打赏
  • 举报
回复
书上不是说了么,继承是静态的关系,在编译器就决定了。也就是说当你extends一个class的时候他们俩的关系就确定了。而使用装饰模式可以在运行期决定class之间的关系,为class增加新的方法,功能。我想就是一个用extends而一个不用,装饰模式使用了点手段达到了服用。打个比方人是个class继承自creature,那么人和creature的关系自人诞生起就决定了,呵呵,这个比方有点不是很恰当哈,如果给人加上好人这个装饰,他就是好人了,如果加坏人装饰,他就是坏人了。可以看出来决定一个人的好坏,不是天生的,这正应证了“人之初,性本善”。而是由外界环境决定,此时外界环境就相当于装饰了。解释不正之处请指正。
killme2008 2006-11-03
  • 打赏
  • 举报
回复
JAVA的io库就是最好的例子呐
zzhzzh204553 2006-11-03
  • 打赏
  • 举报
回复
class A{
void eat();
}

class DA{
A a;
void eat(){
//吃饭先 洗手.
洗手.
a.eat();  
}

}

这样你就很容易决定,
要在吃饭前和吃饭后要做些什么.
usherlight 2006-11-02
  • 打赏
  • 举报
回复
模式和继承本来就不是一个概念啊。
一个语言的特性,一个编程的方式方法。

C#设计模式(1) 一、 C# 面向对象程序设计复习 二、 设计模式举例 三、 先有鸡还是先有蛋? 四、 大瓶子套小瓶子还是小瓶子套大瓶子? 五、 .net本质 C#设计模式(2) 一、 "开放-封闭"原则(OCP) 二、 里氏代换原则(LSP) C#设计模式(3) 三、 依赖倒置原则(DIP) 四、 接口隔离原则(ISP) 五、 合成/聚合复用原则(CARP) 六、 迪米特法则(LoD) C#设计模式(4)-Simple Factory Pattern 一、 简单工厂(Simple Factory)模式 二、 Simple Factory模式角色与结构: 三、 程序举例: 四、 Simple Factory模式演化 五、 优点与缺点: C#设计模式(5)-Factory Method Pattern 一、 工厂方法(Factory Method)模式 二、 Factory Method模式角色与结构: 三、 程序举例: 四、 工厂方法模式与简单工厂模式 五、 Factory Method模式演化 六、 Factory Method模式与其它模式的关系 七、 另外一个例子 C#设计模式(6)-Abstract Factory Pattern 一、 抽象工厂(Abstract Factory)模式 二、 Abstract Factory模式的结构: 三、 程序举例: 四、 在什么情形下使用抽象工厂模式: 五、 抽象工厂的起源 六、 Abstract Factory模式在实际系统中的实现 七、 "开放-封闭"原则 C#设计模式(7)-Singleton Pattern 一、 单例(Singleton)模式 二、 Singleton模式的结构: 三、 程序举例: 四、 在什么情形下使用单例模式: 五、 Singleton模式在实际系统中的实现 六、 C#中的Singleton模式 C#设计模式(8)-Builder Pattern 一、 建造者(Builder)模式 二、 Builder模式的结构: 三、 程序举例: 四、 建造者模式的活动序列: 五、 建造者模式的实现: 六、 建造者模式的演化 七、 在什么情况下使用建造者模式 C#设计模式(9)-Prototype Pattern 一、 原型(Prototype)模式 二、 Prototype模式的结构: 三、 程序举例: 四、 带Prototype Manager的原型模式 五、 浅拷贝与深拷贝 六、 Prototype模式的优点与缺点 C#设计模式(10)-Adapter Pattern 一、 适配器(Adapter)模式 二、 类的Adapter模式的结构: 三、 类的Adapter模式示意性实现: 四、 对象的Adapter模式的结构: 五、 对象的Adapter模式示意性实现: 六、 在什么情况下使用适配器模式 七、 一个实际应用Adapter模式的例子 八、 关于Adapter模式的讨论 C#设计模式(11)-Composite Pattern 一、 合成(Composite)模式 二、 合成模式概述 三、 安全式的合成模式的结构 四、 安全式的合成模式实现 五、 透明式的合成模式结构 六、 透明式的合成模式实现 七、 使用合成模式时考虑的几个问题 八、 和尚的故事 九、 一个实际应用Composite模式的例子 C#设计模式(12)-Decorator Pattern 一、 装饰Decorator模式 二、 装饰模式的结构 三、 装饰模式示例性代码 四、 装饰模式应当在什么情况下使用 五、 装饰模式实际应用的例子 六、 使用装饰模式的优点和缺点 七、 模式实现的讨论 八、 透明性的要求 九、 装饰模式在.NET中的应用 C#设计模式(13)-Proxy Pattern 一、 代理(Proxy)模式 二、 代理的种类 三、 远程代理的例子 四、 代理模式的结构 五、 代理模式示例性代码 六、 高老庄悟空降八戒 七、 不同类型的代理模式 八、 代理模式实际应用的例子 设计模式(14)-Flyweight Pattern 一、 享元(Flyweight)模式 二、 单纯享元模式的结构 三、 单纯享元模式的示意性源代码 四、 复合享元模式的结构 五、 一个咖啡摊的例子 六、 咖啡屋的例子 七、 享元模式应当在什么情况下使用 八、 享元模式的优点和缺点 设计模式(15)-Facade Pattern 一、 门面(Facade)模式 二、 门面模式的结构 三、 门面模式的实现 四、 在什么情况下使用门面模式 五、 一个例子 六、 使用门面模式的设计 设计模式(16)-Bridge Pattern 一、 桥梁(Bridge)模式 二、 桥梁模式的结构 三、 桥梁模式的示意性源代码 四、 调制解调器问题 五、 另外一个实际应用Bridge模式的例子 六、 在什么情况下应当使用桥梁模式 设计模式(17)-Chain of Responsibility Pattern 一、 职责链(Chain of Responsibility)模式 二、 责任链模式的结构 三、 责任链模式的示意性源代码 四、 纯的与不纯的责任链模式 五、 责任链模式的实际应用案例 六、 责任链模式的实现 设计模式(18)-Command Pattern 一、 命令(Command)模式 二、 命令模式的结构 三、 命令模式的示意性源代码 四、 玉帝传美猴王上天 五、 命令模式的实现 六、 命令模式的实际应用案例 七、 在什么情况下应当使用命令模式 八、 使用命令模式的优点和缺点 设计模式(19)-Observer Pattern 一、 观察者(Observer)模式 二、 观察者模式的结构 三、 观察者模式的示意性源代码 四、 C#中的Delegate与Event 五、 一个实际应用观察者模式的例子 六、 观察者模式的优缺点 设计模式(20)-Visitor Pattern 一、 访问者(Visitor)模式 二、 访问者模式的结构 三、 示意性源代码 四、 一个实际应用Visitor模式的例子 五、 在什么情况下应当使用访问者模式 六、 使用访问者模式的优点和缺点 设计模式(21)-Template Method Pattern 一、 模板方法(Template Method)模式 二、 模版方法模式的结构 三、 模板方法模式的示意性代码 四、 继承作为复用的工具 五、 一个实际应用模板方法的例子 六、 模版方法模式中的方法 七、 重构的原则 设计模式(22)-Strategy Pattern 一、 策略(Strategy)模式 二、 策略模式的结构 三、 示意性源代码 四、 何时使用何种具体策略角色 五、 一个实际应用策略模式的例子 六、 在什么情况下应当使用策略模式 七、 策略模式的优点和缺点 八、 其它

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧