Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to minimize number of classes to modify when interface is changed

There is an interface Accountable which has 2 methods. There are 9 classes which implement the Accountable interface.

public interface Accountable{
    boolean isAccountable();
    float getWorth();
}

We have got a new requirement as follows: Two more methods declarations to be added to the interface. But we need to minimize the effect on the existing classes. I was told that we can use Adaptors to resolve the issue. But I am not sure how to do it. Could anyone please help me solve the issue?

like image 643
Naga Pradeep Avatar asked Dec 06 '22 17:12

Naga Pradeep


1 Answers

Using java 8 you can declare default implementation just in interface:

public interface Accountable{
  boolean isAccountable();
  float getWorth();
  default int someMethod() {return 0;}
}

If you use old java the only way is to add an abstract class as a middlware, but since java don't support multiple inheritance it could be painful.

like image 87
Konstantin Labun Avatar answered Dec 09 '22 15:12

Konstantin Labun