-1

I am creating a small project that implements an interface in Java. I am not allowed to modify the interface, which means I can't change the functions in my class that implements the interface.

However, one of the methods in the class (from the interface) requires a different behaviour in some cases. How do I implement this, in a way that it allows me to adapt the behaviour and make changes to the method without changing the interface?

  • I don't think someone can answer this without knowing any details about the interface, the method, what kind of different behaviour, and what "some cases" means. But beware, coding help is off-topic on this site, so I guess it would be better to ask a heavily improved version of this question on Stackoverflow. – Doc Brown Jan 4 at 13:23
  • I want to learn how to tackle this problem in general, so don't want to focus on a specific challenge. I'm sure it's possible to answer the question without knowing the code behind it. – DON TOLIVER Jan 4 at 13:25
  • If the method signature in the interface still fits to this new behaviour and does not need a change, the answer is trivial, just change the implementation. If the new behaviour requires a different signature, this will be not possible. So what is your case, and what is your question? – Doc Brown Jan 4 at 13:31
  • Okay, so I have a method in the class that prints lines of strings with two different values ("hey-hi"). I need to implement a way so that the same method can allow the strings to be printed with the two different values to be printed in the reverse order ("hi-hey"). How do I do this without changing the interface? – DON TOLIVER Jan 4 at 13:35
  • I can't tell you if this is possible as long as I don't know the interface. Also, what does "allow the strings to be printed" mean - how do you want this new behaviour to be controlled? By some additional boolean parameter in the interface? Then the interface has to be changed. By an external configuration file? Then the interface does probably not have to be changed. Or do you want the new behaviour just unconditionally replace the old? Then there is obviously no need to change the interface. – Doc Brown Jan 4 at 13:36
1

Your question is very general. So will be my answer.

The easiest way would be to write the implemented method to cover the special cases. It it's complex, you may delegate the execution to more specialised function (that can be outside the interface). But if it were that simple you wouldn't probably ask.

Another way could be :

  • to implement your method using a strategy. This allows you to handle special cases by changing strategy dynamically or by injecting it into the constructor.
  • to implement your class according to the state pattern. This is especially suitable if the "special cases" could apply to all the objects of your class, depending dynamically on the state of the object.

If the special conditions are related to special kind of objects (an not all objects), and if you are allowed to subclasses your implementing class, you may also consider:

  • to simply subclass your class and implement the method differently.
  • to implement your method as a template method, doing its job following a skeleton but using some private methods that could be specialized through inheritance.

As you see, there are plenty of ways of doing it. If you would provide more information, we could advise more specifically.

  • I have to design a solution that simply makes it easy to allow for future changes in the way the output is formatted in the method that the class implements from the interface (so I 'd have the same implementation on all the different behaviours for other modules, except this one). If I were to go with the strategy/state pattern, I would have a lot of repetition (if I understand correctly). It seems like introducing a flag on the constructor and showing the different outputs at runtime based on the flag is more suitable for managing future change on this single method? – DON TOLIVER Jan 4 at 14:22
  • 1
    @DONTOLIVER indeed, this is what I meant with the easiest way. Strategy could help if the formatting would be more complex and could evolve. But strategy for formatting seems overkill. State would be completely inappropriate for this purpose. – Christophe Jan 4 at 14:39
0

The method that implements the interface will do exactly what the code that you put into the method does. So if you want the instances of your class to do something different when the interface method is called, you just do it.

Some interface methods are explicitely defined to work that way. For example, in iOS a UIViewController has an interface method viewWillAppear: which gets called just before a view controlled by the view controller appears on the screen, and the contract says that you as the developer take whatever actions you think are appropriate to take at that time.

Other interface methods tell you exactly what you should do (apart from the implementation of that interface depending on the details of the class in question). In that situation, if your implementation does something different, that will be unexpected and your application may misbehave.

  • Sorry, I am a little confused about your answer. Do you mean to implement a different class that extends our existing one (which implements the interface), and overrides the functionality of the method we want to change in order to allow for the different behaviour required? – DON TOLIVER Jan 4 at 13:32
  • @DONTOLIVER You write the code. You do it whatever way you like. If "our class" implements an interface, but no the way you like, it's obvious that you would change the implementation to do what you want it to do, right? If "our class" inheriits an implementation of an interface, then "our class" is a subclass already, so why would you subclass your subclass? – gnasher729 Jan 5 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.