Blog

Filter posts by Category Or Tag of the Blog section!

Open Closed Principle of Solid

Tuesday, 03 December 2013

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: 

 

 public class ElementerySchool
    {
        public double Norm { get; set; }
        public int Age { get; set; }
        public bool Validate { get; set; }

    }

    public class HighSchool
    {
        public double Norm { get; set; }
        public bool Validate { get; set; }
    }

    public class LibraryValidation
    {
        public const double Norm = 17;
        public const int Age = 8;
       
        private HighSchool _highSchool = new HighSchool();
        private ElementerySchool _kindergarten = new ElementerySchool();

        public bool ValidateQualification(double norm, int? age)
        {
            if (norm < Norm)
            {
                _highSchool.Validate = false;
            }
            if (age < Age && norm<Norm)
            {
                _kindergarten.Validate = true;
            }
            return true;
        }
    }

 

 High school students with the limited norm of 17 are valid to be a library member but for the kindergarten student, there are two rules: the 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 the property for validating the students, you know there are lots of good ways to do that)

 

 public abstract class Student
    {
        public abstract double Norm { get; set; }
    }

    public class HighSchool : Student
    {
        private double norm;
        public override double Norm
        {
            get
            {
                if (norm > 17) return norm;
                return norm;
            }
            set { norm = value; }
        }
    }

    public class ElementerySchool : Student
    {
        private int age;
        private double norm;

        public override double Norm
        {
            get
            {
                if (norm > 17)
                {
                    return norm;
                }
                return 0; // other business
            }
            set
            {
                norm = value;
            }
        }

        public int Age
        {

            get
            {
                if (age > 5)
                    return age;
                return 0;// other business
            }
            set
            {
                age = value;
            }
        }
    }

 

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. These properties are closed for modification but you can extend them by adding another business.

Category: Software

Tags: Principle

comments powered by Disqus