{"id":2619,"date":"2024-10-08T21:57:08","date_gmt":"2024-10-08T21:57:08","guid":{"rendered":"https:\/\/sgrsoftwaresolution.com\/us\/?p=2619"},"modified":"2024-10-08T21:57:10","modified_gmt":"2024-10-08T21:57:10","slug":"solid-with-a-simple-example","status":"publish","type":"post","link":"https:\/\/www.sgrsoftwaresolution.com\/us\/solid-with-a-simple-example\/","title":{"rendered":"&#8220;SOLID&#8221; with a Simple Example"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>&#8220;SOLID&#8221;<\/strong>&nbsp;are five basic principles that help to create good software architecture.&nbsp;<strong>&#8220;SOLID&#8221;<\/strong>&nbsp;is an acronym.<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>S &#8211; SRP (Single responsibility principle)<\/li>\n\n\n\n<li>O &#8211; OCP (Open closed principle)<\/li>\n\n\n\n<li>L &#8211; LSP (Liskov substitution principle)<\/li>\n\n\n\n<li>I &#8211; ISP (Interface segregation principle)<\/li>\n\n\n\n<li>D &#8211; DIP (Dependency inversion principle)<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/solid.png\" alt=\"service app development\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Single Responsibility Principle (SRP)<\/h3>\n\n\n\n<p>SRP says that classes or modules should have only one responsibility and not multiple.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>We can notice that in below code, the Add method has too many responsibilities, where it will have an add method to DB and error handling logic. This violates the single responsibility principle.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/Problem1.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>Abstracting functionality of error logging so we no longer violate the single responisbility principle<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/solution1.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Open Closed Principle (OCP)<\/h3>\n\n\n\n<p>OCP says that software entities (classed, modules, methods, etc) should be open for extensions, but closed for modification.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>We can notice in below code that it is violating the Open Closed Principle, because if we need to add another company (like big basket) we need to modify the existing code in the switch statement<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/Problem2.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>By creating inheritance, we make it easy to modify by adding a derived class without touching the existing class<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/solution2.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Liskov substitution principle (LSP)<\/h3>\n\n\n\n<p>LSP says that a parent class should be able to refer child objects seamlessly during runtime polymorphism<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>We can notice in the below code, for COD (Cash on deliver) transaction there is no need to check the balanace method\/feature and deduct an amount method\/feature, but the client is still using it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/Problem3.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>As a COD (Cash on deliver) transaction is only needed to do a transaction, there&#8217;s no need to check the balance method\/feature and deduct the amount method\/feature. We can use the interface and implement it based on the scenario.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/solution3.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Interface segragation principle (ISP)<\/h3>\n\n\n\n<p>ISP says show only the methods to the client which they need, i.e., no client should be forced to depend on methods they do not use.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>We can notice in below code, as we add more functionalities (i.e. read method) all the client should use is read method, if it&#8217;s not required.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/Problem4.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>By creating another interface and extending from it, we can avoid violating the above scenario.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/solution4.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Dependency inversion principle (DIP)<\/h3>\n\n\n\n<p>DIP says that high-level modules should not depend on low-level modules, but rather should depend on abstraction.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Problem<\/h4>\n\n\n\n<p>We can notice that in the below code, in case if we want to change FileLogger to EmailLogger method\/feature, we have to modify it in the Customer class, thus it was violating this principle.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/Problem5.png\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Solution<\/h4>\n\n\n\n<p>By injecting any dependencies of a class through the class constructor as an input parameter.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.sgrsoftwaresolution.com\/images\/blog\/solution5.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p>In this article, we have learned simple examples of the SOLID principles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;SOLID&#8221;&nbsp;are five basic principles that help to create good software architecture.&nbsp;&#8220;SOLID&#8221;&nbsp;is an acronym. Single Responsibility Principle (SRP) SRP says that classes or modules should have only one responsibility and not multiple. Problem We can notice that in below code, the Add method has too many responsibilities, where it will have an add method to DB [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2620,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[33],"tags":[34],"class_list":["post-2619","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-technology"],"_links":{"self":[{"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/posts\/2619"}],"collection":[{"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/comments?post=2619"}],"version-history":[{"count":1,"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/posts\/2619\/revisions"}],"predecessor-version":[{"id":2621,"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/posts\/2619\/revisions\/2621"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/media\/2620"}],"wp:attachment":[{"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/media?parent=2619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/categories?post=2619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sgrsoftwaresolution.com\/us\/wp-json\/wp\/v2\/tags?post=2619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}