Skip to main content

Posts

Showing posts with the label open close

Open Close Principle with example in Java

Open close principle Bertrand Meyer coined the term open close principle in his 1988 book Object Oriented Software Construction . Software entities like classes, modules and functions should be " open for extension but closed for modifications ". It is a generic principle. You can consider it when writing your classes to make sure that when you need to extend their behaviour you don't have to change the class but to extend it. When referring to the classes Open Close principle can be ensured by the use of Abstract classes and/ or Interfaces and concrete classes implementing their behaviour. This will enforce having concrete classes implementing Abstract classes/ Interfaces instead of changing them.  Some particular cases where this principle is used are Template Design Pattern, Strategy design pattern . We want to draw different kind of images. For this, We wrote a generic class ImageEditer which can draw shapes. See the below code snippet. package javawi