Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /**
  2. *
  3. * ПАТТЕРН ДЕКОРАТОР (обертка)
  4. * Позволяет наделить обьект новыми возможностями не меняя первоначальный класс и не создавая дочерние классы
  5. * Принцип работы: декоратор помещает целевой обьект в обьект обертку, кот-й запускает базовое поведение обьекта,
  6. * а затем добавляет/отнимает что-то свое.
  7. *
  8. */
  9.  
  10. class interface_Coffee {
  11. constructor() {
  12. if (this.constructor.name === 'Creator') {
  13. throw new Error(`${this.constructor.name}: can not create instance of interface`);
  14. }
  15. }
  16. getCost() {
  17. throw new Error(`Не описан метод getCost() в классе ${this.constructor.name}`);
  18. }
  19.  
  20. getDescription() {
  21. throw new Error(`Не описан метод getDescription() в классе ${this.constructor.name}`);
  22. }
  23. }
  24.  
  25.  
  26.  
  27. class StandartCoffee extends interface_Coffee {
  28. cost = 10;
  29.  
  30. getCost() {
  31. return this.cost
  32. }
  33.  
  34. getDescription() {
  35. return 'Standart coffee'
  36. }
  37. }
  38.  
  39.  
  40.  
  41. class MilkCoffee extends interface_Coffee {
  42. constructor(coffee) {
  43. super();
  44. this.coffee = coffee
  45. }
  46.  
  47. getCost() {
  48. return this.coffee.getCost() + 2
  49. }
  50.  
  51. getDescription() {
  52. return this.coffee.getDescription() + ', milk'
  53. }
  54. }
  55.  
  56. class WhipCoffee extends interface_Coffee {
  57. constructor(coffee) {
  58. super();
  59. this.coffee = coffee
  60. }
  61.  
  62. getCost() {
  63. return this.coffee.getCost() + 5
  64. }
  65.  
  66. getDescription() {
  67. return this.coffee.getDescription() + ', whip'
  68. }
  69. }
  70.  
  71. class VanillaCoffee extends interface_Coffee {
  72. constructor(coffee) {
  73. super();
  74. this.coffee = coffee
  75. }
  76.  
  77. getCost() {
  78. return this.coffee.getCost() + 3
  79. }
  80.  
  81. getDescription() {
  82. return this.coffee.getDescription() + ', vanilla'
  83. }
  84. }
  85.  
  86. class DiscountCoffee extends interface_Coffee {
  87. constructor(coffee, percent) {
  88. super();
  89. this.coffee = coffee;
  90. this.percent = percent;
  91. }
  92.  
  93. // намеренно провоцируем ошибку из интерфейса
  94. // getCost() {
  95. // return this.coffee.getCost() * ((100 - this.percent) / 100);
  96. // }
  97. getDescription() {
  98. return this.coffee.getDescription() + `, with discount ${this.percent}%`
  99. }
  100. }
  101.  
  102.  
  103.  
  104. let someCoffee = new StandartCoffee()
  105. console.log(someCoffee.getCost())// 10
  106. console.log(someCoffee.getDescription())// Простой кофе
  107.  
  108. someCoffee = new MilkCoffee(someCoffee)
  109. console.log(someCoffee.getCost())// 12
  110. console.log(someCoffee.getDescription())// Простой кофе, молоко
  111.  
  112. someCoffee = new WhipCoffee(someCoffee)
  113. console.log(someCoffee.getCost())// 17
  114. console.log(someCoffee.getDescription())// Простой кофе, молоко, сливки
  115.  
  116. someCoffee = new VanillaCoffee(someCoffee)
  117. console.log(someCoffee.getCost())// 20
  118. console.log(someCoffee.getDescription())// Простой кофе, молоко, сливки, ваниль
  119.  
  120. someCoffee = new DiscountCoffee(someCoffee, 20)
  121. console.log(someCoffee.getCost())// 16
  122. console.log(someCoffee.getDescription())// Простой кофе, молоко, сливки, ваниль, скидка 20%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement