Guest User

Untitled

a guest
Feb 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. interface Laptop {
  2.     model: string;
  3.     price: number;
  4.  
  5.     setFeature(extras: string);
  6.     getModel(): string;
  7.     setPrice(cost: number);
  8.     getPrice(): number;
  9. }
  10.  
  11. interface Fearure {
  12.     feature: string;
  13.     price: number;
  14. }
  15.  
  16. class ThinkPad implements Laptop {
  17.     model: string = 'T460';
  18.     price: number = 1200;
  19.  
  20.     setFeature(extras: string) {
  21.         this.model += ', ' + extras;
  22.     }
  23.  
  24.     getModel(): string {
  25.         return this.model;
  26.     }
  27.  
  28.     setPrice(cost: number): void {
  29.         this.price += cost;
  30.     }
  31.  
  32.     getPrice(): number {
  33.         return this.price;
  34.     }
  35. }
  36.  
  37. // decorator #1
  38. class SSD implements Fearure {
  39.     feature = 'SSD';
  40.     price = 300;
  41.  
  42.     constructor(machine: Laptop) {
  43.         machine.setPrice(this.price);
  44.         machine.setFeature(this.feature);
  45.     }
  46. }
  47.  
  48. class Modem3G implements Fearure {
  49.     feature = '3G Modem';
  50.     price = 50;
  51.  
  52.     constructor(machine: Laptop) {
  53.         machine.setPrice(this.price);
  54.         machine.setFeature(this.feature);
  55.     }
  56. }
  57.  
  58. let laptop = new ThinkPad();
  59.  
  60. new SSD(laptop);
  61. new Modem3G(laptop);
  62.  
  63. alert('Laptop: ' + laptop.getModel());
  64. alert('$ ' + laptop.getPrice());
Advertisement
Add Comment
Please, Sign In to add comment