Created
June 6, 2020 16:56
-
-
Save DoRightt/63c2370f61d4f77c1bf651fae590a67f 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 QuackBehavior { | |
quack(): void; | |
} | |
interface FlyBehavior { | |
fly(): void; | |
} | |
class Quack implements QuackBehavior { | |
quack(): void { | |
console.log("quack-quack"); | |
} | |
} | |
class muteQuack implements QuackBehavior { | |
quack(): void { | |
console.log("..."); | |
} | |
} | |
class FakeQuack implements QuackBehavior { | |
quack(): void { | |
console.log("quack like a fake"); | |
} | |
} | |
class FlyWithWings implements FlyBehavior { | |
fly(): void { | |
console.log("shoo-shoo fly"); | |
} | |
} | |
class FlyRocketPowered implements FlyBehavior { | |
fly(): void { | |
console.log("Im fly to the moon, like a rocket!"); | |
} | |
} | |
class FlyNoWay implements FlyBehavior { | |
fly(): void { | |
console.log("sry, but i can't fly"); | |
} | |
} | |
abstract class HuntingLures implements QuackBehavior { | |
quackBehavior: QuackBehavior; | |
performQuack() { | |
this.quackBehavior.quack(); | |
} | |
} | |
class DuckQuacker extends HuntingLures { | |
quackBehavior = new FakeQuack(); | |
} | |
abstract class Duck implements QuackBehavior, FlyBehavior { | |
quackBehavior: QuackBehavior; | |
flyBehavior: FlyBehavior; | |
setFlyBehavior(fb: FlyBehavior) { | |
this.flyBehavior = fb; | |
} | |
setQuackBehavior(qb: QuackBehavior) { | |
this.quackBehavior = qb; | |
} | |
display(): void { | |
console.log("duck was displayed"); | |
} | |
performQuack() { | |
this.quackBehavior.quack(); | |
} | |
performFly() { | |
this.flyBehavior.fly(); | |
} | |
} | |
class MollardDuck extends Duck { | |
quackBehavior = new Quack(); | |
flyBehavior = new FlyWithWings(); | |
} | |
class WoodenDuck extends Duck { | |
quackBehavior = new muteQuack(); | |
flyBehavior = new FlyNoWay(); | |
} | |
class ModelDuck extends Duck { | |
flyBehavior = new FlyNoWay(); | |
quackBehavior = new Quack(); | |
} | |
const duffy: Duck = new MollardDuck(); | |
const woodenDuck: Duck = new WoodenDuck(); | |
const model: Duck = new ModelDuck(); | |
const duckDecoy: HuntingLures = new DuckQuacker(); | |
console.log("mollard duck perfomance:"); | |
duffy.display(); | |
duffy.performQuack(); | |
duffy.performFly(); | |
console.log("wooden duck now:"); | |
woodenDuck.performQuack(); | |
woodenDuck.performFly(); | |
console.log("Now is model duck perfomance:"); | |
model.performFly(); | |
model.setFlyBehavior(new FlyRocketPowered()); | |
model.performFly(); | |
duckDecoy.performQuack(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment