OiO.lk Blog java Design pattern for Interface with implementing classes no longer requiring all methods
java

Design pattern for Interface with implementing classes no longer requiring all methods


I have an interface AService which has 2 methods, and this interface is implemented by two different classes. One of the methods is directly called from an another service by calling on an AService instance (the actual implementation is given by a factory method).

interface AService {
method1
method2
}
class Impl1 {
//both methods implemented
}
class Impl2 {
//both methods implemented
}
class AnotherService {
void process() {
AService service = factory.getService(...);
if (some logical condition) {
service.method1();
}
}
}

The situation changed over time and now one of the classes should not need method1 any more, whereas the other class still needs it. At the moment, I kept the implementation body empty and method1 declared in the class no longer needing it, as it is still called from the service depending on the result of the factory method.
What is the cleanest way to handle this? Would it be better to introduce an instanceof check in the service to check that the instance is of that particular type, and then cast it and call the method, and remove method1 from the common interface?



You need to sign in to view this answers

Exit mobile version