Created
March 1, 2018 04:51
-
-
Save ythalorossy/45546217efd6858e261cb1b8c4710fff to your computer and use it in GitHub Desktop.
Decorator Pattern in Typescript
This file contains hidden or 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
abstract class Beverega { | |
abstract cost(): Number; | |
} | |
abstract class AddonDecorator extends Beverega { | |
public abstract cost(): Number; | |
} | |
class Expresso extends Beverega { | |
public cost(): Number { | |
return 1; | |
} | |
} | |
class Caramel extends AddonDecorator { | |
constructor(private beverage: Beverega) { | |
super(); | |
} | |
public cost(): Number { | |
return +this.beverage.cost() + 2; | |
} | |
} | |
class Chocolate extends AddonDecorator { | |
constructor(private beverage: Beverega) { | |
super(); | |
} | |
public cost(): Number { | |
return +this.beverage.cost() + 5; | |
} | |
} | |
// Annonymous Test Class | |
new class DecoratorPatternTests { | |
constructor() { | |
( () => console.log(new Chocolate(new Caramel(new Expresso())).cost()) )(); | |
} | |
}(); | |
// By Ythalo Rossy | |
// [email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment