Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Adapter Pattern.

Similar presentations


Presentation on theme: "The Adapter Pattern."— Presentation transcript:

1 The Adapter Pattern

2 Problem Specification:
You are given the following class library to draw shapes. Shape +display() +fill() +undisplay() Line Square

3 2. Now you are asked to add another class to deal with circle
2. Now you are asked to add another class to deal with circle. Your plan was: Shape +setLoccation() +getLocation() +display() +fill() +undisplay() Line Square Circle +display() +fill() +setColor() +undisplay()

4 3. Then your client said: “No,no,no”
3. Then your client said: “No,no,no”. You have to use this nice class library for drawing circles. Unfortunately, it is from a different vendor and it has a different interface. Shape +display() +fill() +undisplay() Line Square AnotherCircle +setLocation() +drawIt() +fillIt() +setItColor() +unDrawIt()

5 Two kinds of adapter pattern
Object adapter pattern The adapter class contains adaptee object Class adapter pattern Adapter class is inherited from both adaptee and abstract class

6 Object Adapter AbstractClass AdapterClass AdapteeClass

7 Adapter Design Pattern Solution Object Adapter
Shape +display() +fill() +undisplay() Line Square adaptee adapter Circle -aCircle:AnotherCircle +display() +fill() +undisplay() AnotherCircle +setLocation() +drawIt() +fillIt() +setItColor() +unDrawIt() void display(){ aCircle.drawIt(); } void fill(){ aCircle.fillIt(); } void undisplay(){ aCircle.unDrawIt(); }

8 class Circle extends Shape {
private AnotherCircle anotherCircle; public Circle(){ anotherCircle = new AnotherCircle(); } public void display() another.displayIt();

9 class Client { Shape aShape = new Circle; aShape.display(); //call Circle::display () }

10 Class Adapter AdapteeClass AbstractClass AdapterClass

11 Adapter Design Pattern Solution Class Adapter
Shape +display() +fill() +undisplay() Line Square AnotherCircle +setLocation() +drawIt() +fillIt() +setItColor() +unDrawIt() Circle +display() +fill() +undisplay() void display(){ drawIt(); } void fill(){ fillIt(); } void undisplay(){ unDrawIt(); }

12 Adapter 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. 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


Download ppt "The Adapter Pattern."

Similar presentations


Ads by Google