0

I do see a lot of questions posted on OC principle and some have good answeres , i still have some doubts regarding this , below are the ones.

1) Lets assume there is registration module in my application currently it supports only one country , i get change request where i have to now support 3 more countries ( Registration form does not change it still remains same) it is just i have to classify users based on country . Only change in my application would be get the country data from UI and save it in DB , do i have to apply OCP here.

2) Does OCP mean there should not be any if else logic in my methods(function) , i know if else is not good for object creation but if i have some business logic to be implemented based on scenario should i not use if else as if more condition gets added i have to modify code.

sumedha
  • 473
  • 1
  • 9
  • 24

1 Answers1

1

Open Closed principle applies to almost all the designs and to yours as well. The only difference from what you have described as scenarios(and these are valid scenarios) is that, you have brought it down to a change request(point 1) and coding practices(point 2).

Open Closed principle however is meant to be looked at more at a higher level i.e.design level.Also it should be seen as a best practice which should be aimed and one should try to be as close to the Open Closed design as possible(i.e. have max possible part of your system open for extension and have max possible part of your system closed for modification). Let me take the 2 scenarios and explain how Open-Closed can be applied to them -

Point 1: 3 new countries scenario - Just 3 new values added to a UI field does not warrant a design change. However, if you have country specific business logic in your code then this change does affect the design. In the case of country-specific logic, you should have designed the business logic implementation in such a way that you could write say a new service class/country-specific handler extending a base handler. This country specific service invocation can be configured to be called based on the actual country selected or the country specific handler can be instantiated based on the country selected in UI. This is then Open for Extension.

On the other hand, the base class service/BaseHandler remains the same and the sequence of execution of code remains the same(or sequence diagram remains the same). The core of your application is thus Closed for modification.

This is how Open-Closed can be applied to this point.

Point 2: If-else - Whether If-else breaks the Open Closed Rule depends on where it is applied. If-else inside a service method for fine-grained business logic is absolutely fine. It is even fine if you are using it to select which handler to instantiate from the factory of handlers.

However, it is not fine if you have a single service method for handling all the countries and you are doing an if-else to check if country 1 then save in DB, if country 2 then ignore etc. This is not a "Closed for Modification" design.

Hope I was able to explain the concept to you.

I have written an article on my blog about Open Closed Principle where I have explained this principle in detail - http://www.javabrahman.com/programming-principles/open-closed-principle-with-examples-in-java/ .In case you still have any doubts you can check it out as well.

Dhruv Rai Puri
  • 1,335
  • 11
  • 20