Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open closed principle

I understand that this principle states that a module is open for extension but closed for modification, and this is clear when we want to upgrade/modify a method in some class - we create another class that inherits the base class and then override the base method here in order to keep both functionalities.

But how about the case when we want to add another different method to the base class? Is this considered as modification or extension of the base class - is ok to add the method to the base class or we should also create new class that inherits the base class and then put new method there?

Also, same question for adding properties and fields to the base class.

like image 310
Ivan Avatar asked Jun 28 '17 08:06

Ivan


People also ask

What is open close principle with example?

The Open-Close principle (OCP) is the O in the well known SOLID acronym. A module will be said to be open if it is still available for extension. For example, it should be possible to add fields to the data structures it contains, or new elements to the set of functions it performs.

What is the benefit of Open-Closed Principle?

The main benefit of this approach is that an interface introduces an additional level of abstraction which enables loose coupling. The implementations of an interface are independent of each other and don't need to share any code. Thus, you can easily cope-up with client's keep changing requirements.

What is Open-Closed Principle in Python?

The open-closed principle states that a class, method, and function should be open for extension but closed for modification. The open-closed principle sounds contradictory.

What are the 5 SOLID principles?

SOLID is an acronym that stands for five key design principles: single responsibility principle, open-closed principle, Liskov substitution principle, interface segregation principle, and dependency inversion principle. All five are commonly used by software engineers and provide some important benefits for developers.


1 Answers

TL;DR
I think adding a new method inherently means that you cannot violate OCP; as OCP mainly focuses on the reckless overriding of existing methods.


Example:
Base class Calculator is inherited by MyCustomCalculator. The base class only has methods for adding, subtracting, multiplying and dividing two numbers.
Creating a new method in MyCustomCalculator, e.g. CalculateAverage(params int[]) does not violate OCP. It does not modify the logic behind any of the existing methods, it merely extends the methods that are provided by the calculator object.

New methods inherently do not change the old methods. Therefore, they don't modify existing logic; then simply extend what is available.

So no, it does not (inherently) violate OCP.

It's almost impossible for it to violate OCP, as creating something new is (inherently) completely different from modifying something that exists.


Some things to take note of though:

  • I can think of one (arguable) exception to this: overloading. If you create a new method that overloads an existing one, then you are responsible to abide by the rules of overloading. Most notably, this means that overloaded methods should perform the exact same task as eachother (overloaded methods are simply based on different input parameters, but their processing and output should be functionally equivalent). Although I am not 100% sure if this is a violation of OCP (because it wrongly extends a base class), or SRP (because it creates an ambiguous responsibility as to the processing/output of the overloaded methods). It seems like a bit of both.
  • The same principle applies even if your new method does not overload an existing one. Is the new method relevant enough to the responsibility (intention/purpose) of the base class? Because if it is very different, then you might be violating SRP.

EDIT

I missed part of your question

is it ok to add the method to the base class or we should also create new class that inherits the base class and then put new method there?

That depends on context. For the Calculator / MyCustomCalculator / CalculateAverage() example I provided; I would instinctively add it to the base class, as calculating averages is a basic operation that pertains to the responsibilities of a calculator.

However, if we were discussing adding a method that only pertains to complex numbers (MethodForComplexNumbers()), then I would advocate the creation of a ComplexNumberCalculator which inherits from Calculator and implements MethodForComplexNumbers().

However, I think this is mainly due to SRP, not OCP.

like image 84
Flater Avatar answered Oct 04 '22 06:10

Flater