The Adapter Pattern: Bridging Incompatible Interfaces in Software Design - Prof. Jeff K. S, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-o7z-1
koofers-user-o7z-1 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 350
CS 350
Software Design
Software Design
The Adapter Pattern
The Adapter Pattern
Chapter 7
Chapter 7
Gang of Four Definition:
Gang of Four Definition:
Convert the interface of a class into another interface that the
Convert the interface of a class into another interface that the client expects. Adapter lets classes
client expects. Adapter lets classes
work together that could not otherwise because of incompatible i
work 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.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download The Adapter Pattern: Bridging Incompatible Interfaces in Software Design - Prof. Jeff K. S and more Study notes Computer Science in PDF only on Docsity!

CS 350

CS 350 –

– Software Design

Software Design

The Adapter Pattern

The Adapter Pattern –

– Chapter 7

Chapter 7

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.

CS 350

CS 350 –

– Software Design

Software Design

The Adapter Pattern

The Adapter Pattern –

– Chapter 7

Chapter 7

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:

CS 350

CS 350 –

– Software Design

Software Design

The Adapter Pattern

The Adapter Pattern –

– Chapter 7

Chapter 7

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.

CS 350

CS 350 –

– Software Design

Software Design

The Adapter Pattern

The Adapter Pattern –

– Chapter 7

Chapter 7

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.

CS 350

CS 350 –

– Software Design

Software Design

The Adapter Pattern

The Adapter Pattern –

– Chapter 7

Chapter 7

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.

CS 350

CS 350 –

– Software Design

Software Design

The Adapter Pattern

The Adapter Pattern –

– Chapter 7

Chapter 7

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();

CS 350

CS 350 –

– Software Design

Software Design

The Adapter Pattern

The Adapter Pattern –

– Chapter 7

Chapter 7

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