Search code examples
oopsolid-principles

OOP and new functionality


I'm going to add new check to existing functionality of class A (method foo()). And I think it would be better to create new class B inherited from A with new private method check() which would be called from foo(). From oop point of view it fits open-close principle, but I'm not sure about Liskov substitution principle. What do you think? Is it right decision ?


Solution

  • If with check() you change the class behavior of foo() then it perhaps does not fit LSP. If check() makes a side-effect, I would create a new method in B like foo_with_check(). If it is just to check a pre-condition before foo() really does something, you can also do it or consider a pattern like proxy.