Click here to Skip to main content
Click here to Skip to main content

Tagged as

Easy way to understand open/closed principle :O of S.O.L.I.D

, 25 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Easy way to understand open/closed principle :O of S.O.L.I.D
Introduction
 
This is just an attempt to make the audience understand the open/closed principle going through step by step refactoring to ultimately coming up with best design adhering OCP.
 
Open/Closed principle (OCP) states "Software entities should be open for extension and closed for modification".

Background
 
We were working on a project where we were using Oracle as the prime repository until one fine day our technical lead came up and announced that our application should support SQL Server as another data source.
 
Now our Data access layer had a DBConnection class with a connect method strongly bound to accept Oracle provider and establish a connection.
 
I ignored the detailed implementation and tried to keep things simple.
 
Initial Code
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SOLID
{
    class DBConnection
    {
        public void Connect(OracleProvider ora)
        {
            Console.WriteLine("Connected to oracle data source");
        }
    }
 
    class OracleProvider
    {
        string _providername;
    }
}
 
As we were in a hurry for the delivery, we could only come up with this design for the existing dbConnection class so that it now supports SQL provider too.
 
After Refactoring
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SOLID
{
    class DBConnection
    {
 
        public void Connect(object o)
        {
 
            if (o is OracleProvider)
            {
                Console.WriteLine("Connected to oracle data source");
            }
            else if (o is SQlProvider)
            {
                Console.WriteLine("Connected to sql data source");
            }
        }
    }
 
    class OracleProvider
    {
        string _providername;
    }
 
    class SQlProvider
    {
        string _providername;
    }
}
 

 
OMG ! We designed something wrong ...
 
Here if you can see from the above code, we are modifying the class DBConnection rather than extending it by including if else statements, this simply is against OCP.
 
Reason is simple if tomorrow another provider is introduced, again we have to make changes to the DBconnection class.
 
Ultimate refactoring ...
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SOLID
{
    interface IDBConnection
    {
        public void Connect();
    }
 
    class DBConnection
    {
        public void Connect(IDBConnection con)
        {
            con.Connect();
        }
    }
 
    class OracleProvider :IDBConnection
    {
        string _providername;
 
        public void Connect()
        {
            Console.WriteLine("Connected to oracle data source");
        }        
    }
 
    class SQlProvider:IDBConnection
    {
        string _providername;
 
        public void Connect()
        {
            Console.WriteLine("Connected to sql data source");
        }        
    }
}
 
Finally the main method ....
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SOLID
{
    class Program
    {
        static void Main(string[] args)
        {
            //Sql connection 
            IDBConnection conSQl = new SQlProvider();
            conSQl.Connect();
 
            //oracle connection 
            //IDBConnection conOra = new SQlProvider();
            //conOra.Connect();
        }       
    }
}
 
Conclusion
 
The above design we could come up seems to adhere to OCP.
Now tomorrow if we have to support another Data source, we can easily do it by simply extending IDBConnection needless to make any changes to DBConnection.
 
So the entities are now open for extension and closed for modification.
 
Happy Coding. Big Grin | :-D

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

imsauravroy
US based MNC
India India
Computer geek very much interested in learning and adopting latest and cutting edge technologies .
 
Expertise in asp.net,c#,WCF,web services,Jquery,MVC ,TTD's and design principles and patterns applying SOLID
 
Designation - Project Lead/Architect
Group type: Organisation

4 members


Comments and Discussions

 
GeneralI don't understand the use of DBConnection in this example. ... PinmemberSivajish1-Mar-11 15:36 
GeneralRe: I'd like to see the code for this. Could you provide the exa... PinmemberKeith.Badeau1-Apr-11 9:48 
Generalyes i agree with you, and there are lacs of text book exampl... Pingroupimsauravroy25-Feb-11 4:49 
GeneralReason for my vote of 2 typical text book example, should go... Pinmemberyounkarm_chung25-Feb-11 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 25 Feb 2011
Article Copyright 2011 by imsauravroy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid