Pattern name: Context

Intent

Loosely couple behavior modification to behavior and structure 

Write an editing program instead of editing the program yourself.
Reuse editing program later.

Define collaborative behavior modification in one place
instead of spreading it over the program.
Context let's you write behavior modifications
with minimal dependency on the class structure.

Also Known As:

Structure-shy behavior modification, Event-based coupling


Motivation

Modify the behavior of a function call without changing the object
or the function directly.


Applicability

An object's function needs to be specialized or replaced.

A function call needs to accomplish additional work.

Express behavior which needs to be executed in addition
to a traversal.

Use the pattern when you need to change the behavior of a group
of classes without changing the classes.

Solution

Use context objects as described in [CONTEXT].


Consequences

Easy behavior adjustments

Allows to use the behavior modification in other situations.

Does not strongly tie behavior modification to the modified program.



Implementation

Use wrappers as in propagation patterns.

Use context tables as proposed in [CONTEXT]. 


Known Uses

Visitor objects in the Visitor pattern of GOF use a primitive
form of the Context pattern.

The Context pattern (with the wrapper implementation)
is used extensively in the implementation
of the Demeter Tools/C++. 

The work of Lang and Lockemann at the University of Karlsruhe
on "Behaviorally Adaptive Objects" uses the Context pattern.
They dissect an object into a crystal and temporarily associated
contexts. An object is defined as a crystal together with
all its current contexts, leading to dynamically changing object
behavior. 


Sample Code

See [CONTEXT].

Related patterns:
Inventor's Paradox, Structure-shy Traversal.

Exercise:

Define a notation to express context objects.


References:

CONTEXT:
Linda Seiter, Jens Palsberg, Karl Lieberherr,
Context Objects
(see URL: ftp://ftp.ccs.neu.edu/pub/people/lieber/context.ps)

==========================================================================