vendredi 6 janvier 2017

Interface Segregation Principle in Java

I'm learning SOLID principles with Java and I'm trying to implement two classes with this. My problem is about ISP. I have some methods that is present in one class but not in the other and I also have to refer both classes with the same interface. This is the first class:

public final class Complex implements Number {
    @Override
    public String polarForm() {
        //This class needs to implement this method
    }

    @Override
    public String rectangularForm() {
        //This class needs to implement this method
    }
}

Here is the second one:

public final class Real implements Number {
    @Override
    public String polarForm() {
        //This class does not need this method!
    }

    @Override
    public String rectangularForm() {
        //This class does not need this method!
    }
}

Finally I have to refer to the classes something like this:

public static void main(String[] args) {
    Number c = new Complex();
    Number r = new Real();
    Number n = c.add(r);
    System.out.println(c.polarForm());
    System.out.println(n);
}

How can I refer to both classes with the same interface without implement unnecessary methods?

Aucun commentaire:

Enregistrer un commentaire