lundi 11 juillet 2022

How to implement abstract factory in js?

I want to implement very simple abstract factory pattern but I am facing this error. Also am I doing something wrong with my code? How can I improve this to make it work properly?

Must call super constructor in derived class before accessing 'this' or returning from derived constructor"

class Pizza {
    constructor(name,size,type) {
        this.name = name;
        this.size = size;
        this.type = type;
    }

    prepare() {
        console.log("prepare");
    }
    bake() {
        console.log("bake");
    }
    deliver() {
        console.log("deliver");
    }
}

class MeatPizza extends Pizza {
    constructor() {
        super("Chicago style pizza", "Large", "Meat")
    }
}

class VegePizza extends Pizza {
    constructor() {
        super("Only natural", "Large", "Vegetarian")
    }
}

class MakePizza {
    createPizza(type) {
        switch (type) {
            case "meat":
                return new MeatPizza()
            case "vege":
                return new VegePizza()
            default:
                throw new Error("Something went wrong...");
        }
    }
}

class MakeMeatPizza extends MakePizza {
    constructor() {
        super.createPizza("meat");
    }
}

class MakeVegePizza extends MakePizza {
    constructor() {
        super.createPizza("vege");
    }
}

class OrderPizza {

}

const test = new MakeVegePizza();
console.log(test)

Aucun commentaire:

Enregistrer un commentaire