Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter...
-
Upload
rudolf-wilson -
Category
Documents
-
view
215 -
download
0
Transcript of Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter...
![Page 1: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/1.jpg)
Class & Object Adapter Patterns
(with a focus on Class Adapter)
Tim Gutowski
CSPP 51023, Winter 2008
![Page 2: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/2.jpg)
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.”
![Page 3: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/3.jpg)
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.
![Page 4: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/4.jpg)
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.
![Page 5: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/5.jpg)
Standard interface: VGA
And the good people at Sony have even provided an interface to do so …
![Page 6: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/6.jpg)
Problem: Wrong interface
But…of course, the TV requires a VGA interface, and my computer only has a DVI interface.
![Page 7: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/7.jpg)
Solution: An adapter
Lo and behold, Apple machines all ship with a DVI to VGA adapter.
![Page 8: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/8.jpg)
Class adapter deployed
The adapter allows incompatible interfaces to work together …
![Page 9: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/9.jpg)
Adaptation complete
… so I can work on UML documents while laying on my couch. (Note: requires wireless keyboard)
![Page 10: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/10.jpg)
Example UML
Adapter class uses inheritance to get its domain-specific and adaptee interface information. Being a subclass, it can override certain adaptee behaviors.
![Page 11: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/11.jpg)
Example PseudoCode/* The Sony interface has a VGA output */public interface Sony_VGA {
...public String ConnectVGA();...
}
/* class Apple wants a DVI inputpublic 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;}
}
![Page 12: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/12.jpg)
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.
![Page 13: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/13.jpg)
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.
![Page 14: Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.](https://reader036.fdocuments.in/reader036/viewer/2022070413/5697bfd81a28abf838caefb1/html5/thumbnails/14.jpg)
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.