|Lesson 3|| How do structural patterns help programmers? |
|Objective||See what structural patterns can do for you. |
How do Structural Patterns help Programmers?
Structural patterns include some quite sophisticated and powerful techniques. However, they are based on just a few basic techniques, primarily:
- Delegation: The pattern is one in which a given object provides an interface to a set of operations. However, the actual work for those operations is performed by one or more other objects.
- Object composition: Other objects are stored as pointers or references inside the object that provides the interface to clients.
Object composition is a powerful yet often overlooked tool in the OOP programmer's toolbox. Structural patterns show you how to take advantage of it.
Structural patterns have many beneficial effects including:
- Increased efficiency (Decorator, Flyweight, Proxy)
- Enhanced reusability (Adapter, Bridge, Decorator)
- Separating implementation from interface (Adapter, Bridge, Façade, Proxy)
- Reducing complexity by providing cleaner, simpler interfaces to a system that are easier for client programmers to understand (Adapter, Bridge, Composite, Facade, Proxy)
Delegation: The IS-A and HAS-A Difference
In the context of design patterns, you will see classes that use other classes in their
construction. When one class passes off a task to another class, that is delegation. It’s
what makes composition so powerful.
With inheritance, each child IS-A part of another class or classes. With composition, objects may USE-A different class or group of classes for a series of tasks. This does not mean that inheritance should not be used. In fact, most design patterns include inheritance and composition used together. Instead of using inheritance in a long series of child, grandchild, great-grandchild,
a design pattern approach encourages using shallow inheritance and using the functionality of more than a single class.
This approach helps to avoid tight binding and crashes occurring when changes are
made to designs where concrete classes have child classes.
Of the many purposes of the seven structural patterns, here are 10 reasons to use structural pattens:
In the next lesson, you will become familiar with the structural patterns introduced here.
- Add new functionality dynamically to existing objects, or remove it (Decorator).
- Control access to an object (Proxy).
- Create expensive objects on demand (Proxy).
- Enable development of the interface and implementation of a component to proceed independently (Bridge).
- Match otherwise incompatible interfaces (Adapter).
- Reduce the cost of working with large numbers of very small objects (Flyweight).
- Reorganize a system with many subsystems into identifiable layers with single entry points (Façade).
- Select or switch implementations at runtime (Bridge).
- Simplify the interface to a complex subsystem (Façade).
- Treat single objects and composite objects in the same way (Composite).