复合模式和装饰模式的区别? [英] Difference between the Composite Pattern and Decorator Pattern?

查看:28
本文介绍了复合模式和装饰模式的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the difference between the Composite Pattern and 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

这篇关于复合模式和装饰模式的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆