Creational Pattern “These patterns focus on creation of objects”.

What is big deal in creating objects? As a developer you days in days
out you are creating objects, either directly in code by instantiating
the class or using IoC container. When we create object any class using
new keyword directly from inside any other class , we create a concrete
object which is tightly coupled to class which creates that instance
from within.

According to Open Closed Principles(OCP) (O in SOLID Principles) our classes should be “Open for extension but Closed for modification“, which means we should write the class in such a way that to add new (& different) functionality we should be able to easily add that functionality without have to change or break the existing method. Remember , it doesn’t say that you can’t change the class at all,
obviously if you have bug or some change in the existing method you have to update that.

Open Closed Principle(OCP) complements Single Responsibility Principle (SRP)

To illustrate this further, let’s explore the Single Responsibility Principle ((SRP), S in SOLID)- SRP states that a class, module or function should have one responsibility i.e. perform a single responsibility only. So what will you do if
you have to to add additional functionality to this class? Will you add the additional functionality in the same class as an additional method? It is good time to introduce the Open Closed Principle (OCP) as well . According to OCP principle “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” i.e. once a class or method has been written to perform a
functionality,it should not allow further modification. It may be confusing for some. Let’s dig in further. 

Most likely you will end up doing one of the below when you have a requirement to change a class to add more functionality.

1. Update the existing method in the same class- You can always do this, if your existing functionality has some bug or requirement change. But that is normal, OCP doesn’t restrict you from changing the method
for fixing/modifying the logic.  But what if you need to add different functionality which can be
called for certain cases? How to add a new functionality while while keeping the method same? I think it should ring a bell for you,  The question you should ask is whether you should create a new class
deriving from the existing class and override the method or overloaded method in the same class.

2. Add a new method/function in the existing class- Is the functionality an integral part of that class? If not creating new method breaks the SRP principle as the class now will have to do two different
jobs. Also, it will not be following OCP where it says the class should be closed for modification.

3. Adding functionality to a new class – If a  class has an additional job to perform you can consider creating another class that can perform that additional job.

4. Extending the existing class- You can extend that existing class by creating a new interface and implementing the additional functionality in the new class. You can then add that interface as a
dependency to that class.  You can also derive from the existing class and extend it. 

 These steps can be  achieved in number of ways depending upon different requirements, some of which you will learn in GoF (Gang of Four) Design Patterns like Decorator Pattern, Factory Method, etc.  In
addition to SRP and OCP, these patterns are also governed by the rest of SOLID Design Principles i.e. L- Liskov substitution, I- Interface Segregation, and D – Dependency Inversion Principle.

From the above, it should also be clear to you that SRP (Single Responsibility Principle) and OCP (Open/Closed Principle) complement each other, with each one aiding in the execution of the other and vice versa.

Leave a Reply

Your email address will not be published. Required fields are marked *