Design Interfaces – Violating the Interface Segregation Principle

designinterfaces

I currently have an interface named InternalEntityIdTransformer, which has a getInternalId method, that given an external id returns the corresponding internal one.

Now I am working on functionality that will need to do the opposite transformation, and go from internal id to external id. Is adding an getExternalId method to this InternalEntityIdTransformer a bad violation of the ISP? (All places that currently are using this interface only need the getInternalId and are not going to care about getExternalId at any point.)

And if it is, then is is particularly harmful? Should I rather have two dedicated interfaces here?

The language used is Java, PHP or C#.

Best Answer

ISP basically says that it's not good to require from a class to implement unrelated things, and/or make a client require unrelated things. Methods in an interface are related — a client may need all of them to complete some reasonable task.

I think getInternalID and getExternalID belong to separate interfaces, since clients wanting one of these don't need the other, per your words.

The class that currently implements InternalEntityIdTransformer could happily implement one more interface, say, ExternalEntityIdTransformer, if sharing of implementation details saves you some work. If at some moment you would need to split the implementations (e.g. external to internal is trivial, but the reverse requires heavy lifting), it will be painless, as it should be.

Related Topic