Open Closed Principle in Java

Previous
Next

In this tutorial, we will learn about Open Closed Design Principle in Java.Open closed principle is one of the SOLID principles.

Open Closed Design Principle dictates that “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”. This definition was provided by Bertrand Meyer.

Once we have written the class and tested it, it should not be modified again and again but it should open for extension.If we modify already test clasess, it may lead to lot of extras effort to test it back and also there can be chances for the introduction of new bugs.

Strategy Design Pattern is another example of Open Closed design Principle. Service class can use various strategies to perform certain tasks based on requirement so we will keep Service class closed but same time, System is open for extension, by introducing new Strategy which will implement Strategy interface. At runtime, you can call Service Class with any new Strategy, based upon your need.

Let’s understand with the help of a simple example.
You need to create two types(CSV and XML) of reports based on input type.Please note that there should be provision to add new report type in future.

Create an Enum called “ReportType.java” as below.

Create Service class named ReportingService as below:

Create a main class named “GenerateReportMain.java” which will called ReportingService to generate report.

Output:

===================================
Generating report based on Type
===================================
Generate CSV Report

===================================
Generating report based on Type
===================================
Generate XML Report

As you can see, this is simple code which is working fine.You have tested code and found out that you are able to generate report based on the type.
Now you need to create one more report type i.e. Excel.If you notice, if you need to make changes as below:
1) You need to make changes in Enum ReportingType.

2) You need to make changes in ReportingService class which you have already tested.

Now you can generate Excel reports as below.

Output:

===================================
Generating report based on Type
===================================
Generate CSV Report

===================================
Generating report based on Type
===================================
Generate XML Report

===================================
Generating report based on Type
===================================
Generate Excel Report

As you can see, we have to modify at many places which we have already tested and we need to retest all the functionalities again.

Open Closed Design Principle

Let’s see how open closed design principle will be able to solve the problem.
Create ReportingService.java as below.

Create an interface called ReportingStrategy as below.

Create a class named “CSVReportingStrategy.java” for generating CSV reports

Create a class named “XMLReportingStrategy.java” for generating XML reports

Let’s create a main class GenerateReportMain.java now.

Output:

===================================
Generating report based on Type
===================================
Generate CSV Report

===================================
Generating report based on Type
===================================
Generate XML Report

Let’s say you want to generate Excel reports.You need to create below changes:
Create another class named “ExcelReportingStrategy.java” as below.

Change in GenerateReportMain.java to add calling code.

Output:

===================================
Generating report based on Strategy
===================================
Generate CSV Report

===================================
Generating report based on Strategy
===================================
Generate XML Report

===================================
Generating report based on Strategy
===================================
Generate Excel Report

As you can see, we did not make any changes ReportingService which was already tested.We just added new class “ExcelReportingStrategy” which enabled us to generate Excel report.

That’s all about Open Closed Design Principle in Java.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog