{"id":1054,"date":"2018-04-16T09:43:24","date_gmt":"2018-04-16T09:43:24","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=1054"},"modified":"2019-03-13T22:37:35","modified_gmt":"2019-03-13T22:37:35","slug":"detect-not-well-implemented-c-design-patterns-automatically","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/detect-not-well-implemented-c-design-patterns-automatically\/","title":{"rendered":"Detect not well implemented C++ design patterns automatically."},"content":{"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 the flyweight pattern.<!--more--><\/p>\n<p>Sometimes the patterns are not well implemented by developers. This\u00a0could generate some design issues and affect the benefits of using them. It\u2019s interesting to detect where they are not well implemented, and correct their implementation.<span id=\"more-578\"><\/span><\/p>\n<p>To detect these kinds of issues we need a maximum of information about the source code including:<\/p>\n<ul>\n<li>Attributes of classes, methods, and fields.<\/li>\n<li>The inheritance between classes.<\/li>\n<li>Dependencies between classes, methods, and fields.<\/li>\n<li>Where classes are instantiated.<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.cppdepend.com\/\">CppDepend<\/a>\u00a0generates a code model which contains all these data and permits to query them using\u00a0<a href=\"http:\/\/cppdepend.com\/Doc_CQLinq_Features.aspx\">CQLinq<\/a>. Let\u2019s try to detect the misusing of two patterns: Singleton and Strategy.<\/p>\n<p><strong>Singleton<\/strong><\/p>\n<p>The\u00a0singleton pattern\u00a0is a\u00a0design pattern that restricts the\u00a0instantiation\u00a0of a class to one\u00a0object.\u00a0However, using this pattern became controversial, and not all architects and designers recommend it, here\u2019s an\u00a0<a href=\"http:\/\/code.google.com\/p\/google-singleton-detector\/wiki\/WhySingletonsAreControversial\">article\u00a0<\/a>talking about the singleton controversy.<\/p>\n<p>A\u00a0common mistake of implementing a singleton pattern is to not make\u00a0the constructor\u00a0private.<\/p>\n<p>The following query detects all classes with the same traits\u00a0as a singleton, \u00a0i.e. classes containing one static field referencing itself, a static method returning this field. And not contains a private constructor.<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pattern1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-612\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pattern1.png\" alt=\"pattern1\" width=\"695\" height=\"205\" \/><\/a><\/p>\n<p><strong>Strategy<\/strong><\/p>\n<p>There are common situations when classes differ only in their behavior. For this case, 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>Here\u2019s the UML diagram of this pattern:<\/p>\n<p><a href=\"http:\/\/www.javadepend.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.javadepend.com\/Blog\/wp-content\/uploads\/Strategy_Pattern_in_UML.png\" alt=\"Strategy_Pattern_in_UML\" width=\"256\" height=\"160\" \/><\/a><\/p>\n<p>As the diagram shows, the context class uses the abstract class \u201cStrategy\u201d and has no knowledge about the concrete implementations. However, \u00a0for some implementations, the concrete classes are used directly by the Context one. Here\u2019s a sample of this mistake:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pattern3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-615\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pattern3.png\" alt=\"pattern3\" width=\"670\" height=\"323\" \/><\/a><\/p>\n<p>Let\u2019s search with\u00a0CQLinq all 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 concrete implementations instead of the abstract ones.<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pattern2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-613\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/pattern2.png\" alt=\"pattern2\" width=\"638\" height=\"121\" \/><\/a><\/p>\n<p>The result of this query will give us the derived types used directly by other methods instead of using the abstract ones. You have just to search for methods using them to know where to correct the strategy pattern implementation.<\/p>\n<p>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\u2019s an issue or not.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>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 the flyweight pattern.<\/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-1054","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=1054"}],"version-history":[{"count":6,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1054\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1054\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=1054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=1054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=1054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}