Skip to content

Instantly share code, notes, and snippets.

@DoRightt
Created June 8, 2020 09:30
Show Gist options
  • Save DoRightt/1906502829dfc657058399bf36d9649c to your computer and use it in GitHub Desktop.
Save DoRightt/1906502829dfc657058399bf36d9649c to your computer and use it in GitHub Desktop.
interface Duck {
quack(): void;
fly(): void;
}
interface Turkey {
gobble(): void;
fly(): void;
}
class MallardDuck implements Duck {
quack() {
console.log("quack quack");
}
fly() {
console.log("mallard duck flying");
}
}
class WildTurkey implements Turkey {
gobble() {
console.log("gobble gobble gobble");
}
fly() {
console.log("turkey fly few metres");
}
}
class TurkeyAdapter implements Duck {
turkey: Turkey;
constructor(turkey: Turkey) {
this.turkey = turkey;
}
quack() {
this.turkey.gobble();
}
fly() {
this.turkey.fly();
}
}
class DuckAdapter implements Turkey {
duck: Duck;
constructor(duck: Duck) {
this.duck = duck;
}
gobble() {
this.duck.quack();
}
fly() {
this.duck.fly();
}
}
const myTurkey = new WildTurkey();
const myDuck = new MallardDuck();
const smartTurkey = new TurkeyAdapter(myTurkey);
myTurkey.fly();
myTurkey.gobble();
myDuck.fly();
myDuck.quack();
smartTurkey.fly();
smartTurkey.quack();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment