40543

Composite Pattern

Question:

I have question regarding composite pattern.

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

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

Here is the uml class diagram of the pattern.

<a href="https://i.stack.imgur.com/SdmLG.gif" rel="nofollow"><img alt="alt text" class="b-lazy" data-src="https://i.stack.imgur.com/SdmLG.gif" data-original="https://i.stack.imgur.com/SdmLG.gif" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

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 <strong>inheritance</strong>.

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

Answer2:

<strong>Component</strong>

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

<strong>Leaf</strong>

<ul><li>represents leaf objects in the composition</li> <li>implements all Component methods</li> </ul>

<strong>Composite</strong>

<ul><li>represents a composite Component (component having children)</li> <li>implements methods to manipulate children</li> <li>implements all Component methods, generally by delegating them to its children</li> </ul>

<a href="http://en.wikipedia.org/wiki/Composite_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Composite_pattern</a>

Answer3:

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

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

More <a href="http://sourcemaking.com/design_patterns/composite" rel="nofollow">here</a>

Recommend

  • C++/CLI: inherit from one CLR class, multiple C++ classes
  • FATAL: could not access private key file “/etc/ssl/private/ssl-cert-snakeoil.key”: Permission denied
  • How to use Spring Webflow popup=“true” with Primefaces?
  • Java SOAP client stub generation with service endpoint determined at runtime
  • protected virtual methods in f#
  • compose Try nicer
  • Java class containing only private members
  • Composite pattern and Dependency Injection
  • How to get Email address from outlook contacts for the names listed in a column?
  • How to plot predictions of binomial GLM that has both continuous and categorical variables
  • UML Sequence Diagram Message Branching
  • UML specification : Do I need inheritance?
  • C++ Binary Tree error: request for member (X) in (Y) which is of non-class type (Z)
  • Relational Database Mapping in MVC
  • Batching in py2neo
  • Easier way to do non-recursive list on gcs bucket outside appengine
  • How do we set maximum_bad_records when loading a Bigquery table from dataflow?
  • How to click on a link that has a certain content in puppeteer?
  • Checking if the Faye server exists before running it for my Rails app
  • How to override __call__ in celery on main?
  • What is the reason that Policy.getPolicy() is considered as it will retain a static reference to the
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • Scala: Function returning an unknown type
  • Why can't UI components be accessed from a backgroundworker?
  • how to change api_token column in token guard
  • LiveData is abstract android
  • Magento site down due to mysql error General error: 1030 Got error -1 from storage engine
  • Why use database factory in asp.net mvc?
  • Firefox Extension - Monitor refresh and change of tab
  • Insert new calendar with SyncAdapter- Calendar API Android
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Django simple Captcha “No module named fields” error
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Typescript - Unable to get 'import' statement to function
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Projection media query: browser support and workarounds?
  • C# - Serializing and deserializing static member
  • Bug in WPF DataGrid
  • Updating server-side rendering client-side
  • Incrementing object id automatically JS constructor (static method and variable)