We need to follow the Design principle while developing the software to reduce the TCO (Total Cost of Ownership), Reduce the Cost of maintenance and for robust design.

 

Definition:
The name itself sounds like contradicting – OPEN & CLOSE, but by definition, A module should be OPEN for extension but CLOSED for modification. The Open-Closed Principle (OCP) is one of the most important Design Principle. OCP originated from the work of by the Bertrand Meyer.

 

In simple words: We can add new features in the module without changing the existing code (CLOSED) but by adding new code (OPEN).  By following this principle, we can make sure that by changing the code in the method we are not going to break existing functionality. Let us try to see it using the example.

 

Example:
First of all we would see the code which doesn’t follow this principle. Consider we have a class which prints the shape information of different shapes based on the input.
image

Code snippet which violates this principle.

 

Now the problem with this code is, when ever we need to add a new type of Shape, we need to change the code in the CREATE_SHAPE, WRITE_SHAPE method, which is clear violation of the OCP. According to OCP, we should only change the method to add new functionality. Here we are not adding a new functionality. Creation of the Shape Object and writing the information of the different Shape is not a new functionality. So, we must re-design our class and methods to be able to follow the Open Closed Principle.

 

We can use the dynamic polymorphism to achieve Open-Closed design. Check the UML for the new design.

image

Now we would create an interface LIF_SHAPE which we will use to create different shapes like circle using LCL_CIRCLE, square using LCL_SQUARE and so on.

 

Implementation of the class LCL_CIRCLE and LCL_SQUARE implements the method

 

Design of the report class is also changed. LCL_REPORT would now accept the class name instead of the simple type to instantiate required object as the parameter of the method CREATE_SHAPE.

 

Implementation of the class LCL_REPORT needs to be changed. CREATE_SHPAE would create the object and WRITE_SHAPE would call the method WRITE_SHAPE from the object O_SHAPE.

 

Full code Snippet which shows how to achieve OCP design.

 

Now, if we want to add a new shape, say TRIANGLE. We can easily add a new shape without changing any existing code.
1. Create a new class LCL_TRIANGLE which implements the interface LIF_SHAPE
2. Implement the method LIF_SHAPE~WRITE_SHAPE in this class.

 

Conclusion:
By using the polymorphism, now both methods CREATE_SHAPE & WRITE_SHAPE of the class LCL_REPORT are CLOSED for any modification and we have successfully achieved OCP design.
Credits:
Bertrand Meyer
Robert C. Martin

To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

  1. Alvaro Tejada Galindo
    Naimesh!

    I was wondering when I was going to read your next blog…good as always! -:D
    I think that OCP is very cool and useful and most important…simple. Thank for sharing -;)

    Greetings,
    Blag.

    (0) 
  2. Pranav Bhatt
    Hi Naimesh,

    OCP sounded contradictory to me everytime but this blog explains it very well with examples.
    keep sharing and blogging…

    Regards,
    Pranav

    (0) 
    1. Naimesh Patel Post author
      Hello James,

      Thanks for your compliments!
      Whenever, I tried to search for OCP with example in ABAP, I didn’t find anything. Those fail efforts encouraged me to write about it.

      Regards,
      Naimesh Patel

      (0) 
  3. Jason Scott

    Nice blog which describes an OO concept that doesn’t seem to be well understood amongst the abap community. Would be good if you could look at the formatting of the code sections though – problem with line wrapping.

    (0) 

Leave a Reply