简体   繁体   中英

how to properly change an interface after it has been implemented by many classes? java

I have an

public interface DoIt {
    void dosomething (int i, double x);
    int dosomethingelse (String s);
}

some class that implement it is class_of_a etc... and i want to add an new method in the interface lets say diditwork(int x); How am i suppose to do that while avoiding the problems of recompiling or whatever problem that might be ? What would be the new hierarchy ?

Create a new interface and extend the old interface, something like:

interface DoIt2 extends DoIt
{
    // void doSomething(int i, double x);
    // int doSomethingelse(String s);
    void didItWork(int x);
}

you can extend your existing Interface like this:

interface DoItMore extends DoIt {  diditwork(int x);  }  

so you will have your old interface for low level Classes and your new Interface for high level. Then you have to change the used Interface in your High level class.

create another interface and let the given class implement both interfaces

eg

public interface DoIt { 
    void dosomething(int i, double x);
    int dosomethingelse(String s);
}

and new interface

public interface CheckIt {
    boolean diditwork(int x);
}

classes without necessity to check would implement only DoIt interface, classes with necessity to check would implement also CheckIt interface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM