Sunday, May 10, 2009

The Open Closed Principle in C# (OCP in C#)



To improve and succeed in market every software needs “Change”. All software change during its life cycle. So it is very important for the developers to design software in a way which can accommodate changes with minimal impact. One of the design principles which help us in designing this kind of software is “Open Closed Principle (OCP)” suggested by Robert Martin. The OCP stats that
“Software entities (Classes, Modules, Functions etc) should be open for extension, but closed for modification”
Software entities that implement the OCP principle will have two big benefits. First they are open to extension which means that we can achieve new business needs with out changing the existing code much. Second they are closed for modification which means that they code itself will not change.
Let us consider an example. Let us say you are designing software for resource allocation. You have two option of resource allocation the “Time slot” and “Space slot”. The below code is implemented without the OCP. The disadvantage with the code is that every time there the new resource type added there need to a lot of changes to the code. Please note the number of comments in below code where the changes are required whenever there is an addition of resource type

Code without OCP


public class ResourceAllocator
{
public string Allocate(int intResourceType)
{
int intRes=0;
switch (intRes)
{
case 1:
return AllocateTimeSlot(intRes); ;
break;
case 2:
return AllocateSpaceSlot(intRes);
break;
//new Case has to be added for a new Allocation Type
default:
throw new Exception("No slot defination");
}
}
public string Free(int intResourceType)
{
int intResFree = 0;
switch (intResFree)
{
case 1:
return FreeTimeSlot(intResFree);
break;
case 2:
return FreeSpaceSlot(intResFree);
break;
//new Case has to be added for a new Allocation Type
default:
throw new Exception("No slot defination");
}
}

//New methods has to be created for the new allocation Type

public string AllocateTimeSlot(int intTimeSlot)
{
//Put the logic to Allocate the time slot
return "";
}
public string AllocateSpaceSlot(int intSpaceSlot)
{
//Put the logic to Allocate the Space slot
return "";
}

public string FreeTimeSlot(int intTimeSlot)
{
//Put the logic to Allocate the Space slot
return "";
}

public string FreeSpaceSlot(int intSpaceSlot)
{
//Put the logic to Allocate the Space slot
return "";
}
}
.
Now let’s take the same example and apply OCP to it.

Below is the code for the resource allocation with OCP

public abstract class ResourcePool
{
public abstract string Allocate(int IntRes);

public abstract string Free(int intRes);

}

public class SpaceSlot:ResourcePool
{

public override string Allocate(int IntRes)
{
//put the logic to allocate space slot
return "";
}

public override string Free(int intRes)
{
//put the logic to free space slot
return "";
}
}

public class TimeSlot : ResourcePool
{
public override string Allocate(int IntRes)
{
//put the logic to allocate time slot
return "";
}

public override string Free(int intRes)
{
//put the logic to Free time slot
return "";
}
}


Now you can have the logic for creating type of object in the resource manager class. Any addition of new code resouce type will not involve much code changes.

No comments: