Java – What design pattern for changing interface

design-patternsjava

Sorry guys, I should have clarified the fact that the modified method is abstract in Father. Then if the method signature changes, all its implementations in SonX shall also change(at least the signature, though the implementation may stay still).

Now the question should be asked like following:
Say I have the abstract class Father, and three(or more) of his children class Son1, Son2 and Son3. Now I have to change one method, in other words, add an argument to one abstract method of Father. However, I will have to change all the implemented methods(the modified abstract method in Father) in SonX, which costs too much. The change to the signature of the method in SonX is essential cause the clients are interacting with SonX and the clients shall invoke the new version of the method.

So my question is: is there any design pattern suitable for such a situation in which the interfaces will change?

Thanks.

Best Answer

The fact that you need to make a change to the interface of a superclass but do not also have a similar need to change its subclasses suggests to me that you have a design error of some kind. Your subclasses would appear, at least in some cases, not to be substitutable for their base class, otherwise they would need to change too.

It may be that your inheritance structure is wrong in that there is an as yet unidentified base class that all of Father and SonX should inherit instead of being direct descendants. Then Father alone could implement the method that needs to change. If there is behavior in common it might belong in the base class, or it may be better if SonX have a Father object they can delegate to.