X

Download ADAPTER PATTERN PowerPoint Presentation

SlidesFinder-Advertising-Design.jpg

Login   OR  Register
X


Iframe embed code :



Presentation url :

Home / Computers & Web / Computers & Web Presentations / ADAPTER PATTERN PowerPoint Presentation

ADAPTER PATTERN PowerPoint Presentation

Ppt Presentation Embed Code   Zoom Ppt Presentation

PowerPoint is the world's most popular presentation software which can let you create professional ADAPTER PATTERN powerpoint presentation easily and in no time. This helps you give your presentation on ADAPTER PATTERN in a conference, a school lecture, a business proposal, in a webinar and business and professional representations.

The uploader spent his/her valuable time to create this ADAPTER PATTERN powerpoint presentation slides, to share his/her useful content with the world. This ppt presentation uploaded by worldwideweb in Computers & Web ppt presentation category is available for free download,and can be used according to your industries like finance, marketing, education, health and many more.

About This Presentation

Slide 1 - ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010
Slide 2 - 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
Slide 3 - 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
Slide 4 - Motivation Example: Amirkabir University of Technoloy Computer Engineering Department slide 4
Slide 5 - 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
Slide 6 - Structure A class adapter uses multiple inheritance to adapt one interface to another: Amirkabir University of Technoloy Computer Engineering Department slide 6
Slide 7 - Structure An object adapter relies on object composition: Amirkabir University of Technoloy Computer Engineering Department slide 7
Slide 8 - Collaboration Amirkabir University of Technoloy Computer Engineering Department slide 8
Slide 9 - 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
Slide 10 - 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
Slide 11 - 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
Slide 12 - 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
Slide 13 - 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
Slide 14 - 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
Slide 15 - 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
Slide 16 - 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
Slide 17 - 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... 17 Amirkabir University of Technoloy Computer Engineering Department
Slide 18 - 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
Slide 19 - 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
Slide 20 - Example 2 (continued) 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 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
Slide 21 - 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
Slide 22 - 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
Slide 23 - 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
Slide 24 - 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
Slide 25 - 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
Slide 26 - 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
Slide 27 - 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... RoundPeg insertIntoHole(): Inserting round peg... SquarePeg insert(): Inserting square peg... 27 Amirkabir University of Technoloy Computer Engineering Department
Slide 28 - 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
Slide 29 - Questions? Amirkabir University of Technoloy Computer Engineering Department slide 29