-
-
Save AlexManSPB/a14fcacdb64b49cf80663d41ca449153 to your computer and use it in GitHub Desktop.
Revisions
-
DmitriiNazimov renamed this gist
Aug 30, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
DmitriiNazimov revised this gist
Aug 13, 2019 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,6 +8,11 @@ */ class interface_Coffee { constructor() { if (this.constructor.name === 'Creator') { throw new Error(`${this.constructor.name}: can not create instance of interface`); } } getCost() { throw new Error(`Не описан метод getCost() в классе ${this.constructor.name}`); } -
DmitriiNazimov revised this gist
Aug 12, 2019 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -20,10 +20,7 @@ class interface_Coffee { class StandartCoffee extends interface_Coffee { cost = 10; getCost() { return this.cost -
DmitriiNazimov revised this gist
Aug 11, 2019 . 1 changed file with 37 additions and 12 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,73 +7,98 @@ * */ class interface_Coffee { getCost() { throw new Error(`Не описан метод getCost() в классе ${this.constructor.name}`); } getDescription() { throw new Error(`Не описан метод getDescription() в классе ${this.constructor.name}`); } } class StandartCoffee extends interface_Coffee { constructor() { super(); this.cost = 10; } getCost() { return this.cost } getDescription() { return 'Standart coffee' } } class MilkCoffee extends interface_Coffee { constructor(coffee) { super(); this.coffee = coffee } getCost() { return this.coffee.getCost() + 2 } getDescription() { return this.coffee.getDescription() + ', milk' } } class WhipCoffee extends interface_Coffee { constructor(coffee) { super(); this.coffee = coffee } getCost() { return this.coffee.getCost() + 5 } getDescription() { return this.coffee.getDescription() + ', whip' } } class VanillaCoffee extends interface_Coffee { constructor(coffee) { super(); this.coffee = coffee } getCost() { return this.coffee.getCost() + 3 } getDescription() { return this.coffee.getDescription() + ', vanilla' } } class DiscountCoffee extends interface_Coffee { constructor(coffee, percent) { super(); this.coffee = coffee; this.percent = percent; } // намеренно провоцируем ошибку из интерфейса // getCost() { // return this.coffee.getCost() * ((100 - this.percent) / 100); // } getDescription() { return this.coffee.getDescription() + `, with discount ${this.percent}%` } } let someCoffee = new StandartCoffee() console.log(someCoffee.getCost())// 10 console.log(someCoffee.getDescription())// Простой кофе -
DmitriiNazimov created this gist
Aug 11, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,95 @@ /** * * ПАТТЕРН ДЕКОРАТОР (обертка) * Позволяет наделить обьект новыми возможностями не меняя первоначальный класс и не создавая дочерние классы * Принцип работы: декоратор помещает целевой обьект в обьект обертку, кот-й запускает базовое поведение обьекта, * а затем добавляет/отнимает что-то свое. * */ class StandartCoffee{ constructor() { this.cost = 10; } getCost() { return this.cost } getDescription() { return 'Standart coffee' } } class MilkCoffee { constructor(coffee) { this.coffee = coffee } getCost() { return this.coffee.getCost() + 2 } getDescription() { return this.coffee.getDescription() + ', milk' } } class WhipCoffee { constructor(coffee) { this.coffee = coffee } getCost() { return this.coffee.getCost() + 5 } getDescription() { return this.coffee.getDescription() + ', whip' } } class VanillaCoffee { constructor(coffee) { this.coffee = coffee } getCost() { return this.coffee.getCost() + 3 } getDescription() { return this.coffee.getDescription() + ', vanilla' } } class DiscountCoffee { constructor(coffee, percent) { this.coffee = coffee this.percent = percent; } getCost() { return this.coffee.getCost() * ((100 - this.percent) / 100); } getDescription() { return this.coffee.getDescription() + `, with discount ${this.percent}%` } } let someCoffee = new StandartCoffee() console.log(someCoffee.getCost())// 10 console.log(someCoffee.getDescription())// Простой кофе someCoffee = new MilkCoffee(someCoffee) console.log(someCoffee.getCost())// 12 console.log(someCoffee.getDescription())// Простой кофе, молоко someCoffee = new WhipCoffee(someCoffee) console.log(someCoffee.getCost())// 17 console.log(someCoffee.getDescription())// Простой кофе, молоко, сливки someCoffee = new VanillaCoffee(someCoffee) console.log(someCoffee.getCost())// 20 console.log(someCoffee.getDescription())// Простой кофе, молоко, сливки, ваниль someCoffee = new DiscountCoffee(someCoffee, 20) console.log(someCoffee.getCost())// 16 console.log(someCoffee.getDescription())// Простой кофе, молоко, сливки, ваниль, скидка 20%