Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Composite Pattern

Composite Pattern

The Composite Pattern, also called the Part-Whole pattern, is used to treat a group of similar objects as a single object. The Composite Pattern composes objects based on a tree structure to represent part as well as whole hierarchies. This type of design pattern belongs to the structural patterns, and it creates a tree structure of groups of objects.

This pattern creates a class that contains its own group of objects. This class provides ways to modify the same group of objects.

We demonstrate the use of the Composite Pattern with the example below. The example demonstrates the hierarchy of employees in an organization.

Introduction

Intent

Compose objects into tree structures to represent "part-whole" hierarchies. The Composite Pattern makes the use of individual objects and compositions of objects consistent for the client.

Main problem solved

Use Cases

Implementation

Key code

Application Example

  1. Arithmetic expressions: build a tree structure consisting of operands, operators, and sub-expressions.
  2. GUI components: in Java's AWT and Swing libraries, a container (such as Panel) can contain other components (such as buttons and checkboxes).

Advantages

  1. Simplifies client code: the client can handle all types of nodes uniformly.
  2. Easy to extend: new leaf types or branch types can be added easily.

Disadvantages

Usage recommendation

Notes

structures

The core roles of the Composite Pattern include:

- Defines the common interface for all objects in the composition; it can be an abstract class or an interface. It declares methods for accessing and managing child components, including add, remove, get child, and so on. - Represents the leaf node objects in the composition; a leaf node has no child nodes. It implements the methods of the Component interface but usually does not contain child components. - Represents the composite objects in the composition; a composite node can contain child nodes, which can be leaf nodes or other composite nodes. It implements the methods of the Component interface, including methods for managing child components. - By interacting with the composite structure through the Component interface, the client does not need to distinguish between leaf nodes and composite nodes, and can treat the whole and the part consistently.

Create a class that implements

We have a class Employeewhich is treated as the composite model class.CompositePatternDemo Class uses Employee class to add the department hierarchy and print all employees.

UML diagram of the composite pattern

Step 1

creation Employee class, which has a Employee list of objects.

Employee.java

import

java

.

util

.

ArrayList

;

import

java

.

util

.

List

;

public

class

Employee

{

private

String

name

;

private

String

dept

;

private

int

salary

;

private

List

<

Employee

subordinates

;

//

constructor

public

Employee

(

String

name

,

String

dept

,

int

sal

)

{

this

.

name

=

name

;

this

.

dept

=

dept

;

this

.

salary

=

sal

;

subordinates

=

new

ArrayList

<

Employee

(

)

;

}

public

void

add

(

Employee

e

)

{

subordinates

.

add

(

e

)

;

}

public

void

remove

(

Employee

e

)

{

subordinates

.

remove

(

e

)

;

}

public

List

<

Employee

getSubordinates

(

)

{

return

subordinates

;

}

public

String

toString

(

)

{

return

(

"

Employee :[ Name :

"

name

"

, dept :

"

dept

"

, salary :

"

salary

"

]

"

)

;

}

}

Step 2

Use Employee class to create and print the employee hierarchy.

CompositePatternDemo.java

public

class

CompositePatternDemo

{

public

static

void

main

(

String

[

]

args

)

{

Employee

CEO

=

new

Employee

(

"

John

"

,

"

CEO

"

,

30000

)

;

Employee

headSales

=

new

Employee

(

"

Robert

"

,

"

Head Sales

"

,

20000

)

;

Employee

headMarketing

=

new

Employee

(

"

Michel

"

,

"

Head Marketing

"

,

20000

)

;

Employee

clerk1

=

new

Employee

(

"

Laura

"

,

"

Marketing

"

,

10000

)

;

Employee

clerk2

=

new

Employee

(

"

Bob

"

,

"

Marketing

"

,

10000

)

;

Employee

salesExecutive1

=

new

Employee

(

"

Richard

"

,

"

Sales

"

,

10000

)

;

Employee

salesExecutive2

=

new

Employee

(

"

Rob

"

,

"

Sales

"

,

10000

)

;

CEO

.

add

(

headSales

)

;

CEO

.

add

(

headMarketing

)

;

headSales

.

add

(

salesExecutive1

)

;

headSales

.

add

(

salesExecutive2

)

;

headMarketing

.

add

(

clerk1

)

;

headMarketing

.

add

(

clerk2

)

;

//

Print all employees of the organization

System

.

out

.

println

(

CEO

)

;

for

(

Employee

headEmployee

:

CEO

.

getSubordinates

(

)

)

{

System

.

out

.

println

(

headEmployee

)

;

for

(

Employee

employee

:

headEmployee

.

getSubordinates

(

)

)

{

System

.

out

.

println

(

employee

)

;

}

}

}

}

Step 3

Execute the program; the output result is:



Employee :[ Name : John, dept : CEO, salary :30000 ]

Employee :[ Name : Robert, dept : Head Sales, salary :20000 ]

Employee :[ Name : Richard, dept : Sales, salary :10000 ]

Employee :[ Name : Rob, dept : Sales, salary :10000 ]

Employee :[ Name : Michel, dept : Head Marketing, salary :20000 ]

Employee :[ Name : Laura, dept : Marketing, salary :10000 ]

Employee :[ Name : Bob, dept : Marketing, salary :10000 ]