Java Reference

In-Depth Information

Idiom

Delegation

Context

An operation is to be performed involving many

interconnected objects. Even if a few interactions with

each single object are required, if several objects are

involved the complexity rises quickly.

We require an algorithm that can be performed ideally

based only on local information. In practice it will be

based on as little global information as possible.

Problem

A centralized algorithm performed on an interconnected

graph of objects involves a large number of interactions.

Forces or tradeoffs

The algorithm is centralized and can be located externally

to the object graph or in a node of it.

The algorithm involves visiting all or most of the objects

of the graph. As a result, a lot of, possibly complex,

interactions take place with the objects that form the

graph.

The classes of the nodes of the graph feature low

encapsulation, since the algorithm needs to access the

information in all the nodes.

There is high coupling between the class that hosts the

algorithm and all the other classes: any change may cause

a ripple effect of related changes.

Solution

A divide and conquer technique can be applied. Each

object part of the graph is responsible for a small part of

the overall task.

The algorithm needs to be transformed from a centralized

to a decentralized form. Each object will be responsible

for operating on itself and will reduce the interaction to

direct neighbours.

Force resolution

The algorithm is decentralized and the duties are

distributed among the nodes of the object graph.

Each object accesses its own information. This increases

encapsulation and lowers coupling. The complexity of

interaction is reduced drastically.

Design rationale

Since we have an algorithm that operates on local

information, we leverage this characteristic and break up

the algorithm. Each node of the object graph will execute

a part of the task.

Pattern

Composite

Context

A tree structure contains different objects. Some objects

are leaves, others intermediate. Both leaves and

intermediate nodes can have different types.

Problem

Application needs to manipulate a hierarchical collection

of
primitive
and
composite
objects.