Solid Principle : Open Closed Principle (OCP)

I have already discussed Solid Principle : Single Responsibility Principle (SRP), today I am going to discuss the second principle of SOLID i.e. Open Closed Principle.

Open Closed Principle (OCP)

Almost everyone who have done some sorts of db involvement work are somehow familiar with the acronym CRUD. When we start working/designing a class we usually set our mindset that we are also doing CRUD operation to classes as well. i.e.

  • C -> Create a class
  • R -> Read from that class
  • U -> Update the class to fit our business need
  • D ->Delete the class when we don’t need that anymore.
Heads Up:

If you are designing  your class in such that it works like CRUD then you are doing it wrong!

The open closed principle (ocp) states that –

“Software entities (classes, modules, etc) should be open for extension, but closed for modification.”

That means, you are not allowed to do any modification(update or delete) of your existing class!  So, CRUD point of view soon changed into this –

  • C -> Create a class
  • R -> Read from that class
  • U -> Update the class to fit our business need
  • D ->Delete the class when we don’t need that anymore.

As you can see OCP basically imposed 2 certain constraints that you should have to follow. Now, you might be wondering why they prefer to follow certain restriction while designing a class. This is because it is assumed that the existing code (which might be already in production!) have already been reviewed and passed the unit test once. So literally we should not try to modify those existing code unless its mandatory coz if you make any changes on your existing  code base then that will require your code to gone through those process which is not only time consuming but also comes with some extra expense.

That’s why the preferred way of writing code is following the OCP principle. This ensures you code is extensible enough so that you only have to write it once and not have to change it later each time a new requirement comes.

Of course there might be situation where you will be forced to violate OCP principle and that is ok because after all we don’t want bugs to stay there on our code base forever. On such scenario you will make modification as much as needed but just keep the SOLID principle in mind.

Implementing OCP is pretty straight forward. First try to implement the SRP on your code base. This will separated each class with its own responsibility. The next step is to abstract these responsibility(concerns) and let consumers of these concerns talk to these abstraction.

Let me come up with a code example to give you an overview of OCP. If you google for OCP example almost everytime they come up with a Rectangle, Circle example. Here I am presenting a different scenario.

The above code snippet calls the generic GetAsync<T>(string EndPoint) method to fetch the mailchimp campaign analytic report. If you check the implementation of GetAsync<T>(string EndPoint) method you will see that under the hood its using HttpClient() class. Check the below code:

Now, assume after a certain period of time, you want to give a try to RestSharp (because RestSharp is opensource and it supports auto serialization as well).

Now, you can certainly create a new class that would expose method which would do GET request with RestSharp. Then you need to make changes to MailChimpReportsOverview class to point the new class. But you are not allowed to do so because OCP states you are not allowed to make modification.  So the only options you are left with is  BaseOperation class. Again changing to this class would also violate OCP! Plus it already has a implementation for HttpClient(). 

So as you can see the whole design that we have been using so far violates the OCP , plus its not easily extendable as well.

Solution :

The solution is simple. To bring the concept of interface, here is simple walk through.

Reference :


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s