Convert the interface of a class into another interface clients expect. An adapter lets class work together that couldn't otherwise because of incompatible interfaces.
TypeScript Adapter Pattern Example
export class Adaptee {
public method(): void {
console.log("`method` of Adaptee is being called");
}
}
export interface Target {
call(): void;
}
export class Adapter implements Target {
public call(): void {
console.log("Adapter's `call` method is being called");
var adaptee: Adaptee = new Adaptee();
adaptee.method();
}
}
export class Demo {
show(): void {
var adapter: Adapter = new Adapter();
adapter.call();
}
}
let demo = new Demo();
demo.show();
Run and Output
PS C:\design_patterns_in_typescript-master\adapter> tsc .\adapter.ts
PS C:\design_patterns_in_typescript-master\adapter> node .\adapter.js
Adapter's `call` method is being called
`method` of Adaptee is being called
Check out nice example at https://gist.github.com/joelpalmer/2baa8d3f2312be80fc074eeeb53b1ce7.
Comments
Post a Comment