0

I have a factory for an interface with different implementations. But one implementation needs an extra method which the others don't need. How do I solve this without implementing "not implemented exception" methods in other implementations?

2
  • 2
    Could you give a more concrete example? Why does one implementation need an extra method? How are the things that take objects that implement this interface supposed to know they've received the one with the extra method without putting implementation details everywhere? Commented Mar 28, 2017 at 7:06
  • If you give a concrete example you will have a more accurate answer. For instance if you take the close() method in some stream interface, in-memory implementation may have an implementation that just do nothing. In other cases that may not be desirable. Commented Mar 28, 2017 at 7:35

2 Answers 2

3

Do the consumers of the inteface need to call the method that is only required for one implementation?

If the consumer does not need to call the method, it is an implementation detail. Therefore, it is not of interest to the consumers of the interface, as they even should/must not call it. Just add the method as a private method of the implementation that needs it.

If on the other hand the method must be accessible for the consumers of the interface, you are violating the Interface segregation and Liskov substitution principles. Extract the method in an own interface.

0

Do not add the method to the interface. If the method is relevant only for that one implementation, only add it to that one. The point of the interface is to abstract the common elements of every factory, so a method specific to a single factory should not be in the general factory interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.