简体   繁体   中英

Composite Pattern

I have a question regarding composite pattern.

Does the base class "Component" act like a pointer to point leaf object in "Composite" class?

Edit: Let me ask my question in following words. "What is the relation between Composite and Component class?"

Here is the uml class diagram of the pattern.

替代文字

Relation between composite and component:

1) Leaf and Composite usually implement one interface or one abstract class. In your diagram they extend Component. So, the relation on your diagram is inheritance .

2) Composite contains instances of Component. Component, as it occasionally can be Composite, can also contain instances of Component. This is called recursive composition . In general, the relation is called aggregation .

Component

  • is the abstraction for all components, including composite ones
  • declares the interface for objects in the composition
  • (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate

Leaf

  • represents leaf objects in the composition
  • implements all Component methods

Composite

  • represents a composite Component (component having children)
  • implements methods to manipulate children
  • implements all Component methods, generally by delegating them to its children

http://en.wikipedia.org/wiki/Composite_pattern

All container and containee classes declare an “is a” relationship to the interface( Component ).

All container classes declare a one-to-many “has a” relationship to the interface.

More here

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