{"id":446,"date":"2020-08-11T17:08:13","date_gmt":"2020-08-11T11:38:13","guid":{"rendered":"https:\/\/saurabhjain1537.wordpress.com\/?p=446"},"modified":"2025-02-27T07:46:42","modified_gmt":"2025-02-27T07:46:42","slug":"solid-principles-open-closed","status":"publish","type":"post","link":"https:\/\/saurabhjain.dev\/?p=446","title":{"rendered":"SOLID Principles: Open-Closed"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>The Open-Closed Principle &#8220;O&#8221;<\/strong><\/p>\n\n\n\n<p>&#8220;software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification&#8221;. <\/p>\n\n\n\n<p>This principle advocates Inheritance and Polymorphism. The way to achieve this contradictory suggestion is to design loosely coupled classes which bind together using Interfaces or Abstraction.<\/p>\n\n\n\n<p>Functionality can be extended by Inheriting a class OR by providing concrete implementation for an Interface or Abstract class. These can be done without changing anything in the existing classes; in fact  existing classes can be in separate library.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Consider this class diagram for Logger:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"387\" height=\"293\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2020\/08\/01-srp-02-logger.png?w=387\" alt=\"\" class=\"wp-image-436\" \/><figcaption>Figure 1<\/figcaption><\/figure><\/div>\n\n\n\n<p>The class <strong>LogManager <\/strong>is defying Open-Closed principle by containing functionalities for <em>logToFile()<\/em> and <em>logToDB()<\/em>. In future, there may be a requirement to log messages using webservice call. This new functionality would require changes in existing <strong>LogManager<\/strong> class; but it&#8217;s bad design if an existing class is modified to add new functionality.<\/p>\n\n\n\n<p>A good design is to have separate classes for logging to File and DB:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"503\" height=\"333\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2020\/08\/02-ocp-01-logger.png?w=503\" alt=\"\" class=\"wp-image-464\" \/><figcaption>Figure 2<\/figcaption><\/figure><\/div>\n\n\n\n<p>The <strong>LogManager<\/strong> class doesn&#8217;t know how to write a String and it depends on derived classes for writing string. It has <em>log(Object)<\/em> method to serialize object into string and <em>log(String)<\/em> abstract method for concrete implementation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public void log(Object obj) {\n\n      String message = serializer.serialize(obj);\n\n     log(message);\n\n    }\n\n    public abstract void log(String msg); <\/code><\/pre>\n\n\n\n<p>With this design, any new type of logger like web-service or message queue can be added by providing another concrete implementation of <strong>LogManager<\/strong>. It will not require any change in the existing classes.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>The Open-Closed principle helps in Object Oriented class designing. It enforces Abstraction and increases code reusability. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Open-Closed Principle &#8220;O&#8221; &#8220;software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification&#8221;. This principle advocates Inheritance and Polymorphism. The way to achieve this contradictory suggestion is to design loosely coupled classes which bind together using Interfaces or Abstraction. Functionality can be extended by Inheriting a class OR by &hellip; <a href=\"https:\/\/saurabhjain.dev\/?p=446\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SOLID Principles: Open-Closed<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[36,45,46,50,55],"class_list":["post-446","post","type-post","status-publish","format-standard","hentry","category-design","tag-loose-coupling","tag-open-closed","tag-open-closed-principle","tag-reusability","tag-solid-principles"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=446"}],"version-history":[{"count":1,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":781,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/446\/revisions\/781"}],"wp:attachment":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}