Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.

Similar presentations


Presentation on theme: "Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008."— Presentation transcript:

1 Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008

2 Adapter Pattern: What/when? The class adapter pattern allows two unrelated interfaces to work together. If a client would like to use an existing class, but does not have a compatible interface, they can still utilize it by using a class adapter. Because the adapter often “wraps” around an existing interface, this adapter is also known as a “wrapper.”

3 Adapter Pattern: Why? You would use a class adapter if a given class has a proven method you would like to call with one of your objects … But for whatever reason, your client’s interface doesn’t match the target’s. This allows developers to re-use code and avoids the scenario where a perfectly good class has to be modified simply to fit your class’ interface.

4 Example: Interface adaptation My problem: I would like to be able to display my computer screen onto my big-screen TV…allowing me to more comfortably work on HW from my couch.

5 Standard interface: VGA And the good people at Sony have even provided an interface to do so …

6 Problem: Wrong interface But…of course, the TV requires a VGA interface, and my computer only has a DVI interface.

7 Solution: An adapter Lo and behold, Apple machines all ship with a DVI to VGA adapter.

8 Class adapter deployed The adapter allows incompatible interfaces to work together …

9 Adaptation complete … so I can work on UML documents while laying on my couch. (Note: requires wireless keyboard)

10 Example UML Adapter class uses inheritance to get its domain-specific and adaptee interface information. Being a subclass, it can override certain adaptee behaviors.

11 Example PseudoCode /* The Sony interface has a VGA output */ public interface Sony_VGA {... public String ConnectVGA();... } /* class Apple wants a DVI input public class Apple { private String specification = "DVI"; public String connectDVI() { return specification; } /* Finally, there will be an adapter class called connector. This will inherit the Sony_VGA and give output for Apple. */ public class connector implements Sony_VGA { public String ConnectVGA { Connector connector = new Connector(); String output = connector.connectDVI(); return output; }

12 Another Basic Example A class that performs a set of regex matching operations which you’d like to apply to some dictionaries in a separate object. Problem: Class interface requires individual string inputs. Adapter converts your dict to a series of strings and utilizes adaptee.

13 Object Adapter Compositional instead of inheritance- based, which GoF book prefers. In this case, adapter wraps an actual instance of the adaptee and calls operations on it. Allows a single adapter to work with multiple adaptees, which isn’t the case with class adapter.

14 Summation Use the Adapter pattern when you want to use an existing class but its interface doesn’t match what you require. Use it to create a reusable converter class that is can deal with unforeseen classes/incompatible interfaces. Work with multiple adaptees (subclasses of parent adaptee) with object adapter.


Download ppt "Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008."

Similar presentations


Ads by Google