Friday 20 May 2016

Open Closed Principle (OCP)

Software entities (class, modules, functions, etc) should be open for extension but closed for modification. Here, we try to explain OCP using codebase. First we'll show a scenario that violate OCP and then we'll remove that violation.

Area Calculation (OCP violation Code) :
public class Rectangle{
 public double Width {get; set;}
 public double Height {get; set;}
}

public class Circle{
 public double Radious {get; set;}
}

public double getArea (object[] shapes){
 double totalArea = 0;

 foreach(var shape in shapes){
  if(shape is Rectangle){
   Rectangle rectangle = (Rectangle)shape;
   totalArea += rectangle.Width * rectangle.Height;
  }
  else{
   Circle circle = (Circle)shape;
   totalArea += circle.Radious * circle.Radious * Math.PI;
  }
 }
}
Now if we need to calculate another another type of object (say, Trapezium) then we've to add another condition. But from the rule's of OCP we know Software entities should be closed for modification. So it is the violation of OCP.

Ok. Let's try to solve this violation implementing OCP.
public abstract class shape{
 public abstract double Area();
}

public class Rectangle : shape{
 public double Width {get; set;}
 public double Height {get; set;}

 public override double Area(){
  return Width * Height;
 }
}

public class Circle : shape{
 public double Radious {get; set;}

 public override double Area(){
  return Radious * Radious * Math.PI;
 }
}

public double getArea (shape[] shapes){
 double totalArea = 0;

 foreach(var shape in shapes){
  totalArea += shape.Area();
 }

 return totalArea;
}
Now when we need to calculate another type of object, we don't need to change logic here (in getArea()), we just have to add another class like Rectangle or Circle..

No comments:

Post a Comment