
{"id":1832,"date":"2016-06-17T10:09:15","date_gmt":"2016-06-17T10:09:15","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=1832"},"modified":"2018-04-04T13:30:56","modified_gmt":"2018-04-04T13:30:56","slug":"the-gof-patterns-resolve-some-oop-drawbacks-the-abstract-factory-case-study","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=1832","title":{"rendered":"The GOF patterns resolve some OOP drawbacks: The Abstract Factory case study"},"content":{"rendered":"<p style=\"color: #555555;\">The majority of developers have already heard about the design patterns,\u00a0<a style=\"color: #2970a6;\" href=\"http:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">GOF(Gang Of Four)<\/a>\u00a0patterns are the most popularized, and each developer has his way to learn them , we can enumerate:<\/p>\n<ul style=\"color: #555555;\">\n<li>Reading a book.<\/li>\n<li>From web sites.<\/li>\n<li>From a collegue.<\/li>\n<li>Doing a training.<\/li>\n<\/ul>\n<p style=\"color: #555555;\"><span id=\"more-741\"><\/span>Regardless of the method chose, we can learn by heart the patterns and spent hours to memorize their UML diagrams, but sometimes when we need to use them in a real project, it becomes more problematic.<!--more--><span id=\"more-720\"><\/span><\/p>\n<p style=\"color: #333333;\"><span style=\"color: #555555;\">What\u2019s very important is not to know exactly the pattern names and how to implement them, but what\u2019s more relevant is the motivation\u00a0behind each pattern,\u00a0it\u2019s from motivations that we invent the patterns.<\/span><\/p>\n<p style=\"color: #333333;\"><strong>OOP Issues<\/strong><\/p>\n<p style=\"color: #333333;\">As Bjarne Stroustrup points out, \u201cC++ is a multi-paradigmed language.\u201d It supports many different styles of programs, or paradigms, and object-oriented programming is only one of these. Some of the others are structured programming, and generic programming. In the last few years C++ experts\u00a0like Andrei Alexandrescu, Scott Meyers and Herb Sutter promotes the uses of the generic programming and they qualify it as Modern C++ Design.<\/p>\n<p style=\"color: #333333;\">In the C++ world \u00a0some developers are anti OOP, others hate the generic programming and some of them mix between them.<\/p>\n<p style=\"color: #333333;\">Here are some assertions from the STL creator\u00a0<span style=\"color: #212324;\">Alexander Stepanov:<\/span><\/p>\n<blockquote>\n<p style=\"color: #212324;\">&#8220;I find OOP technically unsound.&#8221;<\/p>\n<p style=\"color: #212324;\">&#8220;I find OOP philosophically unsound.&#8221;<\/p>\n<p style=\"color: #212324;\">&#8220;I find OOP methodologically wrong.&#8221;<\/p>\n<\/blockquote>\n<p style=\"color: #212324;\"><em>What&#8217;s really the big issue of the OOP approach?<\/em><\/p>\n<p style=\"color: #212324;\">Maybe the big drawback of using the OOP approach is that you introduce easilly a high coupling between your classes and methods. Think about the inheritence which is overused in OOP, it introduces a very high coupling. However even the composition introduces a high coupling. To discover that let\u2019s compare the implementation of a class calculating a tax using\u00a0\u00a0OOP and generic programming.<\/p>\n<p style=\"color: #333333;\"><em>OOP implemenation<\/em><\/p>\n<p style=\"color: #333333;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-287\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics1.png\" alt=\"generics1\" width=\"475\" height=\"332\" \/><\/a><\/p>\n<p style=\"color: #333333;\">What the CTaxCalculator class implementation tell us exactly?<\/p>\n<p style=\"color: #333333;\">I\u2019m the class CTaxCalculator, I know how to calculate the tax but I collaborate only with classes of ICalculator\u00a0kind. I refuse to colaborate with any other not ICalculator class even if it can help me to calculate the Tax.<\/p>\n<p style=\"color: #333333;\"><em>Generic implementation<\/em><\/p>\n<p style=\"color: #333333;\"><a style=\"color: #ff4b33;\" href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-288\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/generics2.png\" alt=\"generics2\" width=\"479\" height=\"270\" \/><\/a><\/p>\n<p style=\"color: #333333;\">The CGenericTaxCalculator class in the other side know how to calculate the tax , and for that it can collaborate with any type capable to calculate the tax, and it\u2019s \u00a0not aware about its kind.<\/p>\n<p style=\"color: #333333;\"><strong>The GOF patterns to the rescue:<\/strong><\/p>\n<p style=\"color: #333333;\">Do you asked yourself why almost all the GOF patterns introduce the abstract classes and the interfaces?\u00a0The short answer is to fight the <strong>high coupling <\/strong>introduced by the OOP approach.<\/p>\n<p style=\"color: #333333;\">The goal of the GOF patterns is not only to improve the design quality of your code base, but also to resolve some OOP drawbacks.<\/p>\n<p style=\"color: #333333;\"><strong>The Abstract factory case study:<\/strong><\/p>\n<p>Let&#8217;s discover the benefits of using the abstact factory pattern and \u00a0let&#8217;s start by its \u00a0common definition :<\/p>\n<blockquote><p><span style=\"color: #333333;\">The Abstract Factory Pattern provides an interface for creating families of related objects without specifying their concrete classes. The Abstract Pattern makes sure that the client does not know anything about the objects being created. The pattern hides the implementation of the product definitions and their class names and the only way the client can create a product is through the factory.<\/span><\/p><\/blockquote>\n<p>But what&#8217;s the role of this pattern in a concrete code base, it&#8217;s worth to use it? or it just complicate the code more than it makes it more readable and maintainable.<\/p>\n<p>In this post we will try to discover visually \u00a0its benefits using\u00a0the dependency structure matrix \u00a0and the dependency graph.<span style=\"color: #333333;\">\u00a0For that we analyze the following\u00a0<\/span><a style=\"color: #999999;\" href=\"http:\/\/sourcemaking.com\/design_patterns\/abstract_factory\/cpp\/1\">example<\/a>\u00a0where\u00a0we have a code source before and after the refactoring to introduce the abstract factory pattern.<\/p>\n<p><a href=\"http:\/\/www.cppdepend.com\">CppDepend <\/a>provides an interesting feature\u00a0to compare between two version, in our case we compare the project before and after the refactoring and here&#8217;s\u00a0the result.<\/p>\n<div style=\"color: #333333;\"><a style=\"color: #999999;\" href=\"http:\/\/1.bp.blogspot.com\/_tWDA5bNBNHI\/SrTNgPeSWNI\/AAAAAAAAAKE\/VOyZXDFkB_w\/s1600-h\/af10.PNG\"><img loading=\"lazy\" decoding=\"async\" id=\"BLOGGER_PHOTO_ID_5383153408407656658\" src=\"http:\/\/1.bp.blogspot.com\/_tWDA5bNBNHI\/SrTNgPeSWNI\/AAAAAAAAAKE\/VOyZXDFkB_w\/s800\/af10.PNG\" alt=\"\" width=\"666\" height=\"394\" border=\"0\" \/><\/a><\/div>\n<p>After introducing the pattern three classes were added(Factory,MotifFactory an WindowsFactory), they are\u00a0<span style=\"color: #333333;\">characterized by the Bold Style in the class browser.<\/span><\/p>\n<p><span style=\"color: #333333;\">There\u00a0are\u00a0also some \u00a0changes concerning the dependencies between the classes as we can see \u00a0in the dependency matrix.<\/span><\/p>\n<p>Until now the result is that we have more lines of code and more classes when introducing the pattern.<\/p>\n<p><strong>The Pattern Benefits<\/strong><\/p>\n<p><span style=\"color: #3366ff;\">Where the WindowsButton class is created?<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">To answer to this question let\u2019s execute the following CQL request to quey the code base:<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #3333ff;\">SELECT METHODS WHERE<\/span><span style=\"color: #333333;\">\u00a0CreateA &#8220;<\/span><span style=\"color: #ff0000;\">WindowsButton<\/span><span style=\"color: #333333;\">&#8221;\u00a0<\/span><span style=\"color: #3333ff;\">ORDER BY<\/span><span style=\"color: #333333;\">\u00a0DepthOfCreateA<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">And here are the methods creating it before the refactoring:<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><\/p>\n<div style=\"color: #333333;\"><a style=\"color: #999999;\" href=\"http:\/\/3.bp.blogspot.com\/_tWDA5bNBNHI\/SrTOyDsEClI\/AAAAAAAAAKM\/gWmmoRoCqpw\/s1600-h\/af1.PNG\"><img decoding=\"async\" id=\"BLOGGER_PHOTO_ID_5383154813993486930\" src=\"http:\/\/3.bp.blogspot.com\/_tWDA5bNBNHI\/SrTOyDsEClI\/AAAAAAAAAKM\/gWmmoRoCqpw\/s800\/af1.PNG\" alt=\"\" border=\"0\" \/><\/a><\/div>\n<p><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">And the ones creating it after introducing the pattern:<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><\/p>\n<div style=\"color: #333333;\"><a style=\"color: #999999;\" href=\"http:\/\/4.bp.blogspot.com\/_tWDA5bNBNHI\/SrTPAA4ByUI\/AAAAAAAAAKU\/d17v_AxKfpU\/s1600-h\/af2.PNG\"><img decoding=\"async\" id=\"BLOGGER_PHOTO_ID_5383155053756533058\" src=\"http:\/\/4.bp.blogspot.com\/_tWDA5bNBNHI\/SrTPAA4ByUI\/AAAAAAAAAKU\/d17v_AxKfpU\/s800\/af2.PNG\" alt=\"\" border=\"0\" \/><\/a><\/div>\n<p><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">Before the refactoring, the class is created in many places, but after the refactoring the creation is isolated in WindowsFactory.<\/span><\/p>\n<p><span style=\"color: #333333;\">The responsibility of the creation is now isolated and it impact the class cohesion, so we have a high cohesion than before. Indeed the logic of the creation is now managed by a specific class, and it become easy to change this logic if we need to.<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #3366ff;\">What about\u00a0the\u00a0coupling?<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">Using the DSM \u00a0can help to detect the coupling between the projects and the classes.<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><\/p>\n<div style=\"color: #333333;\"><a style=\"color: #999999;\" href=\"http:\/\/1.bp.blogspot.com\/_tWDA5bNBNHI\/SrTP_qSoL1I\/AAAAAAAAAKc\/W3XyPHW9cgs\/s1600-h\/af3.PNG\"><img decoding=\"async\" id=\"BLOGGER_PHOTO_ID_5383156147205713746\" src=\"http:\/\/1.bp.blogspot.com\/_tWDA5bNBNHI\/SrTP_qSoL1I\/AAAAAAAAAKc\/W3XyPHW9cgs\/s800\/af3.PNG\" alt=\"\" border=\"0\" \/><\/a><\/div>\n<p><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">If you look at the intersection between the main() method and the MotifButton class we observe a red \u201c-\u201csign, what indicate tha\u00a0we lose a dependency between main() \u00a0and the other classes \u00a0WindowsButton and \u00a0MotifButton. However a dependency was introduced between the main() method and the factory classes.<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">Let\u2019s use\u00a0the dependency graph\u00a0to discover what happens to the dependencies:<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">Before the refactoring:<\/span><\/p>\n<div style=\"color: #333333;\"><a style=\"color: #999999;\" href=\"http:\/\/2.bp.blogspot.com\/_tWDA5bNBNHI\/SrTQPsgZ5QI\/AAAAAAAAAKk\/bhFv-GY3zwI\/s1600-h\/af5.PNG\"><img decoding=\"async\" id=\"BLOGGER_PHOTO_ID_5383156422678275330\" src=\"http:\/\/2.bp.blogspot.com\/_tWDA5bNBNHI\/SrTQPsgZ5QI\/AAAAAAAAAKk\/bhFv-GY3zwI\/s800\/af5.PNG\" alt=\"\" border=\"0\" \/><\/a><\/div>\n<p>The FactoyGlobalMembers which represent the global methods including the main() method uses directly the Gui widgets (Button, Menu, &#8230;)<\/p>\n<p><span style=\"color: #333333;\">And after the refactoring:<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><\/p>\n<div style=\"color: #333333;\"><a style=\"color: #999999;\" href=\"http:\/\/3.bp.blogspot.com\/_tWDA5bNBNHI\/SrTQcxkBr3I\/AAAAAAAAAKs\/qTg8MGy5V8w\/s1600-h\/af4.PNG\"><img decoding=\"async\" id=\"BLOGGER_PHOTO_ID_5383156647373942642\" src=\"http:\/\/3.bp.blogspot.com\/_tWDA5bNBNHI\/SrTQcxkBr3I\/AAAAAAAAAKs\/qTg8MGy5V8w\/s800\/af4.PNG\" alt=\"\" border=\"0\" \/><\/a><\/div>\n<p><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">The global methods are now isolated form the concretes classes used, we introuce\u00a0a low coupling so we can add another family of classes without changing the client using them, only the factory will be changed.<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"font-weight: bold; color: #333333;\">Conclusion:<\/span><br style=\"color: #333333;\" \/><br style=\"color: #333333;\" \/><span style=\"color: #333333;\">The abstract factory pattern is not only a pattern which is good to know when you have an interview. But it improves the cohesion and the coupling of your classes.\u00a0<\/span><\/p>\n<p>The GOF patterns are very useful to fight the high coupling introduced by some OOP mechanisms and the improve the design quality of your code base to make it more readable and maintainable.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The majority of developers have already heard about the design patterns,\u00a0GOF(Gang Of Four)\u00a0patterns are the most popularized, and each developer has his way to learn them , we can enumerate: Reading a book. From web sites. From a collegue. Doing a training. Regardless of the method chose, we can learn by heart the patterns and &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=1832\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;The GOF patterns resolve some OOP drawbacks: The Abstract Factory case study&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1832","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1832"}],"version-history":[{"count":15,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1832\/revisions"}],"predecessor-version":[{"id":2472,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1832\/revisions\/2472"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}