Professional Documents
Culture Documents
stefoxp/head-first-design-patterns Public
master
head-first-design-patterns / being_adaptive.md
stefoxp c.7 Being adaptive completed
1 contributor
Being Adaptive
The OO adapters take an interface and adapt it to one that a client is expecting. The
adapter as the middleman by receiving request from the client and converting them
into requests that make sense on the vendor classes.
Implementation Code Duck adapter
The Adapter Pattern explained
the client makes a request to the adapter by calling a method on it using the
target interface
the adapter translates that request into one or more calls on the adaptee using
the adaptee interface
the client receives the results of the call and never knows there is an adapter
doing the translation
Note that the Client ad Adapter are decoupled: neither knows about the other.
Definition of the Adapter Pattern
The Adapter Pattern - class diagram
/* object adapter */
https://github.com/stefoxp/head-first-design-patterns/blob/master/being_adaptive.md 1/7
06/05/2023, 13:40 head-first-design-patterns/being_adaptive.md at master · stefoxp/head-first-design-patterns · GitHub
interface Target {
request()
}
class Target {
request()
}
class Adaptee {
specificRequest()
}
// the Adapter now subclasses the Adaptee and the Target classes
class Adapter: Target, Adapteee {
request()
}
https://github.com/stefoxp/head-first-design-patterns/blob/master/being_adaptive.md 2/7
06/05/2023, 13:40 head-first-design-patterns/being_adaptive.md at master · stefoxp/head-first-design-patterns · GitHub
Today
We are often faced with legacy code that exposes the Enumerator interface, yet we'd
like for our new code to only use Iterators. It looks like we need to build an adapter.
interface Iterator = Target interface
interface Enumeration = Adaptee interface
How to map the remove() method in Iterator from the Enumeration?
https://github.com/stefoxp/head-first-design-patterns/blob/master/being_adaptive.md 3/7
06/05/2023, 13:40 head-first-design-patterns/being_adaptive.md at master · stefoxp/head-first-design-patterns · GitHub
/* class diagram */
// we're making the Enumerations in your old code look like Iterators for
// your code still gets to use Iterators, even if there's really an Enumer
interface Iterator {
hasNext()
next()
remove()
}
We know Enumeration just doesn't support remove. It's a "read only" interface.
There's no way to implement a fully functioning remove() method on the adapter. The
best we can do is throw a runtime exception. (luckily remove supports an
UnsupportedOperationException)
/* sample */
// happy client whose job just became easier because of the facade
class Client {}
https://github.com/stefoxp/head-first-design-patterns/blob/master/being_adaptive.md 5/7
06/05/2023, 13:40 head-first-design-patterns/being_adaptive.md at master · stefoxp/head-first-design-patterns · GitHub
Take any object, from any method in that object, the principle tells us that we should
only invoke methods that belong to:
the object itself
objects passed in as a parameter to the method
any object the method creates or instantiates
any components of the object
For example:
// without the principle
public float getTemp() {
// we get the thermometer object from the station and then call the ge
Thermometer thermometer = station.getThermometer();
return thermometer.getTemperature();
}
Car class sample demonstrates all the ways you can call methods and still adhere to
the Principle:
public class Car {
// a component of this class. We can call its methods
Engine engine;
public Car() {
// initialize engine, etc.
}
if (authorized) {
// you can call a method on a component of the object
engine.start();
// you can call a local method within the object
https://github.com/stefoxp/head-first-design-patterns/blob/master/being_adaptive.md 6/7
06/05/2023, 13:40 head-first-design-patterns/being_adaptive.md at master · stefoxp/head-first-design-patterns · GitHub
updateDashboardDisplay();
// you can call a method on an object you create or instantiat
doors.lock();
}
https://github.com/stefoxp/head-first-design-patterns/blob/master/being_adaptive.md 7/7