Created
June 8, 2020 09:30
-
-
Save DoRightt/1906502829dfc657058399bf36d9649c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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