Open/closed principle

Today we are going to look at the open/closed principle from the the SOLID principles. The first mention of the open/closed principle was by Bertrand Meyer in the late 80’s. According to his definition a class is closed, because it’s in stored in a library and used by other classes. It is also open, since you can inherit from this class. This way all existing users of the class don’t need to be changed, but you can change the functionality of the class.

Example

    interface IMove
    {
        void Up();
        void Right();
        void Down();
        void Left();     
    }

We define an interface with the methods our classes must implement.

    class Taxi : IMove
    {
        public int X { get; private set; }
        public int Y { get; private set; }

        public Taxi(int x, int y)
        {
            X = x;
            Y = y;
        }

        public void Down()
        {
            Y -= 1;
        }

        public void Left()
        {
            X -= 1;
        }

        public void Right()
        {
            X += 1;
        }

        public void Up()
        {
            Y += 1;
        }
    }

A Taxi class which drives at normal speed.

    class RaceCar : IMove
    {
        public int X { get; private set; }
        public int Y { get; private set; }
        public int Speed { get; private set; }

        public RaceCar(int x, int y)
        {
            X = x;
            Y = y;
            Speed = 2;
        }

        public void Down()
        {
            Y -= 1 * Speed;
        }

        public void Left()
        {
            X -= 1 * Speed;
        }

        public void Right()
        {
            X += 1 * Speed;
        }

        public void Up()
        {
            Y += 1 * Speed;
        }
    }

A RaceCar class which drives twice as fast.

            RaceCar racecar = new RaceCar(20, 20);
            Taxi taxi = new Taxi(10, 10);

            IList<IMove> movableObjects = new List<IMove>(){
                racecar,
                taxi
            };

            foreach (var item in movableObjects)
            {
                item.Up();
            }

Here we create a List of movable objects and move all of the objects in here up. When we create a class person that implements IMove we can add it to the list and we do not have to change the code for moving in the foreach. Because of this it’s open for extension (you add new classes that implement IMove) and closed for modification (You do not need to change the foreach when new classes with IMove are added).

How to recognize you violate Open/closed principle

The most important way to recognize that you are violating the open/closed principle is if you see multiple switch / if statements that are checking for the same conditions all over your code. When you see this try too look if you can abstract these conditions away using polymorphism as in the example above.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s