0% found this document useful (0 votes)
46 views12 pages

Design Patterns Design Patterns

The Composite Pattern allows objects to be composed into tree structures to represent part-whole hierarchies. It treats individual objects and compositions of objects uniformly by providing a uniform interface for accessing and managing them. For example, in a graphical application, components that contain other objects are Composite objects, while components that don't are Leaf objects, but the pattern allows clients to treat them uniformly. The pattern provides for recursive composition so that individual objects and compositions are treated the same way.

Uploaded by

Arslan Sohail
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views12 pages

Design Patterns Design Patterns

The Composite Pattern allows objects to be composed into tree structures to represent part-whole hierarchies. It treats individual objects and compositions of objects uniformly by providing a uniform interface for accessing and managing them. For example, in a graphical application, components that contain other objects are Composite objects, while components that don't are Leaf objects, but the pattern allows clients to treat them uniformly. The pattern provides for recursive composition so that individual objects and compositions are treated the same way.

Uploaded by

Arslan Sohail
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Design Patterns

Composite Pattern
Structural Pattern

The Composite Pattern p


Frequently programmers develop systems in which a component may be
an individual object OR it may represent a collection of objects

Now, we can create separate interfaces for individual objects and collection of objects. However we want to
be able to treat the collection of objects and individual objects in the th same way yet still have the ability to distinguish between them

C Composite pattern is designed to accommodate both cases it tt i d i dt d t b th


a composite is a collection of objects, any one of which may be either a composite, or just a primitive object in tree nomenclature some objects may be nodes with additional nomenclature, branches and some may be leaves
Design Patterns 3

Composite Design Pattern p g


Intent
Compose objects into tree structures to represent part-whole hierarchies C Composite lets clients treat individual objects and compositions of objects it l t li t t t i di id l bj t d iti f bj t uniformly

Consider a graphical application, g p pp ,


We have top level components (Frame, Panel etc) containing other components (menus, text panes, scrollbars, buttons etc). Two type of components are involved here:
Components that contain other objects are called Composite objects Components that dont contain other components are Leaf objects (GUI consists of several parts, but when we display it, we generally think of it as a whole)

If we have separate/different interfaces for Composite and Leaf objects


Cons:
Treating primitive (leaf) and container (composite) objects differently, even if most of the time the user treats them identically Having to distinguish these objects makes the application more complex

Composite pattern describes how to use recursive composition so that clients don't have to make this distinction
Design Patterns 4

Composite Pattern p
Structure

Applicability: Use the Composite pattern when


you want to represent part-whole hierarchies of objects you want clients to be able to ignore the difference between compositions of objects and individual objects
clients will treat all objects in the composite structure uniformly
Design Patterns 5

Composite Pattern: Example p p


Lets consider a small company. It may have started with a single person who got the business going.
H was, of course, the CEO, although he may have been too busy to think about it He f th CEO lth hh h b t b t thi k b t at first. Then he hired a couple of people to handle the marketing and manufacturing. Soon each of them hired some additional assistants to help with advertising, shipping and so f th and the became the companys first two vice-presidents. hi i d forth, d they be e fi t t i e e ide t As the companys success continued, the firm continued to grow until it has the organizational chart we see below:

Design Patterns

Composite: Example p p
Now, if the company is successful, each of these company members receives a salary, and we could at any time ask y, y for the cost of any employee to the company.
We define the cost as the salary of that person and those of all his subordinates:
The cost of an individual employee is simply his salary (and benefits). The cost of an employee who heads a department is his salary plus those of all his subordinates.

We would like a single interface that will produce the y y p y salary totals correctly whether the employee has subordinates or not.
public float getSalaries();

Design Patterns

The Employee Class p y

Calculates the salaries recursively

Design Patterns

Some checks are in order!


It could be that certain employees or job positions are designed so that they never should have subordinates g y
Assembly workers or salesmen may advance in the company by being named to a new position, but those holding these leaf positions will never have subordinates design the Employee class so that you can specify that this is a permanent leaf position
set a variable which is checked before subordinates can be added if the position is leaf position, the method returns f l or th th iti i l f iti th th d t false throws an exception.

Design Patterns

Implementation issues p
References to parent class
The parent maintains a list of its children If a child component also k hild t l knows about it parent, it can simplify the traversal and b t its t i lif th t l d management of composite structures

Child Access Operations


All methods in the Components interface are not useful for Leaf nodes E.g., getChild() Does it make sense to keep this in the Component interface ?

Child Management Operations


Should the child management operations be part of the Component interface or only Composite subclass
E.g., add(), remove()

Trade-off between safety and transparency f y p y


If we keep these methods in the Component interface, we have transparency
However, someone might try to add or remove components to a leaf

If we keep these methods in the Composite subclass, we have safety


However, we lose transparency as now Composite and Leaf have different interfaces

Go for Transparency !
Design Patterns 10

Implementation Issues p
Child Ordering
Depends on the type of the composite object
In a Graphics application
ordering may reflect front-to-back ordering

In case of a parse tree


ordering should reflect the order of parsing g p g

Design child access and management interfaces carefully to manage the sequence of children
The Iterator pattern can be helpful in this regard

Caching Results
If you frequently ask for data which must be computed from a series of child components, it may be advantageous to cache these i f hild t b d t t h th computed results in the parent However, unless the computation is relatively intensive and you are quite certain that the underlying data have not changed, this may not be worth the effort
Design Patterns 11

Composite Pattern: Consequences p q


Pros
allows you to define a class hierarchy of simple objects and more y y p j complex composite objects so that they appear to be the same to the client program
primitive objects can be composed into more complex objects, which in t i turn can be composed, and so on recursively b d d i l wherever client code expects a primitive object, it can also take a composite object.

makes the client simple makes it easier to add new kinds of components.
as long as they support the same programming interface

Cons
can make your design overly general
disadvantage of making it easy to add new components is that it p p makes it harder to restrict the components of a composite sometimes you want a composite to have only certain components
Design Patterns 12

You might also like