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

问题描述

复合图案和装饰图案之间有什么区别?

解决方案

他们通常会手牵手。因为使用复合图案经常导致也使用装饰图案。



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





装饰器模式允许一个实体完全包含另一个实体,以便使用装饰器看起来与所包含的实体相同。这允许装饰器修改任何封装的行为和/或内容,而不改变实体的外观。例如,您可以使用装饰器在包含的元素的使用上添加日志输出,而不会更改包含的元素的任何行为。




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

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

09-11 11:21