{"id":2756,"date":"2023-07-16T08:37:00","date_gmt":"2023-07-16T06:37:00","guid":{"rendered":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2756"},"modified":"2023-07-18T10:34:54","modified_gmt":"2023-07-18T08:34:54","slug":"open-close-principle","status":"publish","type":"post","link":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/?p=2756","title":{"rendered":"Open\/Close Principle"},"content":{"rendered":"<p style=\"text-align: justify;\">Under this statement hides simple definition &#8211;&nbsp;<strong>open for development \/ closed for modification<\/strong>. I would describe that principle as one of the hardest, because it requires from programmer an ability to forsee what are the chances of any problems that may occur driven in the future. It means, that every single class should be precisely thought before to provide new functionalities, without any modifications &#8211; they are forbidden, because of one crucial thing &#8211; any change of declaration may occur as a crash program result. Lets focus on code written below:<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Square\n{\n    public int A { get; set; }\n}\n\npublic class Rectangle\n{\n    public int A { get; set; }\n    public int B { get; set; }\n}\n\npublic class Calculator\n{\n    public int Area(object shape)\n    {\n        if (shape is Square square)\n        {\n            return square.A * square.A;\n        }\n        else if (shape is Rectangle rectangle)\n        {\n            return rectangle.A * rectangle.B;\n        }\n\n        return 0;\n    }\n}\n\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">Ok &#8211; our code looks great &#8211; we have three different classes, and each class has its own responsibility. But there is a catch. What if we wanted to add another class figure called <span style=\"background-color: #efefef;\">Circle<\/span>? We would have to break the principle, and modify our <span style=\"background-color: #efefef;\">Calculator<\/span> class for another <span style=\"background-color: #efefef;\">else if<\/span> statement, which obviously would result as more lines of code. Thanks to benefits which we owe to OOP, there is a possibility to use polymorphism &#8211; a possibility for objects of various classes to be treated as objects of the common base class.&nbsp;<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ created abstract class \npublic abstract class Shape\n{\n    public abstract int Area();\n}\n\/\/ class Square inherits from Shape class\npublic class Square : Shape\n{\n    public int A { get; set; }\n\n    public override int Area()\n    {\n        return A * A;\n    }\n}\n\/\/ class Rectangle inherits from Shape class\npublic class Rectangle : Shape\n{\n    public int A { get; set; }\n    public int B { get; set; }\n\n    public override int Area()\n    {\n        return A * B;\n    }\n}\n\/\/Calculator class provides a possibility to calculate any figure's Area\npublic class Calculator\n{\n    public int Area(Shape shape)\n    {\n        return shape.Area();\n    }\n}\n\n<\/pre><\/div>\n\n<p style=\"text-align: justify;\">This principle is very common to see while using design patterns, especially factory or strategy, which I&#8217;m discussing <strong>here<\/strong>. Thanks to this mechanism, there is a very simple way in the future to develop our code. I would like to point out, that we have applied a little bit another mechanism, fulfills dependency inversion principle, described&nbsp;<strong>here.<\/strong><\/p>","protected":false},"excerpt":{"rendered":"<p>Second principle, stating that code should be open to rebuild and closed to modifications<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[1,13],"tags":[],"class_list":["post-2756","post","type-post","status-publish","format-standard","hentry","category-bez-kategorii","category-solid-articles"],"_links":{"self":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2756"}],"version-history":[{"count":15,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2756\/revisions"}],"predecessor-version":[{"id":2834,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/2756\/revisions\/2834"}],"wp:attachment":[{"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/radoslaw-gucwa.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}