{"id":4737,"date":"2014-03-30T15:02:32","date_gmt":"2014-03-30T15:02:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/looking-for-a-design-pattern-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:02:32","modified_gmt":"2014-03-30T15:02:32","slug":"looking-for-a-design-pattern-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/looking-for-a-design-pattern-collection-of-common-programming-errors\/","title":{"rendered":"Looking for a design pattern-Collection of common programming errors"},"content":{"rendered":"<p>I haven&#8217;t studied design patterns, but I&#8217;m willing to bet that there is one for what I need to do. I&#8217;m running a set of different algorithms over a couple of trees. They all implement an interface:<\/p>\n<pre><code>public interface DistanceMetric {\n    public double distance(AbstractTree t1, AbstractTree t2);\n}\n\npublic class concreteDistanceAlgorithmV1 implements DistanceMetric{\n\n    public double distance(AbstractTree t1, AbstractTree t2){\n        \/\/ algorithm methods\n        return distance; \n    }\n}\n<\/code><\/pre>\n<p>However, suddenly I now need two versions of each algorithm one as above and the second is a variation which has the first tree preprocessed:<\/p>\n<pre><code>public interface DistanceMetricType2 {\n    public double distance(AbstractTree t);\n}\n\npublic class concreteDistanceAlgorithmV2 implements DistanceMetricType2{\n\n    private Object transformation1;\n\n    public concreteDistanceAlgorithmV2(AbstractTree t1){\n        transformation1 = process(t1);\n    }\n\n    public double distance(AbstractTree t2){\n\n        Object transformation2 = process(t2);\n        \/\/algorithm involving both transformations\n        return distance;\n    }\n}\n<\/code><\/pre>\n<p>There must be a better way than making two classes for every algorithm? Is this a use for the strategy pattern or similar? How can I modify what I have to make better use of good design principles?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I haven&#8217;t studied design patterns, but I&#8217;m willing to bet that there is one for what I need to do. I&#8217;m running a set of different algorithms over a couple of trees. They all implement an interface: public interface DistanceMetric { public double distance(AbstractTree t1, AbstractTree t2); } public class concreteDistanceAlgorithmV1 implements DistanceMetric{ public double [&hellip;]<\/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-4737","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4737"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4737\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}