Tutorials : Putting Design Principles to the Test(A Java based Case Study) :

Testing against OCP

What if we need to add another type of reporting say PDFReporter? Obviously, as ReporterServlet needs to be altered in order to accommodate this third type of reporting capability. Worse is the case, if we don’t want one of the existing type of report any more. Even then, we need to go back to the ReporterServlet and accommodate the change. Clearly, this is against OCP. This design is not closed for modification and is not open for extension.

OCP compliance

Clearly, the solution is to make the ReporterServlet depend on an abstraction of the ReportWriter. This way, the underlying implementation will not affect the ReporterServlet code to change. The design in Figure 4 is OCP compliant solution:

The classes in a package are reused together. If you use one of the classes in a package, you reuse all the classes in that package.


Figure 4: The OCP compliant design for the ReportWriter component

Test against DIP

The design in Figure 4 is OCP compliant and also maintains the principle of Dependency Inversion. Previously, the high-level module the ReporterServlet had a strong coupling with the concrete implementation of the individual Reporter. Now both the high-level module and the low-level XXXReporter implementations depend on the abstraction viz. Reporter interface.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.