Structural Patterns  «Prev  Next»
Lesson 3 How do structural patterns help programmers?
ObjectiveSee what structural patterns can do for you.

How do Structural Patterns help Programmers?

Structural patterns, as classified in the Gang of Four (GoF) design patterns, are powerful tools for programmers, primarily addressing the composition and arrangement of classes and objects within software systems. They confer several key benefits and rely on a range of fundamental object-oriented techniques. Firstly, structural patterns facilitate better system design through the compartmentalization of responsibilities, fostering clean, efficient, and effective software architecture. They simplify complex relationships between different parts of the software, leading to less error-prone code. This inherently leads to enhanced software maintainability because well-structured code is easier to understand, debug, and update.
Secondly, structural patterns enhance code reusability. By defining ways to compose objects, they enable the same object structure to be used across different parts of a system or even in different systems. This leads to less redundant code, faster development times, and reduced likelihood of errors. Thirdly, these patterns offer ways to manage the evolution of software systems. As system requirements change over time, structural patterns provide a blueprint for extension and modification, promoting system scalability and flexibility without necessitating wholesale redesigns. Structural patterns draw upon several key object-oriented techniques to provide these benefits:
  1. Inheritance: Structural patterns extensively use inheritance to establish relationships and promote code reusability. For example, the Decorator pattern uses inheritance to extend the behavior of a base class.
  2. Encapsulation: Encapsulation is vital to hide implementation details and maintain the internal consistency of objects. The Facade pattern encapsulates a complex subsystem behind a simplified interface.
  3. Polymorphism: Structural patterns often leverage polymorphism to enable objects to take on many forms depending on the context. The Adapter pattern, for instance, relies on polymorphism to adapt one interface to another.
  4. Composition: Many structural patterns use composition to combine simple objects into complex ones, enhancing flexibility. The Composite pattern, for instance, treats single instances and compositions in the same way.
  5. Aggregation: This technique is used to represent relationships between different components. For example, the Proxy pattern uses aggregation to hold a reference to the object it controls.

Structural patterns are powerful tools for programmers, providing ways to simplify complex systems, promote reusability, and enable software evolution. They are built upon key object-oriented principles, thus aiding programmers in leveraging the full power of object-oriented programming.

Associated Techniques

Structural patterns include some quite sophisticated and powerful techniques. However, they are based on just a few basic techniques, primarily:
  1. 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.
  2. 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.

Benefits

Structural patterns have many beneficial effects including:
  1. Increased efficiency (Decorator, Flyweight, Proxy)
  2. Enhanced reusability (Adapter, Bridge, Decorator)
  3. Separating implementation from interface (Adapter, Bridge, Façade, Proxy)
  4. Reducing complexity by providing cleaner, simpler interfaces to a system that are easier for client programmers to understand (Adapter, Bridge, Composite, Facade, Proxy)

Delegation and the Distinction Between IS-A and HAS-A in Structural Design Patterns

In the architectural realm of software design, structural patterns emphasize the composition of classes or objects to form larger structures. A key principle underlying these patterns is the concept of delegation. To truly grasp the essence of delegation, it's pivotal to differentiate between the "IS-A" and "HAS-A" relationships, two foundational object-oriented paradigms.
  1. IS-A Relationship: The "IS-A" relationship is grounded in inheritance. When we say "Class B IS-A Class A", it implies that Class B inherits from Class A. This relationship establishes a parent-child connection between the two classes, allowing Class B to inherit attributes and behaviors (methods) from Class A. It's a vertical relationship, often visualized as a hierarchical tree structure.
  2. HAS-A Relationship: In contrast, the "HAS-A" relationship is based on composition. When we state "Class C HAS-A Class D", it means that Class C comprises or contains an instance of Class D. Here, rather than inheriting attributes or behaviors, Class C leverages the functionalities of Class D by holding a reference to its instance. It's a horizontal relationship, encapsulating the idea that one class contains another.

Delegation within this context

Delegation is a technique where an object expresses certain behavior not by implementing that behavior directly but by forwarding requests to a separate object, known as its delegate. In essence, an object hands over its responsibilities to another object. This is where the "HAS-A" relationship shines. Instead of inheriting functionalities (as in "IS-A"), the primary object contains a reference to another object and delegates specific tasks to it.
For instance, consider a "Printer" class. Instead of implementing the printing logic directly, it holds a reference to a "PrintStrategy" object and delegates the actual printing task to this strategy object. Here, "Printer" HAS-A "PrintStrategy", and instead of inheriting the behavior, it delegates the behavior.

Advantages of Delegation over Inheritance

  1. Flexibility: Delegation fosters flexibility. Since behaviors are delegated to contained objects, changing behavior dynamically at runtime becomes feasible. Simply swap the delegate with another having a different implementation.
  2. Avoids Class Explosion: In scenarios where multiple combinations of behaviors are needed, inheritance can lead to a combinatorial explosion of classes. Delegation, by allowing behavior to be combined and recombined at runtime, mitigates this.
  3. Enhances Composition: Delegation promotes composition over inheritance, aligning with the adage "favor 'object composition' over 'class inheritance'."

In conclusion, delegation stands as a testament to the power of the "HAS-A" relationship in structural design patterns. By emphasizing composition and the delegation of responsibilities, it offers a more dynamic, maintainable, and modular approach than the rigid hierarchies often found in deep inheritance structures. It's a paradigm shift, accentuating the interplay of objects over the inheritance of classes, underscoring the dynamism inherent in modern object-oriented design.
In the next lesson, you will become familiar with the structural patterns introduced here.