Class Object Adapter Patterns with a focus on
Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008
Adapter Pattern: What/when? l l l 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. ”
Adapter Pattern: Why? l l l 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.
Example: Interface adaptation l 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.
Standard interface: VGA l And the good people at Sony have even provided an interface to do so …
Problem: Wrong interface l But…of course, the TV requires a VGA interface, and my computer only has a DVI interface.
Solution: An adapter l Lo and behold, Apple machines all ship with a DVI to VGA adapter.
Class adapter deployed l The adapter allows incompatible interfaces to work together …
Adaptation complete l … so I can work on UML documents while laying on my couch. (Note: requires wireless keyboard)
Example UML l Adapter class uses inheritance to get its domain-specific and adaptee interface information. Being a subclass, it can override certain adaptee behaviors.
Example Pseudo. Code /* The Sony interface has a VGA output */ public interface Sony_VGA {. . . public String Connect. VGA(); . . . } /* class Apple wants a DVI input public class Apple { private String specification = "DVI"; public String connect. DVI() { 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 Connect. VGA { Connector connector = new Connector(); String output = connector. connect. DVI(); return output; } }
Another Basic Example l l l 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.
Object Adapter l l l Compositional instead of inheritancebased, which Go. F 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.
Summation l l l 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.
- Slides: 14