Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 expects. Adapter lets classes work together that could not otherwise because of incompatible interfaces. Let’s look at a simple example: Imagine you’ve been given the following requirements: Create classes for points, lines, and squares that have the behavior display. Create classes for points, lines, and squares that have the behavior display. The client objects should not have to know whether they actually have a point, a line, or a square. They just want to know that they have one of these shapes. The client objects should not have to know whether they actually have a point, a line, or a square. They just want to know that they have one of these shapes. So although the system has points, lines, and squares, the client objects only think they have shapes.

2 CS 350 – Software Design The Adapter Pattern – Chapter 7 This is a problem that cries out for polymorphism. At your level of programming knowledge, it shouldn’t be hard to imagine how to develop such a program (given that you have an object to draw points on the screen). We need to create a shape class and have the concrete classes of point, line, and square derive from shape as in the following figure:

3 CS 350 – Software Design The Adapter Pattern – Chapter 7 We define a series of behaviors (methods) that all Shapes will have in common in the Shape class and then override their behavior in the concrete classes of Point, Line, and Square. A Shape must be able to: –Set it’s location –Get it’s Location –Display itself –Fill itself –Set it’s color –Undisplay itself A more detailed UML specification is shown to the right:

4 CS 350 – Software Design The Adapter Pattern – Chapter 7 This works great. We should be very happy with ourselves, shouldn’t we? We created an intuitive solution, that is easy to understand and implement. What could be wrong? Why do you sense I will find something wrong with out elegant solution? What happens if I ask you to draw a circle? How many of you know the mathematics involved in drawing a circle? Could you write the class in 10 minutes, because that’s all the time I am going to give you? If you were slick, you might realize that circle code probably has been written over and over again. Why not lift code from another place? Forget the legal issues. Can you do it? Sure, but the interface of the code you find probably doesn’t match your interface.

5 CS 350 – Software Design The Adapter Pattern – Chapter 7 So what do you do if you have a great class, but the wrong interface? One solution is to take it apart and use the internals and write a new class that subscribes to your interface. This solution is risky. You could introduce errors. You need to learn more about the existing code, which may not be as trivial as this example. A better solution is to encapsulate the object with another wrapper-like class. In essence ADAPT the existing class to your interface with another class.

6 CS 350 – Software Design The Adapter Pattern – Chapter 7 So if you have an XXCircle class that handles all the functionality you require but has the following interface: displayIt(); displayIt(); fillIt(); fillIt(); undisplayIt(); undisplayIt();

7 CS 350 – Software Design The Adapter Pattern – Chapter 7 Create a Circle object that encapsulates XXCircle by making an XXCircle object an attribute of the Circle class.

8 CS 350 – Software Design The Adapter Pattern – Chapter 7 Some sample code to help you see how the adapter works: Class Circle extends Shape { … private XXCircle myXXCircle; private XXCircle myXXCircle; … public Circle () { public Circle () { myXXCircle = new XXCircle(); myXXCircle = new XXCircle(); } void public display() { void public display() { myXXCircle.displayIt(); myXXCircle.displayIt(); } …}

9 CS 350 – Software Design The Adapter Pattern – Chapter 7 Key Features of the Adapter Pattern: Intent: Match an existing object beyond your control to a particular interface Problem: A system has the right data and behavior, but the wrong interface. Solution: Provides a wrapper with the desired interface. P & C: Adapters Target (the class it derives from). Allows the Client to use the Adaptee as if it were a type of Target. Consequences: Allows for preexisting objects to fit into new class structures. Implementation: Contain the existing class in another class. Have the containing class match the required interface and call the methods of the contained class.

10 CS 350 – Software Design The Adapter Pattern – Chapter 7 Differences between Adapter and Façade These patterns are often confused. Here’s a simple chart to show you the differences: FaçadeAdapter Are there preexisting classes?YesYes Is there an interface we MUST design to?NoYes Does an object need to behave polymorphically?NoProbably Is a simpler interface needed?YesNo


Download ppt "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."

Similar presentations


Ads by Google