ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm.
Patterns – Day 4 Interfaces and Adapter Reminders: Faculty candidate talks Monday and Thursday. No class on Monday.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Patterns – Day 5 Adapter Reminders: Faculty candidate talk Thursday. No class next Tuesday. Course newsgroup: rhit.cs.patterns.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design Adapter Pattern Façade pattern.
1 Structural Patterns  How classes and objects are composed to form larger structures  Structural class patterns: use inheritance to compose interfaces.
ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.
Java Design Patterns 1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Emil Vassev, Joey Paquet :
Object Adapter Pattern Danny Leavitt. Imagine... You program for the control center of a US phone company. Your network managment software is Object Oriented.
Computer Science 313 – Advanced Programming Topics.
1 Unit 5 Design Patterns: Design by Abstraction. 2 What Are Design Patterns?  Design patterns are: Schematic descriptions of design solutions to recurring.
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 0 CSE3311 – Software Design Adapter Pattern.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMING PRACTICES Design patterns.
Computing IV Introduction to Design Pattern Xinwen Fu.
Design Patterns – II Lecture IV. Singleton Pattern Intent – Ensure a class only has one instance, and provide a global point of access to it Motivation.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
By Shishir Kumar Contact:
Structural Design Patterns
COMP 121 Week 12: Decorator and Adapter Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Design patterns.
Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Adaptor Bridge Composite UNIT-IV1 Repeated key points for Structural Patterns (Intent, Motivation, Also Known As…) Code Examples Reference
CS 350 – Software Design The Adapter Pattern – Chapter 7 Gang of Four Definition: Convert the interface of a class into another interface that the client.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
S.Ducasse Stéphane Ducasse 1 Adapter.
Structural Patterns C h a p t e r 4 – P a g e 55 StructuralPatterns Design patterns that describe how classes and objects can be combined to form larger.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Examples (D. Schmidt et al)
Structural Patterns How classes and objects are composed to form larger structures Structural class patterns: use inheritance to compose interfaces or.
Design Patterns: Brief Examples
Design Patterns Lecture part 2.
Design Patterns C++ Java C#.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Factory Patterns 1.
By SmartBoard team Adapter pattern.
Design Patterns C++ Java C#.
Software Design and Architecture
Adapter Design Pattern
Adapter Pattern 1.
Object Oriented Design Patterns - Structural Patterns
Advanced Programing practices
What is Adapter Category: Structural Also known as ‘Wrapper’
Structural Pattern part-I introduction
Structural Patterns: Adapter and Bridge
Introduction to Design Patterns
Adapter Design Pattern
The Adapter Pattern.
Decorator Pattern.
Adapter Pattern Jim Fawcett
Software Design Lecture 10.
Adapter Pattern Jim Fawcett
Presentation transcript:

ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010

Design Patterns presentation: Adapter pattern Winter 2010 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.  Wrap an existing class with a new interface.  Also Known As -> Wrapper Amirkabir University of Technoloy Computer Engineering Department slide 2

Design Patterns presentation: Adapter pattern Winter 2010 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 Amirkabir University of Technoloy Computer Engineering Department slide 3

Design Patterns presentation: Adapter pattern Winter 2010 Motivation  Example: Amirkabir University of Technoloy Computer Engineering Department slide 4

Design Patterns presentation: Adapter pattern Winter 2010 Participants  Target (shape)  defines the domain-specific interface that Client uses.  Adapter (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. Amirkabir University of Technoloy Computer Engineering Department slide 5

Design Patterns presentation: Adapter pattern Winter 2010 Structure  A class adapter uses multiple inheritance to adapt one interface to another: Amirkabir University of Technoloy Computer Engineering Department slide 6

Design Patterns presentation: Adapter pattern Winter 2010 Structure  An object adapter relies on object composition: Amirkabir University of Technoloy Computer Engineering Department slide 7

Design Patterns presentation: Adapter pattern Winter 2010 Collaboration Amirkabir University of Technoloy Computer Engineering Department slide 8

Design Patterns presentation: Adapter pattern Winter 2010 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 Amirkabir University of Technoloy Computer Engineering Department slide 9

Design Patterns presentation: Adapter pattern Winter 2010 Consequences  Class adapter  Concrete Adapter class  Unknown Adaptee subclasses might cause problem  Overloads Adaptee behavior  Introduces only one object  Object adapter  Adapter can service many different Adaptees  May require the creation of Adaptee subclasses and referencing those objects Amirkabir University of Technoloy Computer Engineering Department slide 10

Design Patterns presentation: Adapter pattern Winter 2010 Implementation  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 Amirkabir University of Technoloy Computer Engineering Department slide 11

Design Patterns presentation: Adapter pattern Winter 2010 Implementation  Adapter should be subtype of Target  Pluggable adapters should use the narrowest definition  Abstract operations to minimize exposed interface  Delegated objects to localize behavior  Parameterized processing avoids subclasses of adaptee Amirkabir University of Technoloy Computer Engineering Department slide 12

Design Patterns presentation: Adapter pattern Winter 2010 Example 1  The classic round pegs and square pegs!  Here's the SquarePeg class: /** * The SquarePeg class. * This is the Target class. */ public class SquarePeg { public void insert(String str) { System.out.println("SquarePeg insert(): " + str); } 13 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 1 (continued)  And the RoundPeg class: /** * The RoundPeg class. * This is the Adaptee class. */ public class RoundPeg { public void insertIntoHole(String msg) { System.out.println("RoundPeg insertIntoHole(): " + msg); } If a client only understands the SquarePeg interface for inserting pegs using the insert() method, how can it insert round pegs? A peg adapter! 14 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 1 (continued)  Here is the PegAdapter class: /** * The PegAdapter class. * This is the Adapter class. * It adapts a RoundPeg to a SquarePeg. * Its interface is that of a SquarePeg. */ public class PegAdapter extends SquarePeg { private RoundPeg roundPeg; public PegAdapter(RoundPeg peg) {this.roundPeg = peg;} public void insert(String str) {roundPeg.insertIntoHole(str);} } 15 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 1 (continued)  Typical client program: // Test program for Pegs. public class TestPegs { public static void main(String args[]) { // Create some pegs. RoundPeg roundPeg = new RoundPeg(); SquarePeg squarePeg = new SquarePeg(); // Do an insert using the square peg. squarePeg.insert("Inserting square peg..."); 16 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 1 (continued) // Now we'd like to do an insert using the round peg. // But this client only understands the insert() // method of pegs, not a insertIntoHole() method. // The solution: create an adapter that adapts // a square peg to a round peg! PegAdapter adapter = new PegAdapter(roundPeg); adapter.insert("Inserting round peg..."); }  Client program output: SquarePeg insert(): Inserting square peg... RoundPeg insertIntoHole(): Inserting round peg Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 2  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 sort(List numbers) { //sort and return return new ArrayList (); } Amirkabir University of Technoloy Computer Engineering Department slide 18

Design Patterns presentation: Adapter pattern Winter 2010 Example 2 (continued)  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); } Amirkabir University of Technoloy Computer Engineering Department slide 19

Design Patterns presentation: Adapter pattern Winter 2010 Example 2 (continued)  Finally, the SortListAdapter implements our target interface and deals with our adaptee, NumberSorter public class SortListAdapter implements Sorter public int[] sort(int[] numbers) { //convert the array to a List List numberList = new ArrayList (); //call the adapter NumberSorter sorter = new NumberSorter(); numberList = sorter.sort(numberList); //convert the list back to an array and return return sortedNumbers; } Amirkabir University of Technoloy Computer Engineering Department slide 20

Design Patterns presentation: Adapter pattern Winter 2010 Example 2 (Continued) int[] numbers = new int[]{34, 2, 4, 12, 1}; Sorter sorter = new SortListAdapter(); sorter.sort(numbers); Amirkabir University of Technoloy Computer Engineering Department slide 21

Design Patterns presentation: Adapter pattern Winter 2010 Example 3  Notice in Example 1 that the PegAdapter adapts a RoundPeg to a SquarePeg. The interface for PegAdapter is that of a SquarePeg.  What if we want to have an adapter that acts as a SquarePeg or a RoundPeg? Such an adapter is called a two-way adapter.  One way to implement two-way adapters is to use multiple inheritance, but we can't do this in Java  But we can have our adapter class implement two different Java interfaces! 22 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 3 (continued)  Here are the interfaces for round and square pegs: /** *The IRoundPeg interface. */ public interface IRoundPeg { public void insertIntoHole(String msg); } /** *The ISquarePeg interface. */ public interface ISquarePeg { public void insert(String str); } 23 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 3 (continued)  Here are the new RoundPeg and SquarePeg classes. These are essentially the same as before except they now implement the appropriate interface. // The RoundPeg class. public class RoundPeg implements IRoundPeg { public void insertIntoHole(String msg) { System.out.println("RoundPeg insertIntoHole(): " + msg); } // The SquarePeg class. public class SquarePeg implements ISquarePeg { public void insert(String str) { System.out.println("SquarePeg insert(): " + str); } 24 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 3 (continued)  And here is the new PegAdapter: /** * The PegAdapter class. * This is the two-way adapter class. */ public class PegAdapter implements ISquarePeg, IRoundPeg { private RoundPeg roundPeg; private SquarePeg squarePeg; public PegAdapter(RoundPeg peg) {this.roundPeg = peg;} public PegAdapter(SquarePeg peg) {this.squarePeg = peg;} public void insert(String str) {roundPeg.insertIntoHole(str);} public void insertIntoHole(String msg){squarePeg.insert(msg);} } 25 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 3 (continued)  A client that uses the two-way adapter: // Test program for Pegs. public class TestPegs { public static void main(String args[]) { // Create some pegs. RoundPeg roundPeg = new RoundPeg(); SquarePeg squarePeg = new SquarePeg(); // Do an insert using the square peg. squarePeg.insert("Inserting square peg..."); // Create a two-way adapter and do an insert with it. ISquarePeg roundToSquare = new PegAdapter(roundPeg); roundToSquare.insert("Inserting round peg..."); 26 Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Example 3 (continued) // Do an insert using the round peg. roundPeg.insertIntoHole("Inserting round peg..."); // Create a two-way adapter and do an insert with it. IRoundPeg squareToRound = new PegAdapter(squarePeg); squareToRound.insertIntoHole("Inserting square peg..."); }  Client program output: SquarePeg insert(): Inserting square peg... RoundPeg insertIntoHole(): Inserting round peg... SquarePeg insert(): Inserting square peg Amirkabir University of Technoloy Computer Engineering Department

Design Patterns presentation: Adapter pattern Winter 2010 Related Patterns  The Bridge pattern shares structure with object adapter, but diverges on intent. Bridge is concerned with separating an object's interface from its implementation while adapter changes the interface of an existing object  The Decorator pattern does not change an object's interface, but it does support recursive composition which adapter does not. In this way, Decorator is more flexible than adapter for dealing with functionality additions Amirkabir University of Technoloy Computer Engineering Department slide 28

Design Patterns presentation: Adapter pattern Winter 2010 Questions? Amirkabir University of Technoloy Computer Engineering Department slide 29