- I have a game that deals with opening and closing doors and the Door Engine
deals with IDoor interface which has Open() and Close() contracts - So far so good. the game is tested and works fine.
- Now a new feature is required, doors can be automatically closed with a
timer, i.e a new contract- SetTimer() is needed.
Only parts of my Door Engine need to deal with setting the timer, the rest
of code need not deal with setting timer, i.e the old IDoor interface is enough.
How would you do it?
- Add the
SetTimer()
toIDoor
interface? and implement the method in the derived classes approp'ly, the existing Door classes can have a dummy method and our special TimedDoor can have approp implemention. Moreover, you could have a 'DoesSupportTimedClosing' contract to make it perfect LSP compliant. - Respect ISP (I know that ISP is for letting clients create their implementations without having to implement the entire interface, but can we consider my internal code as a client too, or am I having a wrong understanding) and create a new interface -
ITimedDoor : IDoor
(ITimedDoor will have theSetTimer
contract) you can create timeddoors that implement ITimedDoor, but, is this violation of LSP? If I want some arbitrary piece of existing code to set a timer on a door - I would need to do type checks and type casts to call theITimedDoor.SetTimer()
on theIDoor
object it has.
In a nut shell, if you have an Interface - IInterface1
and existing code thats already tested and works fine, how would you handle the requirement of adding new methods to it?
Some references on related topics: