






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The adapter pattern is a design solution that enables incompatible classes to work together by converting the interface of one class into another interface expected by the client. This pattern is essential when dealing with systems that have the right data and behavior but the wrong interface. In this document, we explore the gang of four definition, a simple example, and the implementation of the adapter pattern.
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Gang of Four Definition: Gang of Four Definition:
Convert the interface of a class into another interface that the client expects. Adapter lets classesConvert the interface of a class into another interface that the
client expects. Adapter lets classes
work together that could not otherwise because of incompatible iwork together that could not otherwise because of incompatible interfaces.
nterfaces.
Let Let’
’s look at a simple example:
s look at a simple example:
Imagine you Imagine you’
’ve been given the following requirements:
ve been given the following requirements:
Create classes for points, lines, and squares that have the beh Create classes for points, lines, and squares that have the behavior display.
avior display.
The client objects should not have to know whether they actuall The client objects should not have to know whether they actually have a point, a line, or a
y have a point, a line, or a
square. They just want to know that they have one of these shape square. They just want to know that they have one of these shapes.
s.
So although the system has points, lines, and squares, the clien So although the system has points, lines, and squares, the client objects only think they have
t objects only think they have
shapes. shapes.
This is a problem that cries out for polymorphism. This is a problem that cries out for polymorphism.
At your level of programming knowledge, it shouldn At your level of programming knowledge, it shouldn’
’t be hard to imagine how to develop
t be hard to imagine how to develop
such a program (given that you have an object to draw points on such a program (given that you have an object to draw points on the screen).
the screen).
We need to create a shape class and have the concrete classes of We need to create a shape class and have the concrete classes of point, line, and square
point, line, and square
derive from shape as in the following figure: derive from shape as in the following figure:
This works great. We should be very happy with ourselves, should This works great. We should be very happy with ourselves, shouldn
n’
’t we?
t we?
We created an intuitive solution, that is easy to understand and We created an intuitive solution, that is easy to understand and implement.
implement.
What could be wrong? Why do you sense I will find something wron What could be wrong? Why do you sense I will find something wrong with out elegant
g with out elegant
solution? solution?
What happens if I ask you to draw a circle?
How many of you kno
What happens if I ask you to draw a circle?
How many of you know the mathematics
w the mathematics
involved in drawing a circle?
Could you write the class in 10 m
involved in drawing a circle?
Could you write the class in 10 minutes, because that
inutes, because that’
’s all the
s all the
time I am going to give you? time I am going to give you?
If you were slick, you might realize that circle code probably h If you were slick, you might realize that circle code probably has been written over and over
as been written over and over
again. Why not lift code from another place? again. Why not lift code from another place?
Forget the legal issues. Can you do it? Forget the legal issues. Can you do it?
Sure, but the interface of the code you find probably doesn Sure, but the interface of the code you find probably doesn’
’t match your interface.
t match your interface.
So what do you do if you have a great class, but the wrong inter So what do you do if you have a great class, but the wrong interface?
face?
One solution is to take it apart and use the internals and write One solution is to take it apart and use the internals and write a new class that subscribes to
a new class that subscribes to
your interface. your interface.
This solution is risky.This solution is risky.You could introduce errors.You could introduce errors.You need to learn more about the existing code, which may not be as trivial as this example.You need to learn more about the existing code, which may not be
as trivial as this example.
A better solution is to encapsulate the object with another wrap A better solution is to encapsulate the object with another wrapper
per-
-like class. In essence
like class. In essence
ADAPT the existing class to your interface with another class. ADAPT the existing class to your interface with another class.
Create a Circle object that encapsulates Create a Circle object that encapsulates XXCircle
XXCircle by making an
by making an XXCircle
XXCircle object an attribute of
object an attribute of
the Circle class. the Circle class.
Some sample code to help you see how the adapter works: Some sample code to help you see how the adapter works:
Class Circle extends Shape { Class Circle extends Shape {
private private XXCircle
XXCircle myXXCircle
myXXCircle;
public Circle () { public Circle () {
myXXCircle myXXCircle = new
= new XXCircle
XXCircle();
void public display() { void public display() {
myXXCircle.displayIt myXXCircle.displayIt();
Differences between Adapter and Fa Differences between Adapter and Faç
çade
ade
These patterns are often confused. Here These patterns are often confused. Here’
’s a simple chart to show you the differences:
s a simple chart to show you the differences:
Fa Faç
çade
ade
Adapter Adapter
Are there preexisting classes? Are there preexisting classes?
Yes Yes
Yes Yes
Is there an interface we MUST design to? Is there an interface we MUST design to?
No No
Yes Yes
Does an object need to behave Does an object need to behave polymorphically
polymorphically?
No No
Probably Probably
Is a simpler interface needed? Is a simpler interface needed?
Yes Yes
No No