Open Closed Principle

Today I am gonna talk about the second principle in SOLID Principles of Object Oriented Design, it’s the Open Closed Principle 🙂

Open Closed Principle: that means that software entities (classes, modules, functions, … etc) should be Open for extension but Closed for modification.

If OCP applied well, changes will be achieved by adding new code, not by changing old code that already works … because each change can introduce bugs and require re-testing.

Now let’s talk about an example that violate the OCP and then try to fix it …

shapes

Here we have an Enum called ShapeType and have two values (circle, and square), and we have two classes called Circle and Square that have some properties and a Draw method.

shapes_main

Look with at the code that runs our classes … in the main method we create a list of shapes and we add to this list two shapes (circle and square), and after that we call DrawAllShapes method and give it our list of shapes as a parameter.

Now let’s take a look to our DrawAllShapes method .. it makes a for loop to iterate throw our list and for each shape we check it’s type and if it is a circle we create a circle object and assigns some values to it’s properties and after that call it’s DrawCirlce method, do the same if the type is square.

mmm What’s the problem in this code ?! .. Why it violates the Open Closed Principle ?!

Let’s think what we need to change if another shape will be added ?! (for example a triangle)

We need to add another class called triangle and also we will change our DrawAllShapes method and add a case for our new shape. (Note: here we change the existing code … too bad haa!! 😦 )

Now let’s write our program in a way that applies Open Closed Principle

ocp_shapes

Here we have an interface called Shape with a Draw method, and we also have two classes (circle and square) that implements this interface … so each class will have a different implementation for Draw method.

ocp_shapes_main

Now look at the previous code it also create a list of shapes and add circle and square to it, and after that we call our method DrawAllShapes.

But look to the difference between it’s implementation and the previous one, in this method we iterate throw our shapes and just call the Draw method.

Let’s ask the previous question again … what if i need to add a new shape ?! (for example a triangle)

We need to add another class called triangle and make it implement the Shape interface and that’s all what we need to do (Note: here no more code needed .. no more changes and modification in our old code .. so good haa 😀 )

Here you will find the source code for the example .. see you later with another new principle 🙂

Advertisements

Single Responsibility Principle

Today, I am gonna talk about the first principle in SOLID Principles of Object Oriented Design, it’s the Single Responsibility Principle 🙂

Single Responsibility Principle: every class should have a single responsibility “one job and only one job to do”.

But what’s the problem if the class has many responsibilities ?!
That means the class will not be robust, because it will be changed when new responsibilities come.

How can we solve this problem ?!
By separating the things that change.

You can find a useful example for applying SRP Here

See you later with another principle of SOLID Principles 🙂