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
    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? – jonrsharpe Mar 28 '17 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. – Walfrat Mar 28 '17 at 7:35

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.


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, privacy policy and cookie policy

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