dimanche 3 octobre 2021

Adding function to interface decorator pattern

Hello,

I'm trying to wrap my head around the decorator pattern, and I'm not sure about something :

Is considered a bad practice or a misuse of the pattern to add a function to the interface when I want to add a new functionality ?

For example if I have a Car interface, and I want to make it amphibious with a decoration, is it allowed to add a getAquaticSpeed() function to the interface only for the purpose of adding this functionality ?

Thanks in advance !

Code example :

interface Car{
   public float getSpeed();
}

public class CarFrame implements Car{
    public float getSpeed(){return 0;}
}

public abstract CarDecorator implements Car{
    private Car delegate;

    public CarDecorator(Car delegate){
        this.delegate = delegate
    }

    public float getSpeed(){return delegate.getSpeed();}
}

If i want to add an aquatic speed, colors etc. to my Car, is it allowed to add new function to my main interface ?

Like so :

interface Car{
   public float getSpeed();
   public float getAquaticSpeed();
   public String getColor();
}

Aucun commentaire:

Enregistrer un commentaire