adapter design pattern

23
ADAPTER PATTERN Adeel Riaz Mirza Danish Baig Malik Mohammad Sohaib Farhan Ahmed Advisor: Muhammad Qasim pasta PAF KIET Fall 11 1

Upload: adeel-riaz

Post on 21-Jul-2015

70 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Adapter Design Pattern

ADAPTER

PATTERN

Adeel Riaz

Mirza Danish Baig

Malik Mohammad Sohaib

Farhan Ahmed

Advisor: Muhammad Qasim pasta

PAF KIET Fall 11

1

Page 2: Adapter Design Pattern

Real World Adapter

Slide 2PAF KIET Fall 11

Page 3: Adapter Design Pattern

Adapter in our Object Oriented

Slide 3PAF KIET Fall 11

Page 4: Adapter Design Pattern

Problem in Object Oriented??

Slide 4PAF KIET Fall 11

Page 5: Adapter Design Pattern

Problem in Object Oriented??

Slide 5

I have already written class called xyzcircle that deals with circles already. But we can’t use directly because I want to preserve polymorphic behavior with Shape

PAF KIET Fall 11

Page 6: Adapter Design Pattern

Problem in Object Oriented??

Slide 6PAF KIET Fall 11

6

Page 7: Adapter Design Pattern

Intent

Convert the interface of a class into

another interface clients expect.

Adapter lets classes work together that

couldn't otherwise because of incompatible

interfaces.

Also Known As -> Wrapper

Slide 7PAF KIET Fall 11

Page 8: Adapter Design Pattern

Motivation

Sometimes a toolkit or class library can not

be used because its interface is incompatible

with the interface required by an application

We can not change the library interface,

since we may not have its source code

Even if we did have the source code, we

probably should not change the library for

each domain-specific application

Slide 8PAF KIET Fall 11

Page 9: Adapter Design Pattern

Motivation

Example:

Slide 9PAF KIET Fall 11

Page 10: Adapter Design Pattern

Applicability

Use the Adapter pattern when

You want to use an existing class, and

its interface does not match the one you

need

You want to create a reusable class

that cooperates with unrelated classes

with incompatible interfaces

Slide 10PAF KIET Fall 11

Page 11: Adapter Design Pattern

Structure An object adapter relies on object

composition:

Slide 11PAF KIET Fall 11

Page 12: Adapter Design Pattern

Participants

Target (Shape)• defines the domain-specific interface that Client uses.

Adapter (Line,TextShape)• adapts the interface Adaptee to the Target interface.

Adaptee (TextView)• defines an existing interface that needs adapting.

Client (DrawingEditor)• collaborates with objects conforming to the Target interface.

Slide 12PAF KIET Fall 11

Page 13: Adapter Design Pattern

Example

Slide 13PAF KIET Fall 11

Page 14: Adapter Design Pattern

Example

Slide 14PAF KIET Fall 11

Page 15: Adapter Design Pattern

Sequence Diagram

Slide 15PAF KIET Fall 11

Page 16: Adapter Design Pattern

Consequences

Class adapter

Concrete Adapter class

Unknown Adaptee subclasses might cause problem

Object adapter

Adapter can service many different Adaptees

May require the creation of Adaptee subclasses and

referencing those objects

Slide 16PAF KIET Fall 11

Page 17: Adapter Design Pattern

Implementation

slide 17

How much adapting should be done?

Simple interface conversion that just changes operation names and order of arguments

Totally different set of operations

Does the adapter provide two-way transparency?

A two-way adapter supports both the Target and the Adaptee interface. It allows an adapted object (Adapter) to appear as an Adaptee object or a Target object

PAF KIET Fall 11

Page 18: Adapter Design Pattern

Comparing the Facade Pattern with the

Adapter Pattern

Slide 18

In both the Facade and Adapter pattern I have preexisting classes.

In the Facade, however, I do not have an interface I must design to, as I do in the Adapter

pattern.

I am not interested in polymorphic behavior in the Façade, while in the Adapter, I probably

am. (There are times when we just need to design to a particular API and therefore must use

an Adapter. In this case, polymorphism may not be an issue— that's why I say "probably").

In the case of the Facade pattern, the motivation is to simplify the interface. With the

Adapter, while simpler is better, I am trying to design to an existing interface and cannot

simplify things even if a simpler interface were otherwise possible.

PAF KIET Fall 11

Page 19: Adapter Design Pattern

Example 1

slide 19

Consider that we have a third party library that provides sorting functionality through it's NumberSorter class. This is our Adaptee

/*

* This is our adaptee, a third party implementation of a

* number sorter that deals with Lists, not arrays.

*/

public class NumberSorter

{

public List<Integer> sort(List<Integer> numbers)

{

//sort and return

return new ArrayList<Integer>();

}

}

PAF KIET Fall 11

Page 20: Adapter Design Pattern

Example 1 (continued)

slide 20

Our Client deals with primitive arrays rather than Lists. For the sake of this example, lets say we can't change the client to use Lists.

We've provided a Sorter interface that expects the client input. This is our target.

//this is our Target interface

public interface Sorter

{

public int[] sort(int[] numbers);

}

PAF KIET Fall 11

Page 21: Adapter Design Pattern

Example 1 (continued)

slide 21

Finally, the SortListAdapter implements our target interface and deals with our adaptee, NumberSorter

public class SortListAdapter implements Sorter

{

@Override

public int[] sort(int[] numbers)

{

//convert the array to a List

List<Integer> numberList = new ArrayList<Integer>();

//call the adapter

NumberSorter sorter = new NumberSorter();

numberList = sorter.sort(numberList);

//convert the list back to an array and return

return sortedNumbers;

}

}

PAF KIET Fall 11

Page 22: Adapter Design Pattern

Example 1 (Continued)

slide 22

int[] numbers = new int[]{34, 2, 4, 12, 1};

Sorter sorter = new SortListAdapter();

sorter.sort(numbers);

PAF KIET Fall 11

Page 23: Adapter Design Pattern

Questions?

slide 23PAF KIET Fall 11