Design Patterns | Adapter
Structural pattern - Adapter
It allows you to connect objects with different interfaces.
Use cases:
- When you have existing classes, but its’ interfaces isn’t compatible with your code
Let’s imagine next situation:
You wanna stream your computer desktop(with VGA OUTPUT) on TV. You only have VGA connector. In case if your TV compatible with VGA you just need to plug in connector, otherwise you need to get an adapter which convert all your computer signals to proper destination.
- Define
VGAClient
. In this case this will be your connector with VGA output - Define two types of devices that support VGA and HDMI Inputs
- Define two concrete classes of
VGADevice and HDMIDevice
- Create and adapter that accepts hdmi signal and convert it to VGA
1 | type VgaClient struct{} |