Composite Pattern – Object Structural

Intent

Compose object intot tree structures to represent part-whole hierachies. Composite lets client treat individual objects and compositions of objects uniformly.

 

Motivation

Graphics application like drawing editors and schematic capture systems let user build complex  diagrams out of simple components. The user can group components to form larger components, which in turn can be grouped to form still larger components.

But there’s  a problem with this approach: Code the uses these classes must treat primitive and container objects differently, even if most of time the user treats them identical. Having to distinguish theses objects makes the application more complex. The composite pattern describes how to use recursive composition so  that clients don’t have to make this distincation.

The key  to the Composite pattern is an abstract class that represents both primitives and their containers.

Applicability

Use the composite pattern when

  1. We want to represent part-whole hierachies og objects.
  2. We want client to ignore compostion objecst and individual objects. Client will treat all object in compostion uniformaly.

Structure

WhatsApp Image 2017-03-26 at 6.50.39 PM

 

A typical Composite object structure might look like this:

Composite pattern structure

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s