Lesson 2 | What is a Structural Pattern? |
Objective | Define Structural Patterns. |
What is a Structural Pattern?
Structural patterns, within the context of the Gang of Four (GoF) design patterns, are a category of design patterns that deal primarily with the composition of classes and objects. They dictate how the entities should be composed to form larger structures in an efficient, reliable, and scalable manner.
Structural design patterns focus on ensuring that different parts of a software system work together properly, taking care to balance responsibilities, dependencies, and relationships between the entities. They assist in establishing beneficial relationships between software components, thereby enabling the creation of complex structures that are maintainable, extensible, and scalable.
There are seven structural patterns as defined by the GoF:
- Adapter: This pattern allows two incompatible interfaces to work together. It wraps itself around an object to expose a standard interface that other objects can interact with.
- Bridge: This pattern decouples an abstraction from its implementation so that the two can vary independently, thereby reducing the complexity.
- Composite: This pattern is used to build complex hierarchical structures of objects and treats individual objects and compositions of objects uniformly.
- Decorator: This pattern dynamically adds/overrides behaviour in an existing method of an object.
- Facade: This pattern provides a simplified interface to a larger body of code. It wraps a complicated subsystem with a simpler interface to ease usage and reduce dependencies on the subsystem.
- Flyweight: This pattern reduces memory usage by sharing as much data as possible with similar objects.
- Proxy: This pattern provides a placeholder for another object to control access to it. This can be used for lazy instantiation, security controls, or introducing additional behavior.
Each of these structural patterns offers unique solutions to design issues that could impede the structure and inter-communication of objects within a system. It's important to remember that design patterns are not silver bullets and should not be forced into places where they do not fit; the decision to use a design pattern should be motivated by a real need to enhance flexibility, maintainability, or scalability in the system design.
A structural design pattern serves as a blueprint for how different classes and objects are combined to form larger structures.
Unlike creational patterns, which are mostly different ways to fulfill the same fundamental purpose, each structural pattern has a different purpose.
Question: What unifies structural patterns?
Answer: They all involve connections between objects.
In some sense, structural patterns are similar to the simpler concept of data structures. However, structural design patterns also specify the methods that connect objects, not merely the references between them. Furthermore, data structures are fairly static entities. They only describe how data is arranged in the structure. A structural design pattern also describes how data moves through the pattern. Structural class patterns use inheritance to combine the interfaces or implementations of multiple classes. Structural class patterns are relatively rare. Structural object patterns use object composition to combine the implementations of multiple objects. They can combine the interfaces of all the composed objects into one unified interface or they can provide a completely new interface.
Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize relationships between entities. This pattern category discusses the composition between the modules or components which allow one to develop or build larger systems. Structural patterns define how each component or entity should be structured so as to have very flexible interconnecting modules which can work together in a larger system.
Structural patterns describe how classes and objects can be combined to form larger structures.
The difference between class patterns and object patterns is that class patterns describe how inheritance can be used to provide more useful program interfaces.
Object patterns describe how objects can be composed into larger structures using
object composition, or the inclusion of objects within other objects.
For example, we will see that the
Adapter pattern can be used to make one class interface match another to make programming easier.
We will also look at a number of other structural patterns where we combine objects to provide new functionality.
The
Composite is exactly that:
A composition of objects, each of which may be either simple or itself a composite object.
The
Proxy pattern is frequently a simple object that takes
the place of a more complex object that may be invoked later, for example when the program runs in a network environment.
The
Flyweight pattern
is a pattern for sharing objects, where each instance does not contain its own state, but stores it externally. This allows efficient sharing of objects to save space, when there are many instances, but only a few different types.
The
Facade pattern is used to make a single class represent an entire
subsystem, and the Bridge pattern separates an object's interface from its implementation, so you can vary them separately.
Finally, the
Decorator Pattern can be used to add responsibilities to objects dynamically.