
{"id":578,"date":"2014-11-07T15:38:02","date_gmt":"2014-11-07T15:38:02","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=578"},"modified":"2014-11-07T15:39:15","modified_gmt":"2014-11-07T15:39:15","slug":"detect-not-well-implemented-design-patterns-in-c-automatically","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=578","title":{"rendered":"Detect  not well implemented design patterns in C++  automatically"},"content":{"rendered":"<p style=\"color: #666666;\">Design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects. Some of them are very popular like singleton, factory and strategy.\u00a0 Others are not widely used like flyweight.<\/p>\n<p style=\"color: #666666;\">Sometimes the patterns are not well implemented by developers. This\u00a0could generates some design issues and affect the benefits of using them. It&#8217;s interesting to detect where they are not well implemented, and correct their implementation.<!--more--><\/p>\n<p style=\"color: #666666;\">To detect these kinds of issues we need a maximum of information about the source code including:<\/p>\n<ul>\n<li><span style=\"color: #666666;\">Attributes of classes, methods and fields.\u00a0<\/span><\/li>\n<li><span style=\"color: #666666;\">The inheritance between classes.<\/span><\/li>\n<li><span style=\"color: #666666;\">Dependencies between classes, methods and fields.<\/span><\/li>\n<li><span style=\"color: #666666;\">Where classes are instantiated.<\/span><\/li>\n<\/ul>\n<p style=\"color: #666666;\"><a href=\"http:\/\/www.cppdepend.com\">CppDepend<\/a> generates a code model which contains all these data and permits to query them using <a href=\"http:\/\/cppdepend.com\/Doc_CQLinq_Features.aspx\">CQLinq<\/a>. Let&#8217;s try to detect the misusing of two patterns: Singleton and Strategy.<\/p>\n<p style=\"color: #666666;\"><strong>Singleton<\/strong><\/p>\n<p style=\"color: #666666;\"><span style=\"color: #252525;\">The\u00a0<\/span><span style=\"font-weight: 600; color: #252525;\">singleton pattern<\/span><span style=\"color: #474747;\">\u00a0is a\u00a0design pattern that restricts the\u00a0instantiation\u00a0of a class to one\u00a0object.\u00a0<\/span><span style=\"color: #474747;\">However, using this pattern became controversial, and not all architects and designers recommend it, here\u2019s an\u00a0<\/span><a style=\"color: #2970a6;\" href=\"http:\/\/code.google.com\/p\/google-singleton-detector\/wiki\/WhySingletonsAreControversial\">article\u00a0<\/a><span style=\"color: #474747;\">talking about the singleton controversy.<\/span><\/p>\n<p style=\"color: #666666;\">A\u00a0common mistake of implementing a singleton pattern is to not make\u00a0the constructor\u00a0private.<\/p>\n<p style=\"color: #666666;\">The following query detect all classes with the same traits\u00a0as a singleton, \u00a0i.e. classes containing one static field referencing it self, a static method returning this field. And not contains a private constructor.<\/p>\n<p style=\"color: #666666;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/pattern1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-612\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/pattern1.png\" alt=\"pattern1\" width=\"695\" height=\"205\" \/><\/a><\/p>\n<p style=\"color: #666666;\">\u00a0<strong>Strategy<\/strong><\/p>\n<p style=\"color: #555555;\">There are common situations when classes differ only in their behavior. For this cases it is a good idea to isolate the algorithms in separate classes in order to have the ability to select different algorithms at runtime. The strategy pattern is a good candidate for such needs.<\/p>\n<p style=\"color: #555555;\">Here&#8217;s the UML diagram of this pattern:<\/p>\n<p style=\"color: #555555;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/Strategy_Pattern_in_UML.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-614\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/Strategy_Pattern_in_UML.png\" alt=\"Strategy_Pattern_in_UML\" width=\"256\" height=\"160\" \/><\/a><\/p>\n<p style=\"color: #555555;\">As the diagram shows, the context class uses the abstract class &#8220;Strategy&#8221; and has no knowledge about the concrete implementations. However, \u00a0for some implementations, the concrete classes are used directly by the Context one. Here&#8217;s a sample of this mistake:<\/p>\n<p style=\"color: #555555;\">\u00a0<a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/pattern3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-615\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/pattern3.png\" alt=\"pattern3\" width=\"670\" height=\"323\" \/><\/a><\/p>\n<p style=\"color: #555555;\">Let\u2019s search with\u00a0CQLinq \u00a0all classes using this\u00a0strategy pattern. For this purpose, we can search for abstract classes having multiple derived classes and where the client uses directly the methods of the \u00a0concrete implementations instead of the abstract one.<\/p>\n<p style=\"color: #555555;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/pattern2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-613\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/pattern2.png\" alt=\"pattern2\" width=\"638\" height=\"121\" \/><\/a><\/p>\n<p style=\"color: #555555;\">The result of this query will give us the methods of the concrete implementations used directly by other classes. You have just to search for methods using them to know where to correct the strategy pattern implementation.<\/p>\n<p style=\"color: #555555;\">However it will not give us exactly the\u00a0places where the strategy pattern is not well implemented , But some potential places where the problem could exist and the developer will check manually if it&#8217;s an issue or not.<\/p>\n<p style=\"color: #555555;\"><strong>Conclusion<\/strong><\/p>\n<p style=\"color: #555555;\">The design patterns improve the design quality. However, if they are not well implemented they could be a source of many issues and bugs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects. Some of them are very popular like singleton, factory and strategy.\u00a0 Others are not widely used like flyweight. Sometimes the patterns are not well implemented by developers. This\u00a0could generates &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=578\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Detect  not well implemented design patterns in C++  automatically&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-578","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/578","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=578"}],"version-history":[{"count":25,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/578\/revisions"}],"predecessor-version":[{"id":631,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/578\/revisions\/631"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}