复合模式和装饰模式的区别?

发布于 2024-08-21 01:16:53 字数 22 浏览 13 评论 0原文

复合模式和装饰模式有什么区别?

What is the difference between the Composite Pattern and Decorator Pattern?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

虐人心 2024-08-28 01:16:53

他们通常齐头并进。因为使用复合模式通常会导致也使用装饰器模式。

复合模式允许您以允许外部代码将整个结构视为单个实体的方式构建层次结构(例如元素树)。因此,叶实体的接口与复合实体的实体完全相同。因此,本质是复合结构中的所有元素都具有相同的接口,即使有些是叶节点而另一些是整个结构。用户界面通常使用这种方法来实现轻松的可组合性。

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

梦旅人picnic 2024-08-28 01:16:53

复合模式和装饰器的结构看起来相同,但它们有不同的意图。

Composite 为叶子和组合提供了统一的接口。

装饰器装饰器为叶子提供了额外的功能,同时提供了统一的界面。


示例

复合模式:经典的Windows文件夹和文件。 Windows 文件夹是复合的。文件是叶子。双击其中任何一个即可打开文件/文件夹 - 双击是统一界面。

装饰器模式:缓冲 io - java.io.FileWriterjava.io.BufferedWriter 都扩展了 java.io.Writer代码>. java.io.BufferedWriter 是复合的,FileWriter 是叶子。 BufferedWriterFileWriter 添加了额外的缓冲职责(或功能)。
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 and java.io.BufferedWriter both extend java.io.Writer. java.io.BufferedWriter is composite and FileWriter is leaf. BufferedWriter adds additional responsibility (or feature) of buffering to FileWriter.
write() method is unified interface, whereas buffering is additional feature.

随波逐流 2024-08-28 01:16:53

装饰器可以被视为只有一个组件的简并复合体。然而,装饰器增加了额外的职责——它不适用于对象聚合。

这是四人帮在《设计模式-可重用面向对象软件的要素》中说的。

A decorator can be viewed as a degenerate composite with only one component. However, a decorator adds additional responsibilities—it isn't intended for object aggregation.

This is what is said in "Design Patterns-Elements of Reusable Object Oriented Software" by the gang of four.

萌能量女王 2024-08-28 01:16:53

差异可能更多地在于目的而不是实施。在某些情况下,复合模式比子类化更可取。例如,您可以通过向类添加其他类的实例,然后通过转发接口公开该功能来添加您希望某个类具有的功能。

装饰器允许您透明地向类添加功能(通常是单个功能),而类实例的客户端不需要知道那里有装饰器 - 例如,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.

念﹏祤嫣 2024-08-28 01:16:53

装饰器模式可用于静态扩展(装饰)某个对象的功能,或者在某些情况下,在运行时,独立于同一类的其他实例。

这是可能的,因为组合:装饰器包含组件,同时它实现组件接口。

复合模式描述了一组对象的处理方式与单个对象相同对象的实例。组合的目的是将对象“组合”成树结构以表示部分-整体层次结构。

实现复合模式可以让客户统一地处理单个对象和组合。

尽管结构看起来相同,但意图和用例不同。

Decorator 模式的用例:

  1. 应动态添加/删除对象职责和行为 具体
  2. 实现应该与职责和行为
  3. 分离 子类化的成本太高,无法动态添加/删除职责

关键 差异 在这两种模式之间:

  1. 装饰器旨在让您无需子类化即可向对象添加职责。 Composite 的重点不是修饰而是表示
  2. 装饰器添加/删除额外的职责 - 它不是用于对象聚合。

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:

  1. Object responsibilities and behaviours should be dynamically added/removed
  2. Concrete implementations should be decoupled from responsibilities and behaviours
  3. subclassing is too costly to dynamically add/remove responsibilities

Key differences between these two patterns:

  1. Decorator is designed to let you add responsibilities to objects without subclassing. Composite's focus is not on embellishment but on representation
  2. Decorator adds/remove additional responsibilities - it isn't intended for object aggregation.

Useful posts in SE for better understanding :

Decorator Pattern for IO

When to Use the Decorator Pattern?

素染倾城色 2024-08-28 01:16:53

结构上的差异

下面是 GoF 书中的类图,使用 PlantUML 复制。

GoF Decorator class diagram

GoF Composite class diagram

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.

GoF Decorator class diagram

GoF Composite class diagram

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.

停顿的约定 2024-08-28 01:16:53

复合:

  • 是使用递归的树结构。
  • Leaf 和 Composite
  • 对象之间具有相同的接口 Unity

Decorator:

  • 是包含另一个实体。
  • 向复合对象添加新功能而不对其进行修改。

Composite:

  • Is a Tree Structure using recursion.
  • Leaf and Composite have same interface
  • Unity between objects

Decorator:

  • Is contain another entity.
  • Adding new functionality to a composite object without modifying it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文