Skip to content

Instantly share code, notes, and snippets.

@AlexManSPB
Forked from DmitriiNazimov/pattern-decorator.js
Created February 13, 2023 12:09
Show Gist options
  • Save AlexManSPB/a14fcacdb64b49cf80663d41ca449153 to your computer and use it in GitHub Desktop.
Save AlexManSPB/a14fcacdb64b49cf80663d41ca449153 to your computer and use it in GitHub Desktop.

Revisions

  1. @DmitriiNazimov DmitriiNazimov renamed this gist Aug 30, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @DmitriiNazimov DmitriiNazimov revised this gist Aug 13, 2019. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions script.js
    Original 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}`);
    }
  3. @DmitriiNazimov DmitriiNazimov revised this gist Aug 12, 2019. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions script.js
    Original file line number Diff line number Diff line change
    @@ -20,10 +20,7 @@ class interface_Coffee {


    class StandartCoffee extends interface_Coffee {
    constructor() {
    super();
    this.cost = 10;
    }
    cost = 10;

    getCost() {
    return this.cost
  4. @DmitriiNazimov DmitriiNazimov revised this gist Aug 11, 2019. 1 changed file with 37 additions and 12 deletions.
    49 changes: 37 additions & 12 deletions script.js
    Original file line number Diff line number Diff line change
    @@ -7,73 +7,98 @@
    *
    */

    class StandartCoffee{
    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() {
    this.cost = 10;
    super();
    this.cost = 10;
    }

    getCost() {
    return this.cost
    }

    getDescription() {
    return 'Standart coffee'
    }
    }




    class MilkCoffee {
    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 {
    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 {
    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 {
    class DiscountCoffee extends interface_Coffee {
    constructor(coffee, percent) {
    this.coffee = coffee
    super();
    this.coffee = coffee;
    this.percent = percent;
    }
    getCost() {
    return this.coffee.getCost() * ((100 - this.percent) / 100);
    }

    // намеренно провоцируем ошибку из интерфейса
    // 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())// Простой кофе
  5. @DmitriiNazimov DmitriiNazimov created this gist Aug 11, 2019.
    95 changes: 95 additions & 0 deletions script.js
    Original 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%