Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Adapter

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

• Pattern Name: Adapting Interface

• Intent:Adapter design pattern is a structural pattern that allows


incompatible classes or interfaces to work together. It acts as a bridge
between two incompatible interfaces by creating an intermediate adapter
object that translates requests from one interface to the other.
OR
• Convert the interface of a class into another interface clients excepts
Adapter lets classes work together that couldn’t otherwise because of
incompatible interfaces.

The Adapter pattern is applicable in the following design situations:

Integrating existing software systems that have incompatible interfaces: When you need to use an
existing software component in a new system, but its interface is incompatible with the new system's
interface, the Adapter pattern can be used to make the existing component compatible by providing a
wrapper that translates its interface to the new system's interface.

Reusing existing code: The Adapter pattern can be used to reuse existing code by providing a new
interface to it. This can be useful when you have a legacy system that you want to integrate into a new
system.

Creating reusable classes: The Adapter pattern can be used to create a reusable class that can be used in
different contexts. By providing a standard interface, the Adapter pattern makes it possible to use the
same class in different systems that have different interfaces.
Providing a simplified interface: The Adapter pattern can be used to provide a simplified interface to a
complex system. By providing a wrapper that exposes only the necessary functionality, the Adapter
pattern can make it easier to use a complex system.

Decoupling interfaces: The Adapter pattern can be used to decouple interfaces. By providing a wrapper
that translates one interface to another, the Adapter pattern can make it possible to change either
interface without affecting the other.

Target interface: This is the interface that the client code uses to interact with the adaptee (the
incompatible interface). The target interface specifies the methods that the client code expects to call,
but it does not specify how these methods are implemented or how they interact with the adaptee.

Adaptee interface: This is the interface that the existing code (the adaptee) uses to interact with the
external world. The adaptee interface specifies the methods that the existing code provides, but these
methods may not be compatible with the target interface.

Adapter class: This is the class that adapts the adaptee interface to the target interface. The adapter
class implements the target interface and delegates the calls to the adaptee interface. The adapter class
may need to perform some conversions or adaptations to ensure that the calls are compatible between
the two interfaces.

Therefore, to implement the Adapter design pattern, you need to have at least two interfaces (the
target and the adaptee) and one adapter class that implements the target interface and uses the
adaptee interface internally. In some cases, you may also need additional helper classes or functions to
perform the necessary conversions or adaptations.
In the Adapter design pattern, the participants collaborate to carry out their responsibilities in the
following way:

The client code calls the methods of the Target interface, assuming that they are compatible with its
needs.

The Adapter class receives the calls from the client code and translates them into calls to the Adaptee
interface, which has a different method signature or behavior.

The Adaptee interface receives the translated calls from the Adapter class and performs the actual work,
using its own implementation logic.

The Adaptee interface returns the results of its work to the Adapter class, which may need to perform
additional translations or adaptations to fit them into the expected format of the Target interface.

The Adapter class returns the final results to the client code, which can continue to use them as if they
were obtained from the Target interface directly.

Overall, the Adapter design pattern allows the client code to interact with the Adaptee interface
indirectly, through the Adapter class, which acts as a bridge between the two incompatible interfaces.
The Adapter class takes care of the necessary translations and adaptations, so that the client code can
work with the Adaptee interface as if it were a compatible Target interface.

Compatibility of interfaces: The main force that drives the need for the Adapter pattern is the
incompatibility between two interfaces. The more incompatible the interfaces are, the more effort and
complexity is required to implement the Adapter pattern. The trade-off is that the Adapter pattern may
introduce some overhead or performance cost, depending on the amount and frequency of data
translations or adaptations that need to be performed.
Flexibility of design: The Adapter pattern can provide more flexibility in the design of the system, by
allowing different components to use different interfaces without knowing each other's details.
However, this flexibility may come at the cost of increased complexity or indirection, which can make
the code harder to understand or modify.

Reusability of code: The Adapter pattern can promote code reuse, by allowing existing components to
be adapted to new contexts or requirements. However, this reuse may depend on the specific
implementation details of the Adapter class, which may not be applicable or useful in other contexts or
scenarios.

Maintainability of code: The Adapter pattern can improve the maintainability of the code, by separating
the concerns of different components and reducing the impact of changes in one component on the
others. However, this separation may also introduce some overhead or duplication of code, which can
make the code harder to maintain or update.

Performance of code: The Adapter pattern can have a performance impact, especially if the translation
or adaptation of data requires significant processing or memory resources. Therefore, it's important to
consider the performance requirements of the system and optimize the implementation of the Adapter
pattern accordingly.

Consistency of interfaces: The Adapter pattern can introduce some inconsistencies or deviations from
the standard conventions of the language or framework, depending on the way it's implemented.
Therefore, it's important to ensure that the Adapter class follows the naming conventions,
documentation, and error handling practices of the target interface, to avoid confusion or errors in the
client code

Decorator pattern: The Decorator pattern is similar to the Adapter pattern, but instead of adapting the
interface of an existing object, it enhances its functionality by adding new features or behaviors. The
Decorator pattern can be used to extend the capabilities of an object without changing its interface,
while the Adapter pattern is used to make two incompatible interfaces work together.
Bridge pattern: The Bridge pattern is similar to the Adapter pattern, but it focuses on decoupling the
abstraction (the high-level interface) from its implementation (the low-level interface), rather than
adapting them to each other. The Bridge pattern can be used to provide multiple implementations of an
abstraction, while the Adapter pattern is used to provide a single implementation of a compatible
interface.

Facade pattern: The Facade pattern is similar to the Adapter pattern, but it provides a simplified
interface to a complex system or subsystem, rather than adapting two incompatible interfaces. The
Facade pattern can be used to hide the complexity of a system from the client code, while the Adapter
pattern is used to make two incompatible interfaces work together.

Proxy pattern: The Proxy pattern is similar to the Adapter pattern, but it provides a surrogate object that
acts as a placeholder or representative for another object, rather than adapting its interface. The Proxy
pattern can be used to control the access to an object, while the Adapter pattern is used to make two
incompatible interfaces work together.

Template method pattern: The Template method pattern is similar to the Adapter pattern, but it focuses
on defining a skeleton algorithm that can be customized by subclasses, rather than adapting two
incompatible interfaces. The Template method pattern can be used to define a generic behavior that
can be tailored to different contexts, while the Adapter pattern is used to make two incompatible
interfaces work together

You might also like