lundi 24 juillet 2023

Interface vs Abstract class in Builder design pattern

In the context of the builder pattern, What is the most correct way to make the builder template?

With the interface is no able to specify the access permissions (public, protected and private), that means there may be a mismatch in the classes that implement it.

On the other hand, I think it is unnecessary to use an abstract class when all methods are going to be overwritten.

Here´re the exmaples:

Interface

// Builder
interface PizzaBuilder {
    pizza: Pizza;
    resert(): void;

    setTomamto(): this;
    setParmesano(): this;
    setPepper(): this;

    build(): Pizza;
}

🆚

Abstract class

// Builder
abstract class PizzaBuilder {
    protected pizza: Pizza = new Pizza
  
    protected abstract reset(): void;
    public abstract setTomamto(): this;
    public abstract setParmesano(): this;
    public abstract setPepper(): this;

    public abstract build(): Pizza;
}

Aucun commentaire:

Enregistrer un commentaire