{"id":33,"date":"2010-03-09T07:51:03","date_gmt":"2010-03-09T07:51:03","guid":{"rendered":"http:\/\/miklcct.no-ip.org\/wordpress\/?p=33"},"modified":"2016-10-31T07:53:32","modified_gmt":"2016-10-31T07:53:32","slug":"template-pattern-v-s-interface-pattern","status":"publish","type":"post","link":"https:\/\/miklcct.com\/wordpress\/2010\/03\/09\/template-pattern-v-s-interface-pattern\/","title":{"rendered":"Template pattern v.s. Interface pattern"},"content":{"rendered":"<p>As a library programmer, the decision between using templates or using inheritance may be a difficult decision. Everyone wants their libraries to be generic. Which pattern is better?<\/p>\n<p><strong>Template pattern<\/strong><br \/>\nTemplates allow you to write code that does the same thing on unrelated types. For example, in C++, <code>std::sort<\/code> in STL is a function template. It allow us to sort anything that can be compared using the less-than operator and swapped using <code>std::swap<\/code> (another function template)<\/p>\n<p><strong>Interface pattern<\/strong><br \/>\nWhen you call a polymorphic method, the final overrider of the method is called, even if you call the method by using an expression of a base class as the static type. A class can be used as an interface if it does not contain any non-polymorphic methods (excluding constructors) or public\/protected fields. An interface may contain public\/protected properties if the underlying method is polymorphic. If a language supports &#8220;polymorphic field&#8221;, that is, a field can be overriden with a property with polymorphic behaviour, you can also include a field in an interface. In short, an interface is a class that its public interface does not depend on any of its implementation.<\/p>\n<p><strong>A comparison<\/strong><br \/>\nTemplates can be written using whatever style you want but there is a number of limitation when using the interface pattern:<\/p>\n<ul>\n<li>You must pass the object by pointer or by reference to the function written by its interface, but not by copy. In languages that support dynamically determining the type of an object (the &#8220;dynamic&#8221; type), it&#8217;s not a problem since you can copy that object inside the function.<\/li>\n<li>The classes accepted by the function must be inherited from the interface. That is, they must share the same class hierarchy. Consider the following code in C++:\n<pre>\r\nclass vehicle {\r\npublic:\r\n\tvirtual ~vehicle();\r\n\tvirtual void accelerate() = 0;\r\n\tvirtual void decelerate() = 0;\r\n};\r\n<\/pre>\n<p>This is an abstract base class without implementation of <code>vehicle::accelerate()<\/code> and <code>vehicle::decelerate()<\/code>. Code can be written using this interface, regardless of the actual implementation of <code>vehicle::accelerate()<\/code> and <code>vehicle::decelerate()<\/code>. However, given another class below:<\/p>\n<pre>\r\nclass car {\r\n\tvirtual void accelerate();\r\n\tvirtual void decelerate();\r\n\tvirtual ~car();\r\n};\r\n<\/pre>\n<p>You can&#8217;t use code written to the <code>vehicle<\/code> interface. You must multiply inherit from <code>car<\/code> (to use its implementation) and <code>vehicle<\/code> (to use its interface). Even worse, if <code>car::accelerate()<\/code> and <code>car::decelerate<\/code> weren&#8217;t made virtual, you cannot use the interface at all. In languages that do not support multiple inheritance, you can&#8217;t use the interface at all.<\/li>\n<\/ul>\n<p>Template itself is good, but made worse by C++ implementations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a library programmer, the decision between using templates or using inheritance may be a difficult decision. Everyone wants their libraries to be generic. Which pattern is better? Template pattern Templates allow you to write code that does the same thing on&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[21],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-programming","tag-cc"],"_links":{"self":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":9,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/posts\/33\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/miklcct.com\/wordpress\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}