{"id":1893,"date":"2020-08-19T10:15:01","date_gmt":"2020-08-19T08:15:01","guid":{"rendered":"http:\/\/web.evertop.pl\/understanding-solid-principles-open-closed\/"},"modified":"2020-10-23T20:58:28","modified_gmt":"2020-10-23T18:58:28","slug":"understanding-solid-principles-open-closed","status":"publish","type":"post","link":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/","title":{"rendered":"Understanding SOLID Principles: Open-Closed"},"content":{"rendered":"<p>That is the second article from the series about SOLID principles. This time I should give you more details about the Open-Closed Principle. The element of software should be locked for modification but open to extension. Uncle Bob himself used to say: \u201cGood architecture reduces the amount of modified code to the absolute minimum. Ideally to zero\u201d.<\/p>\n<h3>Major errors made during system expansion<\/h3>\n<p>Thanks to SRP we know how to divide the system into smaller parts that can be easily extended. Easily? But how? Let\u2019s imagine that Payroll Department wants to extend the PayCalculator class mentioned in the <a href=\"https:\/\/www.web.evertop.pl\/understanding-solid-principles-single-responsibility\/\" target=\"_blank\" rel=\"noreferrer noopener\">previous article<\/a>\u00a0by implementing other methods of calculation. The basic form of calculation was hour-based calculation. The next step was adding the bonus calculation for sales department employees. In the next stage calculation for drivers and B2B co-workers was added changing at the same time the name of original method into \u2018fullTimepay()\u2019.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1830\" src=\"http:\/\/web.evertop.pl\/wp-content\/uploads\/2020\/09\/1-2.png\" alt=\"\" width=\"170\" height=\"110\" \/><br \/>\n<img loading=\"lazy\" class=\"aligncenter size-full wp-image-1831\" src=\"http:\/\/web.evertop.pl\/wp-content\/uploads\/2020\/09\/pyt.png\" alt=\"\" width=\"26\" height=\"47\" \/><br \/>\n<img loading=\"lazy\" class=\"aligncenter size-full wp-image-1832\" src=\"http:\/\/web.evertop.pl\/wp-content\/uploads\/2020\/09\/2-1.png\" alt=\"\" width=\"200\" height=\"120\" \/><br \/>\n<img loading=\"lazy\" class=\"aligncenter size-full wp-image-1831\" src=\"http:\/\/web.evertop.pl\/wp-content\/uploads\/2020\/09\/pyt.png\" alt=\"\" width=\"26\" height=\"47\" \/><br \/>\n<img loading=\"lazy\" class=\"aligncenter size-full wp-image-1833\" src=\"http:\/\/web.evertop.pl\/wp-content\/uploads\/2020\/09\/3-1.png\" alt=\"\" width=\"200\" height=\"148\" \/><\/p>\n<p>Is such a development of the system correct? According to SOLIDS\u2019s first principle everything seems to be well-defined \u2013 our class has only one actor \u2013 Payroll Department, and therefore \u2013 one source of changes. It turns out however that the complexity of choosing the right method of wages calculation more and more increases cyclomatic complexity. This method results in the necessity of making modifications in the class with every single change introduced. And what\u2019s worse, there will be a need for \u201cexternal\u201d changes for other modules\/classes to be able to use new methods.<\/p>\n<p>According to Robert C. Martin\u2019s recommendations the number of changes in the class and related code elements are far too high.<\/p>\n<h3>Proper development<\/h3>\n<p>Looking at the tasks that are carried out by the class it can be easily noticed that its main purpose is to calculate the wages of the employees by means of different algorithms. It is worth to pay attention to the fact that each algorithm is independent from others and there is a high probability of the occurrence of another algorithm (but it doesn\u2019t really mean that such one must appear).<\/p>\n<p>We use inheritance and polymorphism in such a scenario. While creating the frame of the class \u2013 the abstract class for instance \u2013 and its further implementations, we\u2019ll obtain the following class scheme:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1834\" src=\"http:\/\/web.evertop.pl\/wp-content\/uploads\/2020\/09\/4-1.png\" alt=\"\" width=\"486\" height=\"220\" srcset=\"https:\/\/www.evertop.pl\/wp-content\/uploads\/2020\/09\/4-1.png 486w, https:\/\/www.evertop.pl\/wp-content\/uploads\/2020\/09\/4-1-300x136.png 300w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/p>\n<p>The above scheme allows to achieve a very important feature. If we use the \u2018factory model\u2019 in the process of creating new classes inherited from PayCalculator it will become the only place in the system where the changes, expanding the system and its new functions, can be implemented. The classes related to payroll calculations will be blocked for any modifications and open to extensions. Furthermore, the system can be extended also with the classes that will not inherit straight from PayCalculator.<\/p>\n<h3>A bit more about architecture<\/h3>\n<p>While using the OCP in designing the payroll calculation we will have to create certain abstraction, a base class. It will be fixed and invariable during the whole life cycle of the system. There might be many different implementations but the said class would remain invariable.<\/p>\n<p>The other modules in the system will depend on that invariable abstract. It will positively affect the stability and reliability of the system. It will be easier for testing (e.g. unit testing), and the need of introducing the changes will not be a threat to anyone.<\/p>\n<h3>Summary<\/h3>\n<p>The essence of OCP is to use the simplicity of its steering components whilst making the class expansion. Using the polymorphism and virtual methods, programming increases the functionality of the systems without the negative impact on the clarity and integrity of the classes. It all involves the use of best patterns and practices. It is worth mentioning that this process affects positively the whole architecture of the system, which becomes more resistant to changes, whilst the modules become more independent. And the cyclomatic complexity decreases.<\/p>\n<p>&nbsp;<\/p>\n<p>Other SOLID principles:<br \/>\n<a href=\"http:\/\/web.evertop.pl\/understanding-solid-principles-single-responsibility\/\">Single Responsibility<\/a><br \/>\n<a href=\"http:\/\/web.evertop.pl\/understanding-solid-principles-liskov-substitution\/\">Liskov Substitution<\/a><br \/>\n<a href=\"http:\/\/web.evertop.pl\/understanding-solid-principles-interface-segregation\/\">Interface Segregation<\/a><br \/>\n<a href=\"http:\/\/web.evertop.pl\/understanding-solid-principles-dependency-inversion\/\">Dependency Inversion<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>That is the second article from the series about SOLID principles. This time I should give you more details about the Open-Closed Principle. The element of software should be locked for modification but open to extension. Uncle Bob himself used to say: \u201cGood architecture reduces the amount of modified code to the absolute minimum. Ideally [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1829,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[23,24,25],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding SOLID Principles: Open-Closed - Evertop<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/\" \/>\n<meta property=\"og:locale\" content=\"nb_NO\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding SOLID Principles: Open-Closed - Evertop\" \/>\n<meta property=\"og:description\" content=\"That is the second article from the series about SOLID principles. This time I should give you more details about the Open-Closed Principle. The element of software should be locked for modification but open to extension. Uncle Bob himself used to say: \u201cGood architecture reduces the amount of modified code to the absolute minimum. Ideally [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"Evertop\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/EvertopPoland\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-19T08:15:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-10-23T18:58:28+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/web.evertop.pl\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1707\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Skrevet av\" \/>\n\t<meta name=\"twitter:data1\" content=\"lpabian\" \/>\n\t<meta name=\"twitter:label2\" content=\"Ansl. lesetid\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutter\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.evertop.pl\/#organization\",\"name\":\"Evertop\",\"url\":\"https:\/\/www.evertop.pl\/\",\"sameAs\":[\"https:\/\/www.facebook.com\/EvertopPoland\/\",\"https:\/\/www.linkedin.com\/company\/evertop-software-development\/\"],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.evertop.pl\/#logo\",\"inLanguage\":\"nb-NO\",\"url\":\"https:\/\/www.evertop.pl\/wp-content\/uploads\/2021\/04\/logo_new.png\",\"contentUrl\":\"https:\/\/www.evertop.pl\/wp-content\/uploads\/2021\/04\/logo_new.png\",\"width\":582,\"height\":114,\"caption\":\"Evertop\"},\"image\":{\"@id\":\"https:\/\/www.evertop.pl\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.evertop.pl\/#website\",\"url\":\"https:\/\/www.evertop.pl\/\",\"name\":\"Evertop\",\"description\":\"we code the future\",\"publisher\":{\"@id\":\"https:\/\/www.evertop.pl\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.evertop.pl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"nb-NO\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#primaryimage\",\"inLanguage\":\"nb-NO\",\"url\":\"https:\/\/www.evertop.pl\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg\",\"contentUrl\":\"https:\/\/www.evertop.pl\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg\",\"width\":2560,\"height\":1707},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#webpage\",\"url\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/\",\"name\":\"Understanding SOLID Principles: Open-Closed - Evertop\",\"isPartOf\":{\"@id\":\"https:\/\/www.evertop.pl\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#primaryimage\"},\"datePublished\":\"2020-08-19T08:15:01+00:00\",\"dateModified\":\"2020-10-23T18:58:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#breadcrumb\"},\"inLanguage\":\"nb-NO\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Strona g\\u0142\\u00f3wna\",\"item\":\"https:\/\/www.evertop.pl\/no\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understanding SOLID Principles: Open-Closed\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#webpage\"},\"author\":{\"@id\":\"https:\/\/www.evertop.pl\/#\/schema\/person\/7fa9c3dddc7eabfad26b496cb9a97628\"},\"headline\":\"Understanding SOLID Principles: Open-Closed\",\"datePublished\":\"2020-08-19T08:15:01+00:00\",\"dateModified\":\"2020-10-23T18:58:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#webpage\"},\"wordCount\":655,\"publisher\":{\"@id\":\"https:\/\/www.evertop.pl\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.evertop.pl\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg\",\"keywords\":[\"programming tips\",\"self development\",\"SOLID\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"nb-NO\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.evertop.pl\/#\/schema\/person\/7fa9c3dddc7eabfad26b496cb9a97628\",\"name\":\"lpabian\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.evertop.pl\/#personlogo\",\"inLanguage\":\"nb-NO\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/330a0a7408b1711d2acb1b0cb39b7e2d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/330a0a7408b1711d2acb1b0cb39b7e2d?s=96&d=mm&r=g\",\"caption\":\"lpabian\"},\"sameAs\":[\"http:\/\/web.evertop.pl\"],\"url\":\"https:\/\/www.evertop.pl\/no\/author\/lpabian\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Understanding SOLID Principles: Open-Closed - Evertop","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/","og_locale":"nb_NO","og_type":"article","og_title":"Understanding SOLID Principles: Open-Closed - Evertop","og_description":"That is the second article from the series about SOLID principles. This time I should give you more details about the Open-Closed Principle. The element of software should be locked for modification but open to extension. Uncle Bob himself used to say: \u201cGood architecture reduces the amount of modified code to the absolute minimum. Ideally [&hellip;]","og_url":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/","og_site_name":"Evertop","article_publisher":"https:\/\/www.facebook.com\/EvertopPoland\/","article_published_time":"2020-08-19T08:15:01+00:00","article_modified_time":"2020-10-23T18:58:28+00:00","og_image":[{"width":2560,"height":1707,"url":"http:\/\/web.evertop.pl\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg","path":"\/home\/evertop\/web-evertop\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg","size":"full","id":1829,"alt":"","pixels":4369920,"type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Skrevet av":"lpabian","Ansl. lesetid":"3 minutter"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/www.evertop.pl\/#organization","name":"Evertop","url":"https:\/\/www.evertop.pl\/","sameAs":["https:\/\/www.facebook.com\/EvertopPoland\/","https:\/\/www.linkedin.com\/company\/evertop-software-development\/"],"logo":{"@type":"ImageObject","@id":"https:\/\/www.evertop.pl\/#logo","inLanguage":"nb-NO","url":"https:\/\/www.evertop.pl\/wp-content\/uploads\/2021\/04\/logo_new.png","contentUrl":"https:\/\/www.evertop.pl\/wp-content\/uploads\/2021\/04\/logo_new.png","width":582,"height":114,"caption":"Evertop"},"image":{"@id":"https:\/\/www.evertop.pl\/#logo"}},{"@type":"WebSite","@id":"https:\/\/www.evertop.pl\/#website","url":"https:\/\/www.evertop.pl\/","name":"Evertop","description":"we code the future","publisher":{"@id":"https:\/\/www.evertop.pl\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.evertop.pl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"nb-NO"},{"@type":"ImageObject","@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#primaryimage","inLanguage":"nb-NO","url":"https:\/\/www.evertop.pl\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg","contentUrl":"https:\/\/www.evertop.pl\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg","width":2560,"height":1707},{"@type":"WebPage","@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#webpage","url":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/","name":"Understanding SOLID Principles: Open-Closed - Evertop","isPartOf":{"@id":"https:\/\/www.evertop.pl\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#primaryimage"},"datePublished":"2020-08-19T08:15:01+00:00","dateModified":"2020-10-23T18:58:28+00:00","breadcrumb":{"@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#breadcrumb"},"inLanguage":"nb-NO","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Strona g\u0142\u00f3wna","item":"https:\/\/www.evertop.pl\/no\/"},{"@type":"ListItem","position":2,"name":"Understanding SOLID Principles: Open-Closed"}]},{"@type":"Article","@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#article","isPartOf":{"@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#webpage"},"author":{"@id":"https:\/\/www.evertop.pl\/#\/schema\/person\/7fa9c3dddc7eabfad26b496cb9a97628"},"headline":"Understanding SOLID Principles: Open-Closed","datePublished":"2020-08-19T08:15:01+00:00","dateModified":"2020-10-23T18:58:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#webpage"},"wordCount":655,"publisher":{"@id":"https:\/\/www.evertop.pl\/#organization"},"image":{"@id":"https:\/\/www.evertop.pl\/no\/understanding-solid-principles-open-closed\/#primaryimage"},"thumbnailUrl":"https:\/\/www.evertop.pl\/wp-content\/uploads\/2020\/09\/pexels-joshua-miranda-4027658-scaled-1.jpg","keywords":["programming tips","self development","SOLID"],"articleSection":["Blog"],"inLanguage":"nb-NO"},{"@type":"Person","@id":"https:\/\/www.evertop.pl\/#\/schema\/person\/7fa9c3dddc7eabfad26b496cb9a97628","name":"lpabian","image":{"@type":"ImageObject","@id":"https:\/\/www.evertop.pl\/#personlogo","inLanguage":"nb-NO","url":"https:\/\/secure.gravatar.com\/avatar\/330a0a7408b1711d2acb1b0cb39b7e2d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/330a0a7408b1711d2acb1b0cb39b7e2d?s=96&d=mm&r=g","caption":"lpabian"},"sameAs":["http:\/\/web.evertop.pl"],"url":"https:\/\/www.evertop.pl\/no\/author\/lpabian\/"}]}},"_links":{"self":[{"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/posts\/1893"}],"collection":[{"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/comments?post=1893"}],"version-history":[{"count":2,"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/posts\/1893\/revisions"}],"predecessor-version":[{"id":2169,"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/posts\/1893\/revisions\/2169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/media\/1829"}],"wp:attachment":[{"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/media?parent=1893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/categories?post=1893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evertop.pl\/no\/wp-json\/wp\/v2\/tags?post=1893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}