Design Patterns & Refactoring
Comparison of the Structural Patterns
Oliver Haase
HTWG Konstanz
Oliver Haase (HTWG Konstanz) Design Patterns & Refactoring 1 / 9
Magic Circle of Structural Patterns — Ok, Just Kidding...
Magic Circle of Structural Patterns — Ok, Just Kidding...
Oliver Haase (HTWG Konstanz) Design Patterns & Refactoring 3 / 9
Adapter vs. Bridge
Commonalities:
Level of Indirection for the access to the actual object
Delegation from an interface that the actual object doesn’t provide
Adapter vs. Bridge
Differences:
Purpose:
Adapterintends to match an implementation with a (different) interface
Bridgeintends to separate implementation from abstract to enable both to evolve separately
Time of application:
Adapteris employed rather later, i.e. when two existing types need to be brought together
Bridgeis employed early, to foresee separate evolution of abstraction and implementation
Oliver Haase (HTWG Konstanz) Design Patterns & Refactoring 5 / 9
Composite vs. Decorator
Commonalities
Structures of composite and decorator very similar (both use recursion to structure hierarchies of objects
Decorator structure might be mistaken for degenerated composite structure
→ composite builds tree, decorator builds chain of objects
Composite vs. Decorator
Differences are in their purpose:
Purpose:
Decoratorintends to add functionality to a type without changing it
→avoids expontential explosion of number of classes
Compositeintends to treat leaves and inner nodes (container objects) of an object hierarchy homogeneously
Oliver Haase (HTWG Konstanz) Design Patterns & Refactoring 7 / 9
Proxy vs. Decorator
Commonalities
Similar structures, in both cases indirect access to actual object via upstream object
in both cases, upstream object maintains reference to actual object (subject) and delegates requests to it
Proxy vs. Decorator
Differences are in their purpose:
Proxyis not about adding functionality, but about avoiding direct access to the subject, for varying reasons (protection, efficiency, transparent remote access)
With proxy pattern, proxy implements key functionality, with decorator pattern, functionality is split across levels of indirection.
Oliver Haase (HTWG Konstanz) Design Patterns & Refactoring 9 / 9