Gamma Structural Patterns

Helmi ben abdallah
6 min readFeb 1, 2019

--

Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications in a variety of ways, for example, the Adapter pattern enables two incompatible systems to communicate, while the Façade pattern enables you to present a simplified interface to a user without removing all the options available in the system.
Structural patterns allow you to create systems without rewriting or customizing the code. This provides the system with enhanced reusability and robust functionality.

Adapter Pattern :

The Adapter pattern acts as an intermediary between two classes, converting the interface of one class so that it can be used with the other. This enables classes with incompatible interfaces to work together.
The Adapter pattern implements an interface known to its clients and provides access to an instance of a class not known to its clients.
An adapter object provides the functionality of an interface without having to know the class used to implement that interface.

A class adapter uses multiple inheritances to adapt one interface to another
An object adapter relies on object composition

BENEFITS: Allows two or more incompatible objects to communicate and interact. It Improves the reusability of older functionality.

WHEN TO USE: You want to use an existing class, and its interface does not match the interface you need. You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don’t necessarily have compatible interfaces. You want to use an object in an environment that expects an interface that is different from the object’s interface. Interface translation among multiple sources must occur

Bridge Pattern:

The Bridge pattern divides a complex component into two separate but related inheritance hierarchies: the functional abstraction and the internal implementation.

Rather than combining the abstractions and implementations into many distinct classes, the Bridge pattern implements the abstractions and implementations as independent classes that can be combined dynamically. This makes it easier to change either aspect of the component so that the two can vary independently.

The Bridge pattern is useful when there is a hierarchy of abstractions and a corresponding hierarchy of implementations.

BENEFITS: Enables you to separate the interface(abstraction functionality ) from the implementation, improves extensibility ,hides implementation details from clients.

WHEN TO USE: You want to avoid a permanent binding between an abstraction and its implementation.
Both the abstractions and their implementations should be extensible using subclasses.
Changes in the implementation of abstraction should have no impact on clients, that is, you should not have to recompile their code.

Composite Pattern :

The Composite pattern enables you to create hierarchical tree structures of varying complexity while allowing every element in the structure to operate with a uniform interface.
The Composite pattern combines objects into tree structures to represent either the whole hierarchy or a part of the hierarchy. This means the Composite pattern allows clients to treat individual objects and compositions of objects uniformly.

BENEFITS: Defines class hierarchies consisting of primitive objects and composite objects .Makes it easier to add new kinds of components Provides flexibility of structure and a manageable interface.

WHEN TO USE: You want to represent the whole hierarchy or a part of the hierarchy of objects. You want clients to be able to ignore the difference between compositions of objects and individual objects. The structure can have any level of complexity, and is dynamic.

Decorator Pattern :

The Decorator pattern enables you to add or remove object functionality without changing the external appearance or function of the object.
It changes the functionality of an object in a way that is transparent to its clients by using an instance of a subclass of the original class that delegates operations to the original object.
The Decorator pattern attaches additional responsibilities to an object dynamically to provide a flexible alternative to changing object functionality without using fixed inheritance.

BENEFITS: More flexibility than fixed inheritance stay away from feature loaded classes high up in the hierarchy Simplifies coding because you write a series of classes, each targeted at a specific part of the functionality, rather than coding all behavior into the object Enhances the object’s extensibility because you make changes by coding new classes.

WHEN TO USE: You want to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
You want to add responsibilities to the object that you might want to change in the future. When extension by fixed subclassing is impossible.

Façade Pattern :

The Façade pattern provides a unified interface to a group of interfaces in a subsystem. The Façade pattern defines a higher-level interface that makes the subsystem easier to use because you have only one interface.
This unified interface enables an object to access the subsystem using the interface to communicate with the subsystem.

WHEN TO USE: You want to provide a simple interface to a complex subsystem. There are many dependencies between clients and the implementation classes of an abstraction. You want to layer your subsystems.

Flyweight Pattern :

The Flyweight pattern reduces the number of low-level, detailed objects within a system by sharing objects. If instances of a class that contain the same information can be used interchangeably, the Flyweight pattern allows a program to avoid the expense of multiple instances that contain the same information by sharing one instance.

BENEFITS : Reduction in the number of objects to handle .Reduction in memory and on storage devices, if the objects are persisted.

WHEN TO USE: The application uses a large number of objects. Storage costs are high because of the number of objects. The application doesn’t depend on object identity.

Proxy Pattern :

The Proxy pattern provides a surrogate(deleguation) or laceholder(container) object to control access to the original object.There are several types of implementations of the Proxy pattern with the Remote proxy and Virtual proxy being the most common.

BENEFITS: A remote proxy can hide the fact that an object resides in a different address space. A virtual proxy can perform optimizations, such as creating an object on demand. A protection proxy controls access to the original object.

WHEN TO USE :You need a more versatile(polyvalen) or sophisticated reference to an object than a simple pointer.

--

--