Zachary W. Huang
Idea: adapt an object or class to an interface that it does not support. This can be done with multiple inheritance or composition. Essentially, we are changing the interface of an existing object (not decoupling interface and implementation like Bridge, enhancing an object like Decorator, nor defining a delegate for another object like Proxy).
Extras: Directly building interface adoption into a class (pluggable adapter) can improve reusability. Adapters can also be two-way in order to provide transparency.
class Target {
doThingA() {};
doThingB() {};
}
// does not fit the target interface
class Adaptee {
thingA: (x: number) => void;
thingB: (x: number) => void;
}
// "Wraps" the adaptee to fit the target interface
class ObjectAdapter implements Target {
_adaptee: Adaptee;
constructor(adaptee: Adaptee) {
this._adaptee = adaptee;
}
doThingA() {
this._adaptee.thingA(0);
}
doThingB() {
this._adaptee.thingA(0);
}
}
// Note: would use true multiple inheritance in a language like C++
class ClassAdapter extends Adaptee implements Target {
doThingA() {
this.thingA(0);
}
doThingB() {
this.thingA(0);
}
}