Wednesday, August 11, 2021

Open Closed Design Principle in Java - Benefits and Example

Great Example of Open Closed Design Principle
I am a big fan of design pattern articles and love to read articles on design patterns and recently wrote about decorator design pattern in JavaObserver patternstatic factory pattern and Singleton pattern. Today I come across this good article on open closed design patterns, 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 the Open-Closed principle is a design principle that says that a class should be open for extension but closed for modification. Open Closed Principles is one of the principles from SOLID design principle where it represents "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

The basic principle of making your code extensible and following open-closed principle is providing an object to class at run time and making use of polymorphism to invoke extended functionality.

If functionality is hard Coded then it wouldn’t be extensible but if you write an interface and provide an implementation of that interface at run time you make it extensible.





Interview Questions you may like

4 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.

Unknown said...

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. We should be able to extend software entities without actually modifying them. This is a crucial principle of SOLID and continuous integration. We usually apply this principle to OOP and we should apply it in every day javascript programming as well. The following video starting from the definition of this principle, identifies some common code smell and puts it into practice with a javascript example
https://youtu.be/t7RgyY9OOd0

Post a Comment