Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Laptop {
- model: string;
- price: number;
- setFeature(extras: string);
- getModel(): string;
- setPrice(cost: number);
- getPrice(): number;
- }
- interface Fearure {
- feature: string;
- price: number;
- }
- class ThinkPad implements Laptop {
- model: string = 'T460';
- price: number = 1200;
- setFeature(extras: string) {
- this.model += ', ' + extras;
- }
- getModel(): string {
- return this.model;
- }
- setPrice(cost: number): void {
- this.price += cost;
- }
- getPrice(): number {
- return this.price;
- }
- }
- // decorator #1
- class SSD implements Fearure {
- feature = 'SSD';
- price = 300;
- constructor(machine: Laptop) {
- machine.setPrice(this.price);
- machine.setFeature(this.feature);
- }
- }
- class Modem3G implements Fearure {
- feature = '3G Modem';
- price = 50;
- constructor(machine: Laptop) {
- machine.setPrice(this.price);
- machine.setFeature(this.feature);
- }
- }
- let laptop = new ThinkPad();
- new SSD(laptop);
- new Modem3G(laptop);
- alert('Laptop: ' + laptop.getModel());
- alert('$ ' + laptop.getPrice());
Advertisement
Add Comment
Please, Sign In to add comment