Naga Pradeep
Naga Pradeep

Reputation: 202

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?

Upvotes: 4

Views: 299

Answers (4)

Marcelo Ferreira
Marcelo Ferreira

Reputation: 466

I love this, most people do not know the power of interfaces and its purpose, interfaces is the best thing for the development already invented solution for your problem used design patterns

Your Interface transformed

public interface Accountable <E> { 

 //method commun all class  
 public boolean isAccountable();

 public float getWorth();

 //method commun all class     
 public int someMethod();

 //method commun all class
 public E getSameCommun();
}

That a generic implementation

public abstract class Generic<T extends Serializable> implements<T>{

    private boolean accountable;

    @Override
    public boolean isAccountable(){
       return 
    }

    @Override
    public int someMethod(){
        System.out.println("do samethink");
    }

    @Override
    public E getSameCommun(){
      System.out.println("do samethink");
    }

}

use in your class

public class Class1 extends Generic<Class1> implements Accountable<Class1>{

   //that moment you bring 3 method of the Generic
   // you need implement specific method of class
   @Override
   public float getWorth(){
      //so something
    }
}

//other class
public class Class2 extends Generic<Class2> implements Accountable<Class2>{

   //that moment you bring 3 method of the Generic
   // you need implement specific method of class
   @Override
   public float getWorth(){
      //so something
    }
}

//other class
public class Class3 extends Generic<Class3> implements Accountable<Class3>{

   //that moment you bring 3 method of the Generic
   // you need implement specific method of class
   @Override
   public float getWorth(){
      //so something
    }

   //that case you need overred same method commun
   @Override
   public E getSameCommun(){
          System.out.println("do other samethink");
   }
}


//other class, you need additional functionality for method
public class Class3 extends Generic<Class3> implements Accountable<Class3>{

   //that moment you bring 3 method of the Generic
   // you need implement specific method of class
   @Override
   public float getWorth(){
      //so something
    }

   //that case you need overred same method commun
   @Override
   public E getSameCommun(){
          super.getSameCommun();
          System.out.println("additional functionality");
         return e;
   }
}

that same objective of the "Design Patterns"

Upvotes: 1

Fildor
Fildor

Reputation: 16094

I guess the goal is to learn about Adapters. In order to use an Adapter you firstly need to create one:

public class AccountableAdapter implements Accountable{
    @Override
    boolean isAccountable(){ return false; }
    @Override
    float getWorth(){ return (float)0.0 }
}

Your classes then extend that Adapter. You see, they automatically will implement all methods of the interface inheriting the default-implementations from the Adapter.

If later the interface is added a method, you will have to add an implementation in the Adapter, only. All extending classes won't break.

Of course until here you only have Default-Implementations. Now you will override all methods that need to have a special implementation in your concrete classes.

Personally I find this is a rather bad example to learn about Adapters. A better and "real-life" example would be event handlers. For example MouseInputAdapter. It supplies you with a variety of possible callbacks from mouse-events. By using an Adapter, you can resort to implement only the Listener you really need and don't have to implement the whole interface.

Upvotes: 2

Konstantin Labun
Konstantin Labun

Reputation: 3908

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.

Upvotes: 8

Vikas Sharma
Vikas Sharma

Reputation: 755

Avoid adding a method to the interface by instead creating a new interface which inherits from that first interface.Now your new class will implement new interface.So there is minimal change.

Upvotes: 4

Related Questions