0% found this document useful (0 votes)
100 views3 pages

Adapter Design Pattern

The Adapter pattern allows incompatible interfaces to work together by providing a standard interface that both can use. It involves a class that joins the functionality of two independent interfaces to make them work together. An example is a card reader that acts as an adapter between a memory card and a laptop, allowing the memory card to be read by the laptop.

Uploaded by

adele5eve55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views3 pages

Adapter Design Pattern

The Adapter pattern allows incompatible interfaces to work together by providing a standard interface that both can use. It involves a class that joins the functionality of two independent interfaces to make them work together. An example is a card reader that acts as an adapter between a memory card and a laptop, allowing the memory card to be read by the laptop.

Uploaded by

adele5eve55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Similarity with another design pattern:

 Bridge is usually designed up-front, letting you develop parts of an application


independently of each other. On the other hand, Adapter is commonly used with
an existing app to make some otherwise-incompatible classes work together
nicely.

 Adapter changes the interface of an existing object, while Decorator enhances


an object without changing its interface. In addition, Decorator supports recursive
composition, which isn’t possible when you use Adapter.

 Adapter provides a different interface to the wrapped object, Proxy provides it


with the same interface, and Decorator provides it with an enhanced interface.
 Facade defines a new interface for existing objects, whereas Adapter tries to
make the existing interface usable. Adapter usually wraps just one object,
while Facade works with an entire subsystem of objects.

 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.

Similarity with another design pattern:

 Bridge is usually designed up-front, letting you develop parts of an application


independently of each other. On the other hand, Adapter is commonly used with an
existing app to make some otherwise-incompatible classes work together nicely.

 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).

You might also like