复合模式和装饰模式的区别?
复合模式和装饰模式有什么区别?
What is the difference between the Composite Pattern and Decorator Pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
复合模式和装饰模式有什么区别?
What is the difference between the Composite Pattern and Decorator Pattern?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
他们通常齐头并进。因为使用复合模式通常会导致也使用装饰器模式。
复合模式允许您以允许外部代码将整个结构视为单个实体的方式构建层次结构(例如元素树)。因此,叶实体的接口与复合实体的实体完全相同。因此,本质是复合结构中的所有元素都具有相同的接口,即使有些是叶节点而另一些是整个结构。用户界面通常使用这种方法来实现轻松的可组合性。
http://en.wikipedia.org/wiki/Composite_pattern
装饰器模式允许实体完全包含另一个实体,以便使用装饰器看起来与包含的实体相同。这允许装饰器修改它所封装的任何内容的行为和/或内容,而不改变实体的外观。例如,您可以使用装饰器添加有关所包含元素的使用情况的日志输出,而不更改所包含元素的任何行为。
http://en.wikipedia.org/wiki/Decorator_pattern
They usually go hand in and hand. In that using the composite pattern often leads to also using the decorator pattern.
The composite pattern allows you to build a hierarchical structure (such as a tree of elements) in a way that allows your external code to view the entire structure as a single entity. So the interface to a leaf entity is exactly the same as the entity for a compound entity. So the essence is that all elements in your composite structure have the same interface even though some are leaf nodes and others are entire structures. User interfaces often use this approach to allow easy composability.
http://en.wikipedia.org/wiki/Composite_pattern
The decorator pattern allows an entity to completely contain another entity so that using the decorator looks identical to the contained entity. This allows the decorator to modify the behaviour and/or content of whatever it is encapsulating without changing the outward appearance of the entity. For example, you might use a decorator to add logging output on the usage of the contained element without changing any behaviour of the contained element.
http://en.wikipedia.org/wiki/Decorator_pattern
复合模式和装饰器的结构看起来相同,但它们有不同的意图。
Composite 为叶子和组合提供了统一的接口。
装饰器装饰器为叶子提供了额外的功能,同时提供了统一的界面。
示例
复合模式:经典的Windows文件夹和文件。 Windows 文件夹是复合的。文件是叶子。双击其中任何一个即可打开文件/文件夹 - 双击是统一界面。
装饰器模式:缓冲 io -
java.io.FileWriter
和java.io.BufferedWriter
都扩展了java.io.Writer
代码>.java.io.BufferedWriter
是复合的,FileWriter
是叶子。BufferedWriter
向FileWriter
添加了额外的缓冲职责(或功能)。write()
方法是统一接口,而缓冲是附加功能。The structure of composite pattern and decorator look the same but they have different intent.
Composite gives an unified interface to a leaf and composite.
Decorator decorator gives additional feature to leaf, while giving unified interface.
Examples
Composite pattern: classic windows folders and files. Windows folders are composites. files are leaves. A double click on either of them opens the file/folder - double click is unified interface.
Decorator pattern: Buffered io -
java.io.FileWriter
andjava.io.BufferedWriter
both extendjava.io.Writer
.java.io.BufferedWriter
is composite andFileWriter
is leaf.BufferedWriter
adds additional responsibility (or feature) of buffering toFileWriter
.write()
method is unified interface, whereas buffering is additional feature.这是四人帮在《设计模式-可重用面向对象软件的要素》中说的。
This is what is said in "Design Patterns-Elements of Reusable Object Oriented Software" by the gang of four.
差异可能更多地在于目的而不是实施。在某些情况下,复合模式比子类化更可取。例如,您可以通过向类添加其他类的实例,然后通过转发接口公开该功能来添加您希望某个类具有的功能。
装饰器允许您透明地向类添加功能(通常是单个功能),而类实例的客户端不需要知道那里有装饰器 - 例如,Django 中视图上的“login_required”装饰器会引发异常如果用户未登录,但否则视图的行为就像没有装饰器一样。
在这两种情况下,您都将一个对象嵌入到另一个对象中,但您想要完成的任务可能是不同的。
The difference is probably more one of purpose than implementation. In some instances the composite pattern is preferable to subclassing. For example, you can add the functionality that you want a class to have by adding instances of other classes to it and then exposing the functionality through a forwarding interface.
Decorators allow you to transparently add functionality, usually a single capability, to an class without clients of the instances of the class needing to know the that there's a decorator there - for example, a "login_required" decorator on a view in Django raises an exception if the user isn't logged in, but otherwise the view behaves as it would without the decorator.
In both cases you have one object embedded within another, but what you're trying to accomplish is arguably different.
装饰器模式可用于静态扩展(装饰)某个对象的功能,或者在某些情况下,在运行时,独立于同一类的其他实例。
这是可能的,因为组合:装饰器包含组件,同时它实现组件接口。
复合模式描述了一组对象的处理方式与单个对象相同对象的实例。组合的目的是将对象“组合”成树结构以表示部分-整体层次结构。
实现复合模式可以让客户统一地处理单个对象和组合。
尽管结构看起来相同,但意图和用例不同。
Decorator 模式的用例:
关键 差异 在这两种模式之间:
SE 中有助于更好理解的有用帖子:
IO 装饰器模式
何时使用装饰器模式?
The Decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class.
It's possible due to composition : Decorator contains Component and at the same time it implements Component interface.
The Composite pattern describes that a group of objects is to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.
Implementing the composite pattern lets clients treat individual objects and compositions uniformly.
Even though the structure seems to be same, intent and use cases are different.
Use cases for Decorator pattern:
Key differences between these two patterns:
Useful posts in SE for better understanding :
Decorator Pattern for IO
When to Use the Decorator Pattern?
结构上的差异
下面是 GoF 书中的类图,使用 PlantUML 复制。
Differences in intent
Decorator 的目的是装饰一个单个组件(UML 图实际上应该显示被装饰组件的多重性),而 Composite 的目的是将组件作为一个整体分组到 Composite 中(同样,UML 应该显示一个包含一个或多个 组件的组合体。
Decorator 的目标是通过 ConcreteDecorators 添加行为(增强
Operation()
方法的行为),而 Composite 的目标是收集组件。Differences in structure
Here are the class diagrams from the GoF book, reproduced using PlantUML.
Differences in intent
The intent of Decorator is to decorate a single component (the UML diagram really should show a multiplicity of one for the decorated component), whereas the intent of Composite is to group Components as a whole in the Composite (again, the UML should show a Composite containing one or more Components).
Decorator has a goal to add behavior (enhance behavior of the
Operation()
method) via the ConcreteDecorators, whereas Composite aims to collect Components.复合:
Decorator:
Composite:
Decorator: