Structural Design Patterns
6 min readNov 22, 2023
Structural design patterns deal with the composition of classes and objects to form larger structures, such as relationships between objects, and focus on how these structures can be used to solve specific problems. There are seven commonly recognized structural design patterns:
1. Adapter Pattern:
- Purpose: Allows the interface of an existing class to be used as another interface.
- Use Case: When you want to make an existing class work with others without modifying its source code or when integrating legacy code with modern systems.
- Example: Consider a system that expects a
USB
interface but you have aPS2
device. You can create an adapter class to make thePS2
device compatible with theUSB
interface.
// Existing class with a different interface
class PS2Device {
public void connectPS2() {
System.out.println("Connected via PS2");
}
}
// Target interface expected by the client
interface USBDevice {
void connectUSB();
}
// Adapter class to make PS2Device compatible with USB
class PS2ToUSBAdapter implements USBDevice {
private PS2Device ps2Device;
public PS2ToUSBAdapter(PS2Device device) {
this.ps2Device = device;
}
@Override
public void connectUSB() {
ps2Device.connectPS2();
System.out.println("Adapter converts…