Skip to main content

Interface Segregation Principle

Zasada ta mówi o tym, żeby nie tworzyć interfejsów z metodami których nie będzie używała klasa. Interfejsy powinny być jak najmniejsze i bardzo konkretne.

Przykład złamania tej zasady:

public interface Animal {
    void eat();
    void breathe();
    void runing() throws ExecutionControl.NotImplementedException;
}
public class Butterfly implements Animal{
    @Override
    public void eat() {
        System.out.println("Pije nektar z kwiatka!");
    }
    @Override
    public void breathe() {
        System.out.println("Oddycham przy pomocy tchawki!");
    }
    @Override
    public void runing() throws ExecutionControl.NotImplementedException {
        throw new ExecutionControl.NotImplementedException("Brak implementacji!");
    }
}
public class Dog implements Animal{
    @Override
    public void eat() {
        System.out.println("Jem swoim pyszczkiem!");
    }
    @Override
    public void breathe() {
        System.out.println("Oddycham przy pomocy pyszczka!");
    }
    @Override
    public void runing() {
        System.out.println("Biegam na swoich czterech łapkach!");
    }
}
public class ShowAnimalInfo {
    public void showInfo(Animal[] animals) throws ExecutionControl.NotImplementedException {
        for (Animal animal : animals) {
            animal.eat();
            animal.breathe();
            animal.runing();
        }
    }
}

Mamy interface Animal który posiada trzy metody: eat, breathe oraz runing. Stworzyłam tu dwie klasy Dog oraz Butterfly które implementują ten interface. Już widać, że w klasie Butterfly będzie problem z metoda runing – motyle poruszają się przecież przy pomocy skrzydeł a nie biegają. Zatem musimy tu rzucić wyjątkiem a tym samym łamiemy zasadę bo metoda runing nie jest potrzebna w klasie Butterfly. Zobaczmy jak wygląda main:

public class Main {
    public static void main(String[] args){
        Animal[] animals = {new Dog(),new Butterfly()};
        ShowAnimalInfo showAnimalInfo = new ShowAnimalInfo();
        try {
            showAnimalInfo.showInfo(animals);
        } catch (ExecutionControl.NotImplementedException e) {
            e.printStackTrace();
        }
    }
}

Przykład zastosowania zasady:

public interface IBreathe {
    void breathe();
}
public interface IEat {
    void eat();
}
public interface IRuning {
    void runing();
}
public class Butterfly implements IBreathe, IEat {
    @Override
    public void breathe() {
        System.out.println("Oddycham przy pomocy tchawki!");
    }
    @Override
    public void eat() {
        System.out.println("Pije nektar z kwiatka!");
    }
}
public class Dog implements IBreathe, IEat, IRuning {
    @Override
    public void breathe() {
        System.out.println("Oddycham przy pomocy pyszczka!");
    }
    @Override
    public void eat() {
        System.out.println("Jem swoim pyszczkiem!");
    }
    @Override
    public void runing() {
        System.out.println("Biegam na swoich czterech łapkach!");
    }
}
public class ShowBreatheInfo {
    public void showBreathe(IBreathe[] breathe){
        for (IBreathe breath: breathe) {
            breath.breathe();
        }
    }
}
public class ShowEatInfo {
    public void showEat(IEat[] eat){
        for (IEat eats: eat) {
            eats.eat();
        }
    }
}
public class ShowRuningInfo {
        public void showRuning(IRuning[] runings){
            for (IRuning runing: runings) {
                runing.runing();
            }
        }
}

Nasz poprzedni interface zastąpiłam trzema prostymi interfejsami. Każdy posiada po jednej metodzie. Klasa Dog implementuje wszystkie trzy, natomiast klasa Butterfly tylko dwa (bez IRuning), dodatkowo napisałam trzy klasy które będą wyświetlały poszczególne informacje. Warto zauważyć, że dzięki temu nie musze już rzucać wyjątkiem. Zobaczmy jak wygląda teraz main:

public class Main {
    public static void main(String[] args){

        Dog dog = new Dog();
        Butterfly butterfly = new Butterfly();

        ShowBreatheInfo showBreatheInfo = new ShowBreatheInfo();
        ShowRuningInfo showRuningInfo = new ShowRuningInfo();
        ShowEatInfo showEatInfo = new ShowEatInfo();

        IBreathe[] iBreathes = {dog,butterfly};
        IEat[] iEat = {dog,butterfly};
        IRuning[] iRuning ={dog};

        showBreatheInfo.showBreathe(iBreathes);
        showEatInfo.showEat(iEat);
        showRuningInfo.showRuning(iRuning);

        }
}
pliki do pobrania:
SOLID Download