The Open-Closed Principle & Physics: Extending Our Understanding

In this article we are going to discusses the concept of open-closed principle in object-oriented programming and demonstrates how it can be applied to create flexible and maintainable code. The article also provides examples of implementing the princi…


This content originally appeared on Level Up Coding - Medium and was authored by Onur Derman

In this article we are going to discusses the concept of open-closed principle in object-oriented programming and demonstrates how it can be applied to create flexible and maintainable code. The article also provides examples of implementing the principle in C# and uses the context of the evolution of physics from Newton’s laws to Einstein’s theory of relativity to illustrate the importance of adapting to changing circumstances in both science and software development
Beautiful Minds

Introduction

For almost 300 years, Newton’s laws of motion dominated the field of physics, providing a solid foundation for our understanding of the physical world. However, in the early 20th century, a new theory of relativity proposed by Albert Einstein completely revolutionized the field, changing the way we think about time, space, and gravity. In this article, we will explore how the Open-Closed Principle in programming relates to this concept.

Understanding the Open-Closed Principle

The Open-Closed Principle is a key principle in object-oriented programming that emphasizes the importance of designing software systems that are extensible and maintainable. The principle states that software entities should be open for extension but closed for modification. This means that new features should be added to the system without modifying existing code. Instead, the system should be designed to allow for new functionality to be added through extension.

Why the Open-Closed Principle Matters

The Open-Closed Principle is important because it helps ensure that software systems remain flexible and adaptable to changing requirements. By designing software systems that are open for extension but closed for modification, developers can create systems that are easy to extend and maintain. This is especially important in software development, where changes in requirements are common.

How are OCP and Evolution of Physics related?

This principle relates to the evolution of physics in that, just as Einstein’s theory of relativity built upon and extended Newton’s laws of motion (actually it demolished the Newtonian physics but extended can be used because we are still using Newtonian mechanics with smaller velocities), new software features should be built on top of existing code rather than modifying it directly. This approach ensures that the system remains flexible and adaptable to changing requirements.

Let’s Code :)

Important Note: You can have the source code from my github repo. The source code is at the end of the article.

Let’s take a look at an example to understand the importance of the OCP in programming. The following code shows the Newton Kinetic Energy Calculator, which was widely used to calculate the kinetic energy of an object based on its mass and velocity.

Newton Calculator Before

However, after Einstein changed the world of physics with his theory of relativity, a new formula was needed to calculate the kinetic energy of an object that is moving at relativistic speeds. To implement this new formula, the Newton Kinetic Energy Calculator would have to be modified, which goes against the OCP.

So how are we going to improve it? When I first started programming one year ago, I was listening to Akın Kaldıroğlu, an excellent instructor who gave very valuable trainings about the OOP concept. His most memorable sentences for me were “program to interfaces not implementations” and “depend on abstractions, not on concretions,” and we will use these principles in this example. I would like to express my respect to him here.

Let’s program to interfaces: The calculator can be extended by creating a new class that implements the same interface but uses the new formula. The following code shows an interface for a Kinetic Energy Calculator and a class that implements the interface using the relativistic formula to calculate kinetic energy.

Interface that will helps us to extend the program

Please do not feel overwhelmed about the formulas. Just focus on the concept and the general structure.

Our Relativistic Calculator
Newton Calculator is now an IKineticEnergyCalculator

Now see how easy we adopt the new formula. We just create a class and an interface. But most importantly we did not change even a single line of code from existing Newton Energy Calculator class. In OCP, this is the most important concept that you should obey. Do not modify any existing code, but you can extend the system by adding new classes and interfaces. That is the whole idea. Finally, let’s write the Calculator Program:

How powerful the Open-Closed Principle is right? I love the idea behind it.

I appreciate your participation and hope that my article has provided you with valuable insights. Thank you for taking the time to read it thus far.

Code Source:
Medium-Articles/OpenClosedPrinciple at master · onrdr/Medium-Articles (github.com)


The Open-Closed Principle & Physics: Extending Our Understanding was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Onur Derman


Print Share Comment Cite Upload Translate Updates
APA

Onur Derman | Sciencx (2023-05-14T15:07:03+00:00) The Open-Closed Principle & Physics: Extending Our Understanding. Retrieved from https://www.scien.cx/2023/05/14/the-open-closed-principle-physics-extending-our-understanding/

MLA
" » The Open-Closed Principle & Physics: Extending Our Understanding." Onur Derman | Sciencx - Sunday May 14, 2023, https://www.scien.cx/2023/05/14/the-open-closed-principle-physics-extending-our-understanding/
HARVARD
Onur Derman | Sciencx Sunday May 14, 2023 » The Open-Closed Principle & Physics: Extending Our Understanding., viewed ,<https://www.scien.cx/2023/05/14/the-open-closed-principle-physics-extending-our-understanding/>
VANCOUVER
Onur Derman | Sciencx - » The Open-Closed Principle & Physics: Extending Our Understanding. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/14/the-open-closed-principle-physics-extending-our-understanding/
CHICAGO
" » The Open-Closed Principle & Physics: Extending Our Understanding." Onur Derman | Sciencx - Accessed . https://www.scien.cx/2023/05/14/the-open-closed-principle-physics-extending-our-understanding/
IEEE
" » The Open-Closed Principle & Physics: Extending Our Understanding." Onur Derman | Sciencx [Online]. Available: https://www.scien.cx/2023/05/14/the-open-closed-principle-physics-extending-our-understanding/. [Accessed: ]
rf:citation
» The Open-Closed Principle & Physics: Extending Our Understanding | Onur Derman | Sciencx | https://www.scien.cx/2023/05/14/the-open-closed-principle-physics-extending-our-understanding/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.