The Second 5 Principles of Object Oriented Design Part 2

The Second 5 Principles of Object Oriented Design Part 2

Open-Closed Principle (OCP)

Open Closed principle is the 2nd principle of the SOLID principle. In SOLID O is stand for Open Closed principle. Open Closed principle states software entities should be open for extension but closed for modification, which means you can change behavior without modifying source code. You shouldn’t touch the existing code. The main idea of this principle is to keep existing code from breaking when you implement new features, which means extending functionality by adding new code instead of changing existing code.


The world changes, business changes, and thus, code changes no getting around that. But what this principle does mean is that we restrict the possibility of changing existing code as much as possible. And it also kinda tells you how to do that: classes should be open for extension and closed for modification.


?In the programmatical word “Separate extensible behavior behind an interface, and flip the dependencies”, that means separate the behaviors, so the system can easily be extended, but never broken.


The Goal of Open Closed principle is Get to a point where you can never break the core of your system.


Let me give you some real-life example


At first, let start by violating the OCP(Open Closed principle) in real-life project. Imagine you have a project where you have multiple platform options for shopping. in this point there are chances to break OCP(Open Closed principle). Now dive into the deep. Look at the picture below.

No alt text provided for this image


To use the?Shop class, we need to instantiate the class in buy method as the picture below.

No alt text provided for this image


And in Shop class you also see two method for two different shop methods. And still this is safe zone. you are not breaking OCP(Open Closed Principle) but if you want to add another shop method like Alibaba as the picture below and now you are breaking OCP(Open Closed Principle) as you are modifying the existing Shop class, not extending.

No alt text provided for this image


You also breaking OCP(Open Closed Principle) in buy method as you are modify the buy method instead of extending like the picture below.

No alt text provided for this image

Now the question is, how can we write code without breaking the OCP(Open Closed Principle)?. How can we extend code instead of modifying the existing code?. How can we add functionality without modifying or touching the exiting code?. now I’ll show you How. we solve the problem with interface. Let’s solve the OCP violation! See the picture below carefully.

No alt text provided for this image
No alt text provided for this image

In the picture above you can see ShopInterface with getProduct method and two different classes ShopWithAmazon and ShopWithEbay. Each Shop classes will implements the getProduct method from ShopInterface.

Now our classes are easily extendable. If asked to add another shopping platform like Alibaba, we can easily do by creating new class that implements the getProduct method from ShopInterface without touching the existing code like the picture below, how easy it is.

No alt text provided for this image


Now we create a?BuyManager class that manages all shop classes:

No alt text provided for this image

We can now pass any class that implements the?ShopInterface?interface to this class. This is the flexibility that interfaces provide.

In conclusion, the Open Closed principle is about designing the system in a way that makes it easy to extend the behavior without modifying the existing code, rather than never modifying the existing code.

Thank you so much for reading.

要查看或添加评论,请登录

Sagni Alemayehu的更多文章

社区洞察

其他会员也浏览了