Adapter
- converts the interface of a class into another interface that a client expects, enabling classes to work together that couldn’t otherwise due to incompatible interfaces
usage
- when you want to use some existing class, but its interface isn’t compatible with the rest of your code
- when you want to reuse several existing subclasses that lack some common functionality that can’t be added to the superclass
pros
- Single Responsibility Principle (SRP)
- separate the interface or data conversion code from the primary business logic of the program
- Open/Closed Principle (OCP)
- introduce new types of adapters into the program without breaking the existing client code, as long as they work with the adapters through the client interface
cons
- overall complexity of the code increases because you need to introduce a set of new interfaces and classes, sometimes it’s simpler just to change the service class so that it matches the rest of your code
relations
- Adapter - Bridge
- both decouple an abstraction from its implementation
- Adapter is commonly used with an existing app to make some otherwise-incompatible classes work together nicely
- Bridge let's you develop parts of an application independently of each other
- Bridge - State - Strategy - and to some degree Adapter
- have very similar structures and all are based on composition, which is delegating work to other objects, but they all solve different problems
- Adapter - Decorator:
- Adapter provides a completely different interface for accessing an existing object
- Decorator adds responsibilities to objects without changing their interface and supports recursive composition
- Adapter - Facade:
- Adapter usually wraps just one object
- Facade works with an entire subsystem of objects
Bridge
- lets you split a large class or a set of closely related classes into two separate hierarchies - abstraction and implementation - which can be developed independently of each other
usage
- when you want to divide and organize a monolithic class that has several variants of some functionality (for example, if the class can work with various database servers)