Structural Design Patterns: Top Examples & Uses
In the realm of software engineering, design patterns are blueprints that help solve common problems in an efficient and proven way. When it comes to structural design patterns, these patterns provide mechanisms for organizing classes or objects into larger structures. Let's delve into an exploration of five structural design patterns, their use cases, and examples.
The five GoF (Gang of Four) structural design patterns are Adapter, Bridge, Composite, Facade, and Decorator. Each pattern brings a unique solution to tackle distinct aspects of structuring your system's building blocks.
Adapter Pattern
The Adapter pattern allows incompatible interfaces to work together. In other words, it acts as a bridge between two incompatible interfaces, making them compatible.
One of the most practical examples of the Adapter pattern is theandar3D library in Java. It adapts the 3D scenes bundled with Collada DAE (.dae) file format to be compatible with Java3D.
Class Adapter Example
In a class adapter, the adapter class inherits from both the adaptation interface and the adapted class. Here's a simple Java example:
public class BirdAdapter extends Turkey implements Duck {
@Override
public void quack() {
gobble();
}
// More methods...
}
Object Adapter Example
In an object adapter, the adapter class holds an instance of the adaptation interface and delegates its methods to that instance.
public class BirdAdapter implements Duck {
private Turkey turkey;
public BirdAdapter(Turkey turkey) {
this.turkey = turkey;
}
@Override
public void quack() {
turkey.gobble();
}
// More methods...
}
Bridge Pattern
The Bridge pattern separates an abstraction into two separate hierarchies: the interface hierarchy and the implementation hierarchy.
Consider a simple remote control for a television. The remote control (abstraction) can have various operation modes (implementation), such as normal mode, stand-by mode, or child-lock mode. The Bridge pattern would decouple these two concerns.
RefactoredGameExample
In this example, the Bridge pattern separates the game interface from its implementation. The game is now independent of its rendering platform - either console or GUI.
// Platform interface abstraction
public interface Platform {
void loadGame();
}
// Concrete implementation classes
public class ConsolePlatform implements Platform {
@Override
public void loadGame() {
// Load game logic for console
}
}
// game interface
public interface Game {
void run(Platform platform);
}
// concrete Game implementation
public class MyGame implements Game {
@Override
public void run(Platform platform) {
platform.loadGame();
// Game logic here...
}
}
RefactoredTVExample
Similarly, a remotely controlled Television could be decoupled using the Bridge pattern.
// Remote control interface abstraction
public interface RemoteControl {
void turnOn();
void turnOff();
}
// Concrete implementation classes
public class SmartTVRemote {
@Override
public void turnOn() {
// Smart TV turning on logic
}
@Override
public void turnOff() {
// Smart TV turning off logic
}
}
// Television class
public class Television {
private final RemoteControl remoteControl;
public Television(RemoteControl remoteControl) {
this.remoteControl = remoteControl;
}
public void useRemote() {
remoteControl.turnOn();
// TV logic here...
remoteControl.turnOff();
}
}
As we've seen, structural design patterns offer compelling ways to organize your code more efficiently. From the Adapter pattern's interface bridging to the Bridge pattern's decoupling, these patterns enable coders to create more flexible and maintainable software. Keep these patterns in mind when evaluating your next system design challenge.