Skip to content

Instantly share code, notes, and snippets.

@andrewborisov
Created August 5, 2020 04:31
Show Gist options
  • Save andrewborisov/2f0c04c8d8f31094abbc806e3234cedb to your computer and use it in GitHub Desktop.
Save andrewborisov/2f0c04c8d8f31094abbc806e3234cedb to your computer and use it in GitHub Desktop.
Гист для статьи "Еще раз о паттернах проектирования в Javascript es6 (часть 2)"
class Composite {
constructor(compositeName) {
this.children = [];
this.compositeName = compositeName;
}
add(child) {
this.children.push(child);
}
getPrice() {
let price = 0;
this.children.forEach((child) => price += child.getPrice());
return price;
}
}
class Leaf {
constructor(leafName, leafPrice) {
this.leafName = leafName;
this.leafPrice = leafPrice;
}
getPrice() {
return this.leafPrice;
}
}
const testing = () => {
const package = new Composite('Автомобиль');
const sportPackage = new Composite('Спортивный пакет');
const sportSuspension = new Leaf('Спортивная подвеска', 50000);
const sportTransmission = new Leaf('Спортивная коробка передач', 100000);
const sportExterior = new Composite('Спортивный экстерьер');
const sportLines = new Leaf('Спортивные полосы на кузове', 10000);
package.add(sportPackage);
sportPackage.add(sportSuspension);
sportPackage.add(sportTransmission);
package.add(sportExterior);
sportExterior.add(sportLines);
console.log(package.getPrice());
};
testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment