Adapter Design Pattern
Adapter Design Pattern
Adapter pattern works as a bridge between two incompatible interfaces. This type of
design pattern comes under structural pattern as this pattern combines the capability of
two independent interfaces.
This pattern involves a single class which is responsible to join functionalities of
independent or incompatible interfaces. A real life example could be a case of card
reader which acts as an adapter between memory card and a laptop. You plugin the
memory card into card reader and card reader into the laptop so that memory card can
be read via the
laptop.
SOLID Principle:
Single Responsibility Principle. You can separate the interface or data conversion code
from the primary business logic of the program.
Open/Closed Principle. You can 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.
Bridge, State, Strategy (and to some degree Adapter) have very similar
structures. Indeed, all of these patterns are based on composition, which is
delegating work to other objects. However, they all solve different problems. A
pattern isn’t just a recipe for structuring your code in a specific way. It can also
communicate to other developers the problem the pattern solves.
Scenario:
A typical scenario for using the Adapter pattern is when you have an existing codebase with an
interface that you cannot modify, but you need to use it with a new interface that your client
code expects. In this case, you can create an adapter that implements the new interface and
internally delegates the requests to the old interface.
Bridge:
Scenario:
A typical scenario for using the Bridge pattern is when you have a system that needs to work
with different types of databases. Rather than hard-coding the database implementation into
your code, you can use the Bridge pattern to separate the database interface from its
implementation. This allows you to switch between different database implementations at
runtime, without affecting the rest of the system
Uses:
Use the Bridge pattern 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).
Use the pattern when you need to extend a class in several orthogonal (independent)
dimensions.
Use the Bridge if you need to be able to switch implementations at runtime.
Bridge, State, Strategy (and to some degree Adapter) have very similar structures.
Indeed, all of these patterns are based on composition, which is delegating work to
other objects. However, they all solve different problems. A pattern isn’t just a recipe for
structuring your code in a specific way. It can also communicate to other developers the
problem the pattern solves.
You can use Abstract Factory along with Bridge. This pairing is useful when some
abstractions defined by Bridge can only work with specific implementations. In this
case, Abstract Factory can encapsulate these relations and hide the complexity from
the client code.
You can combine Builder with Bridge: the director class plays the role of the
abstraction, while different builders act as implementations.
Composite:
Uses:
Use the Composite pattern when you have to implement a tree-like object structure
Use the pattern when you want the client code to treat both simple and complex
elements uniformly.
Scenario:
A typical scenario for using the Composite pattern is when you have a GUI framework that
needs to support a hierarchy of user interface elements, such as windows, panels, buttons, and
text boxes. Rather than treating each element as a separate entity, you can use the Composite
pattern to represent the hierarchy as a tree of objects, where each object can be either a leaf
object (such as a button or text box) or a composite object (such as a window or panel).