Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is Adapter Category: Structural Also known as ‘Wrapper’

Similar presentations


Presentation on theme: "What is Adapter Category: Structural Also known as ‘Wrapper’"— Presentation transcript:

1 What is Adapter Category: Structural Also known as ‘Wrapper’
Ties two disparate/dissimilar systems together ‘Adapts’ one object to meet the demands of another object Typically wires two different interfaces/classes together Analogous to the Square Peg/Round Hole metaphor

2 How to use Adapter Given class Source and class Target, define class Adapter that ties the two together. Class Adapter should implement Target, and take an instance of Source. For each method of Target, execute and adapt results from Source.

3 From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)
Class Diagram From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)

4 Why Adapter Adapter allows two very different systems to work together. Can mold an object to meet many different criteria (many adapters for one object)

5 Example (taken from UMBC.edu in references)
/** * The SquarePeg class. * This is the Target class. */ public class SquarePeg { public void insert(String str) { System.out.println("SquarePeg insert(): " + str); } * The RoundPeg class. * This is the Adaptee class. public class RoundPeg { public void insertIntoHole(String msg) { System.out.println("RoundPeg insertIntoHole(): " + msg); * 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); }

6 Adapter References (in addition to references in Introduction)


Download ppt "What is Adapter Category: Structural Also known as ‘Wrapper’"

Similar presentations


Ads by Google