[go: up one dir, main page]

0% found this document useful (0 votes)
6 views8 pages

Adapter Pattern

Uploaded by

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

Adapter Pattern

Uploaded by

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

Adapter Pattern

Md Fahimul Islam, Dept of CSE, SUST


------------------------------------------------------------------------------------------------------------------
Core idea

❑ a structural design pattern that converts the interface of a class into another
interface that clients expect, allowing classes with incompatible interfaces to
work together
❑ An adapter converts one interface into another interface that a client already knows.
The client continues to call familiar methods. The adapter translates those calls and
forwards them to a different object that has an incompatible interface. This
preserves reuse and avoids changing existing client code.

Problem it solves
1. You want to use an existing class.
2. Its interface does not match what your client expects.
3. You cannot change the client or the existing class.
4. You add a small “translator” in the middle. That translator is the adapter.

Roles and structure

Target: The interface the client already uses.


Client: The code that calls the Target interface.
Adaptee: The existing class with the incompatible interface.
Adapter: The wrapper that implements Target and forwards to an Adaptee.

Scenario: Ducks vs. Turkeys


In your system, there is an existing interface called Duck.
You have many client classes that depend on ducks behaving in a specific way:

• Ducks quack.
• Ducks fly (long distances).
• Clients use only the Duck API.
Now, your team wants to integrate some Turkey objects into the same system.
However:

❑ Turkeys don’t quack → they gobble.


❑ Turkeys fly, but only short bursts.
❑ The Turkey interface is completely different from the Duck interface.
❑ Changing all existing client code to work with Turkeys would be too costly.

Problem:
How do we make Turkeys look like Ducks so existing code can work without
modification?
Requirements

• Don’t modify existing client code → clients expect a Duck.


• Don’t change the Duck interface → it's used widely.
• Don’t change the Turkey interface → it’s part of an external library.
• Must make Turkeys compatible with existing Duck-using code.

Solution: Use the Adapter Pattern


You create a TurkeyAdapter:

❑ It implements the Duck interface → so client code sees it as a Duck.


❑ It wraps a Turkey object → holds an instance internally.
❑ It translates Duck calls into Turkey calls:
➢ When the client calls quack(), the adapter calls gobble().
➢ When the client calls fly(), the adapter calls fly() on the Turkey five times to
simulate long-distance flight.

Code:
Interfaces and concrete classes

Duck

Turkeys that don’t match the Duck interface


The adapter

Client

You might also like