Skip to content

Instantly share code, notes, and snippets.

@nchDev-js
Created February 20, 2021 19:00
Show Gist options
  • Select an option

  • Save nchDev-js/c8b6bba0bd6564d61778312cefc7a9cd to your computer and use it in GitHub Desktop.

Select an option

Save nchDev-js/c8b6bba0bd6564d61778312cefc7a9cd to your computer and use it in GitHub Desktop.
Design Patterns / STRUCTURAL
class Engine2 {
simpleInterface() {
console.log("Engine 2.0 - tr-tr-tr");
}
}
class EngineV8 {
complecatedInterface() {
console.log("Engine V8! - wroom wroom!");
}
}
class EngineV8Adapter {
constructor(engine) {
this.engine = engine;
}
simpleInterface() {
this.engine.complecatedInterface();
}
}
class Auto {
startEngine(engine) {
engine.simpleInterface();
}
}
class Model {
constructor(color) {
this.color = color;
}
}
class Color {
constructor(type) {
this.type = type;
}
get() {
return this.type;
}
}
class BlackColor extends Color {
constructor() {
super("dark-black");
}
}
class SilbrigColor extends Color {
constructor() {
super("Silbermetallic");
}
}
class Audi extends Model {
constructor(color) {
super(color);
}
paint() {
return `Auto: Audi, Color: ${this.color.get()}`;
}
}
class Bmw extends Model {
constructor(color) {
super(color);
}
paint() {
return `Auto: Bmw, Color: ${this.color.get()}`;
}
}
class Equipment {
getPrice() {
return this.price || 0;
}
getName() {
return this.name;
}
setName(name) {
this.name = name;
}
setPrice(price) {
this.price = price;
}
}
class Engine extends Equipment {
constructor() {
super();
this.setName("Engine");
this.setPrice(800);
}
}
class Body extends Equipment {
constructor() {
super();
this.setName("Body");
this.setPrice(3000);
}
}
class Tools extends Equipment {
constructor() {
super();
this.setName("Tools");
this.setPrice(4000);
}
}
class Composite extends Equipment {
constructor() {
super();
this.equipments = [];
}
add(equipment) {
this.equipments.push(equipment);
}
getPrice() {
return this.equipments
.map((equipment) => equipment.getPrice())
.reduce((a, b) => a + b);
}
}
class Car extends Composite {
constructor() {
super();
this.setName("Audi");
}
}
class Car {
constructor() {
this.price = 10000;
this.model = "Car";
}
getPrice() {
return this.price;
}
getDescription() {
return this.model;
}
}
class Tesla extends Car {
constructor() {
super();
this.price = 25000;
this.model = "Tesla";
}
}
class Autopilot {
constructor(car) {
this.car = car;
}
getPrice() {
return this.car.getPrice() + 5000;
}
getDescription() {
return `${this.car.getDescription()} with autopilot`;
}
}
class Parktronic {
constructor(car) {
this.car = car;
}
getPrice() {
return this.car.getPrice() + 3000;
}
getDescription() {
return `${this.car.getDescription()} with parktronic`;
}
}
class Сonveyor {
setBody() {
console.log("Body set!");
}
getEngine() {
console.log("Dismantle Engine!");
}
setEngine() {
console.log("Engine set!");
}
setInterior() {
console.log("Exterior added!");
}
changeInterior() {
console.log("Update interior!");
}
setExterior() {
console.log("Added interior!");
}
setWheels() {
console.log("Wheels!");
}
addElectronics() {
console.log("Added electronics!");
}
paint() {
console.log("Car painted!");
}
}
class СonveyorFacade {
constructor(car) {
this.car = car;
}
assembleCar() {
this.car.setBody();
this.car.setEngine();
this.car.setInterior();
this.car.setExterior();
this.car.setWheels();
this.car.addElectronics();
this.car.paint();
}
}
class Auto {
constructor(model) {
this.model = model;
}
}
class AutoFactory {
constructor(name) {
this.models = {};
}
create(name) {
let model = this.models[name];
if (model) return model;
this.models[name] = new Auto(name);
return this.models[name];
}
}
class CarAccess {
open() {
console.log("Opening car door");
}
close() {
console.log("Closing the car door");
}
}
class SecuritySystem {
constructor(door) {
this.door = door;
}
open(password) {
if (this.authenticate(password)) {
this.door.open();
} else {
console.log("Access denied!");
}
}
authenticate(password) {
return password === "Ilon";
}
close() {
this.door.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment