Ehsan Ghanbari

Experience, DotNet, Solutions

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: 

 

 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.

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
How to query over Icollection<> of a type with linq
What's the DDD-lite?
comments powered by Disqus