Open Closed Principle of Solid

Maybe you have heard this for times: "Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification."

it should be possible to extend the existing code with new functionality without any modification and without adding duplicate code or duplicate functionality. Take a look at this piece of code: 

  1.  public class ElementerySchool
  2.     {
  3.         public double Norm { get; set; }
  4.         public int Age { get; set; }
  5.         public bool Validate { get; set; }
  6.  
  7.     }
  8.  
  9.     public class HighSchool
  10.     {
  11.         public double Norm { get; set; }
  12.         public bool Validate { get; set; }
  13.     }
  14.  
  15.     public class LibraryValidation
  16.     {
  17.         public const double Norm = 17;
  18.         public const int Age = 8;
  19.        
  20.         private HighSchool _highSchool = new HighSchool();
  21.         private ElementerySchool _kindergarten = new ElementerySchool();
  22.  
  23.         public bool ValidateQualification(double norm, int? age)
  24.         {
  25.             if (norm < Norm)
  26.             {
  27.                 _highSchool.Validate = false;
  28.             }
  29.             if (age < Age && norm<Norm)
  30.             {
  31.                 _kindergarten.Validate = true;
  32.             }
  33.             return true;
  34.         }
  35.     }

 High school students with the limited norm of 17 are valid to be a library member but for kindergarten student, there are two rules: limited norm of 17 and limited age of 5!  Now after implementing this piece of code by applying OCP, it could be something like this (for keeping it simple I prefer to use property for validating the students, you know there are lots of good ways to do that)

  1.  public abstract class Student
  2.     {
  3.         public abstract double Norm { get; set; }
  4.     }
  5.  
  6.     public class HighSchool : Student
  7.     {
  8.         private double norm;
  9.         public override double Norm
  10.         {
  11.             get
  12.             {
  13.                 if (norm > 17) return norm;
  14.                 return norm;
  15.             }
  16.             set { norm = value; }
  17.         }
  18.     }
  19.  
  20.     public class ElementerySchool : Student
  21.     {
  22.         private int age;
  23.         private double norm;
  24.  
  25.         public override double Norm
  26.         {
  27.             get
  28.             {
  29.                 if (norm > 17)
  30.                 {
  31.                     return norm;
  32.                 }
  33.                 return 0; // other business
  34.             }
  35.             set
  36.             {
  37.                 norm = value;
  38.             }
  39.         }
  40.  
  41.         public int Age
  42.         {
  43.  
  44.             get
  45.             {
  46.                 if (age > 5)
  47.                     return age;
  48.                 return 0;// other business
  49.             }
  50.             set
  51.             {
  52.                 age = value;
  53.             }
  54.         }
  55.     }

As you can see, changing any part of this piece of code will influence other parts and rather than that each school has its own rules for validating the students. This properties are closed for modification but you can extend them by adding another businesses.


Tags: Principle


comments powered by Disqus