Algo-Sim › Design Patterns
Adapter: two legacy sensors, one interface
Easy · Design Patterns · step through it in Java, Python and C++ on your own input
Wrap a class whose interface doesn’t fit in an adapter that implements the interface the client expects and translates each call. The client and the legacy class both stay unchanged.
Problem
A dashboard reads temperatures through CelsiusSensor.celsius(). Two vendor classes you cannot change report differently: FahrenheitGauge.readFahrenheit() and KelvinProbe.kelvin(). Write adapters so the dashboard can show either. For each reading, return what show(sensor) returns: Fahrenheit converts as (F − 32) · 5 / 9 with integer division, Kelvin as K − 273.
Constraints
- 32 ≤ F ≤ 212, 273 ≤ K ≤ 373
- Neither the dashboard nor the vendor classes may change
- Each adapter call is O(1)
Edge cases to test
- A method the legacy class does not support
- Different units or formats between the two interfaces
- Errors that must be translated too
Follow-up questions
- Adapter or facade?
- An adapter makes one interface fit another; a facade gives a simpler interface over a whole subsystem.
- Class or object adapter?
- An object adapter wraps an instance (composition) and works with subclasses too; a class adapter inherits and needs multiple inheritance.
Solution
Java
interface CelsiusSensor { // what the dashboard expects
int celsius();
}
class FahrenheitGauge { // legacy vendor class we can't change
private final int f;
FahrenheitGauge(int f) { this.f = f; }
int readFahrenheit() { return f; }
}
class KelvinProbe { // another vendor, another API
private final int k;
KelvinProbe(int k) { this.k = k; }
int kelvin() { return k; }
}
class FahrenheitAdapter implements CelsiusSensor {
private final FahrenheitGauge gauge;
FahrenheitAdapter(FahrenheitGauge gauge) { this.gauge = gauge; }
public int celsius() { return (gauge.readFahrenheit() - 32) * 5 / 9; }
}
class KelvinAdapter implements CelsiusSensor {
private final KelvinProbe probe;
KelvinAdapter(KelvinProbe probe) { this.probe = probe; }
public int celsius() { return probe.kelvin() - 273; }
}
class Dashboard {
/** Client code: knows only CelsiusSensor. */
int show(CelsiusSensor sensor) {
return sensor.celsius();
}
}Python
class FahrenheitGauge: # legacy vendor class we can't change
def __init__(self, f):
self.f = f
def read_fahrenheit(self):
return self.f
class KelvinProbe: # another vendor, another API
def __init__(self, k):
self.k = k
def kelvin(self):
return self.k
class FahrenheitAdapter: # implements celsius()
def __init__(self, gauge):
self.gauge = gauge
def celsius(self):
return (self.gauge.read_fahrenheit() - 32) * 5 // 9
class KelvinAdapter: # implements celsius()
def __init__(self, probe):
self.probe = probe
def celsius(self):
return self.probe.kelvin() - 273
class Dashboard:
# Client code: knows only celsius().
def show(self, sensor):
return sensor.celsius()C++
#include <bits/stdc++.h>
using namespace std;
struct CelsiusSensor {
virtual int celsius() const = 0;
virtual ~CelsiusSensor() = default;
};
class FahrenheitGauge {
int f;
public:
explicit FahrenheitGauge(int f)
: f(f) {}
int readFahrenheit() const {
return f;
}
};
class KelvinProbe {
int k;
public:
explicit KelvinProbe(int k)
: k(k) {}
int kelvin() const {
return k;
}
};
class FahrenheitAdapter : public CelsiusSensor {
FahrenheitGauge gauge;
public:
explicit FahrenheitAdapter(FahrenheitGauge g)
: gauge(g) {}
int celsius() const override {
return (gauge.readFahrenheit() - 32) * 5 / 9;
}
};
class KelvinAdapter : public CelsiusSensor {
KelvinProbe probe;
public:
explicit KelvinAdapter(KelvinProbe p)
: probe(p) {}
int celsius() const override {
return probe.kelvin() - 273;
}
};
struct Dashboard {
int show(const CelsiusSensor &sensor) {
return sensor.celsius();
}
};
More Design Patterns problems
- Strategy: swappable discounts (Easy)
- Observer: publish and subscribe (Easy)
- State: a document workflow (Easy)
- Command: undo and redo (Medium)
- Chain of Responsibility: an ATM (Easy)
- Factory: create without naming classes (Easy)
- Singleton: one shared instance (Easy)
- Builder: a pizza step by step (Easy)
- Decorator: stack toppings at runtime (Easy)
- Proxy: a caching, lazy price service (Easy)
- Composite: folder sizes (Easy)