简体   繁体   中英

Composite design pattern definition

In the book Design Patterns : Elements of Reusable Object-Oriented Software, It says:

"The composite design pattern composes objects into tree structures to represent part-whole hierarchies."

In wikipedia the composite design pattern is defined as :

"The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects in to tree structures to represent part-whole hierarchies."

Picture of composite design pattern:

在此处输入图片说明

If my Composite stores components as directed acyclic graph( for example it only stores components which are sources of DAC in queue data structure and those sources have references to another components and so on... ) which is not tree because it violates some tree structure condition. Can I still say that I have used composite design pattern?

You can store your components in any data structure you like. The point is that each of your components can also be treated as “whole”.

The easiest example is a CompositeView object which contains subviews. CompositeView is a View and its subview objects are also Views. So you have a common interface/abstract class for your objects. It doesn't matter at all what data structure is used to store subviews.

In the mentioned tree-like structure your list/set/dag/whatever... of components defines a set of child nodes for a given parent node.

Another example could be with Brick , Wall , House , Block . A Wall is composed by many Bricks; in same way an House is composed by four Walls (for the example leave me say that an House doesn't have a roof) and a Block is composed by many Houses. Brick in the Composite pattern represents a Leaf, instead Wall,House,Block are specialisations of Composite, but all can be considered as ConstructionComponents (or ConstructionEntities).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM