Thursday, November 24, 2011

Great Example of Open Closed Design Principle in Java

Great Example of Open Closed Design Principle
I am big fan of design pattern article and love to read articles on design patterns and recently wrote about decorator design pattern in JavaObserver patternstatic factory pattern and about Singleton pattern. Today I come across this good article on open closed design pattern, what I like most is there example and clear way of explanation, first example is true value and it will help you understand open closed principle very quickly and second example is also not bad. I have also shared many questions on design patterns and Object oriented design principles like 10 Java singleton interview questions , which discusses some of the most common questions on Singleton pattern like how to write Thread-Safe Singleton class , should we use Enum as Singleton or not etc and 20 design pattern interview questions, if you are preparing for senior level Java interview, where questions from design is expected, its worth looking for quick review.

open closed design principle exampleOne word about Open Closed principle is a design principle which says that a class should be open for extension but closed for modification. Open Closed Principles is one of the principle from SOLID design principle where it represent "O". Read more about OOPS and SOLID design pattern in my post 10 OOPS and SOLID design principles Java programmer should know.  


In Simple language Open closed design principles says that new functionality should be added by introducing new classes, methods or fields instead of modifying already tried and tested code. One of the way to achieve this is Inheritance where class is extended to introduce new functionality on top of inherited basic features.

Benefit or Open Closed Design Principle:
1) Application will be more robust because we are not changing already tested class.
2) Flexible because we can easily accommodate new requirements.
3) Easy to test and less error prone.

How to make code extensible
Basic principle of making your code extensible and following open closed principle is providing object to class at run time and making use of polymorphism to invoke extended functionality.
If functionality is hard Coded than it wouldn’t be extensible but if you write interface and provide implementation of that interface at run time you make it extensible.


For detailed example see this link open closed design pattern on sanaulla blog post http://blog.sanaulla.info/2011/11/19/solid-open-closed-principle/

Interview Questions you may like

3 comments :

OOOD said...

I think best example of Open Closed Principle is alphabets and words, you have just got 26 alphabets but you can make any number of words. 26 Alphabets are close for modification but open to made any words, what do you say?

Anonymous said...

I think key to understand Open Closed principle or OCP is creating better abstraction. If you have better abstraction, you will more likely to confirm open closed design principle. At the same time, you should be careful of creating too many abstraction, which will result in lots of vapor classes, Instead you can delay creation of Abstraction related to Open Closed principle, till requirement comes up, but once a requirement to change a module comes up, you should put together correct set of abstraction, so that module can handle similar changes on that module.

Side note: If your class tends to follow Single Responsibility principle (SRP), then they are more likely to follow OCP as well, because it delegates responsibility to a different class, which handles a particular abstraction.

Raj said...

Strategy Design Pattern is another example of Open Closed design Principle. By using Strategy to perform certain task, we keep Service Class, which uses this strategy closed, but same time, System is open for extension, by introducing new Strategy, by implementing Strategy interface. At runtime, you can compose Service Class with any new Strategy, based upon your need.

Post a Comment