0 votes
in JAVA by
Give me an example of design pattern which is based upon open closed principle?

1 Answer

0 votes
by

This is one of the practical questions I ask experienced Java programmer. I expect them to know about OOP design principles as well as patterns. Open closed design principle asserts that your code should be open for extension but closed for modification. Which means if you want to add new functionality, you can add it easily using the new code but without touching already tried and tested code.  There are several design patterns which are based upon open closed design principle e.g. Strategy pattern if you need a new strategy, just implement the interface and configure, no need to modify core logic. One working example is Collections.sort() method which is based on Strategy pattern and follows the open-closed principle, you don't modify sort() method to sort a new object, what you do is just implement Comparator in your own way.

Related questions

0 votes
asked May 3, 2022 in Cyber Security by sharadyadav1986
0 votes
asked Jan 27, 2020 in JAVA by rahuljain1
...