{"id":26,"date":"2011-09-18T01:24:21","date_gmt":"2011-09-18T01:24:21","guid":{"rendered":"http:\/\/www.lexicalscope.com\/blog\/?p=26"},"modified":"2011-09-19T14:19:47","modified_gmt":"2011-09-19T14:19:47","slug":"compositional-patterns-for-test-driven-development","status":"publish","type":"post","link":"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/18\/compositional-patterns-for-test-driven-development\/","title":{"rendered":"Compositional Patterns for Test Driven Development"},"content":{"rendered":"<p>This article is going to look at how to implement a parameterizable algorithm so it can both conform  generally to the open\/closed principal and also most effectively be tested.<\/p>\n<p>A common pattern for code reuse, implementation selection, or extension, is to use class inheritance and the template method pattern. This is an example of an implementation of the template method pattern with two different variations <code>B<\/code> and <code>C<\/code>:<\/p>\n<pre lang=\"java\">\r\nabstract class A {\r\n    public void doSomething() {\r\n        doP();\r\n        doQ();\r\n        doR();\r\n    }\r\n\r\n    protected abstract void doP();\r\n    protected abstract void doQ();\r\n    protected abstract void doR();\r\n}\r\n\r\nclass B extends A {\r\n    @Override protected void doP() { \/* do P the B way *\/}\r\n    @Override protected void doQ() { \/* do Q the B way *\/}\r\n    @Override protected void doR() { \/* do R the B way *\/}\r\n}\r\n\r\nclass C extends A {\r\n    @Override protected void doP() { \/* do P the C way *\/}\r\n    @Override protected void doQ() { \/* do Q the C way *\/}\r\n    @Override protected void doR() { \/* do R the C way *\/}\r\n}\r\n<\/pre>\n<h3>Refactoring template method pattern to Strategy Pattern<\/h3>\n<p>We can always convert this template method pattern to a compositional pattern by performing a refactoring in the following steps (if you have member variable access there are a couple more steps, but I&#8217;ll cover them in a follow up article):<\/p>\n<p><strong>Step 1<\/strong>; encapsulate the construction of the B and C strategies:<\/p>\n<pre lang=\"java\">\r\nabstract class A {\r\n    public void doSomething() {\r\n        doP();\r\n        doQ();\r\n        doR();\r\n    }\r\n\r\n    protected abstract void doP();\r\n    protected abstract void doQ();\r\n    protected abstract void doR();\r\n}\r\n\r\nclass B extends A {\r\n    public static A createB() {\r\n        return new B();\r\n    }\r\n\r\n    @Override protected void doP() { \/* do P the B way *\/}\r\n    @Override protected void doQ() { \/* do Q the B way *\/}\r\n    @Override protected void doR() { \/* do R the B way *\/}\r\n}\r\n\r\nclass C extends A {\r\n    public static A createC() {\r\n        return new C();\r\n    }\r\n\r\n    @Override protected void doP() { \/* do P the C way *\/}\r\n    @Override protected void doQ() { \/* do Q the C way *\/}\r\n    @Override protected void doR() { \/* do R the C way *\/}\r\n}<\/pre>\n<p><strong>Step 2<\/strong>; extract an interface for the strategy methods:<\/p>\n<pre lang=\"java\">\r\ninterface S {\r\n    void doP();\r\n    void doQ();\r\n    void doR();\r\n}\r\n\r\nabstract class A implements S {\r\n    private final S s = this;\r\n\r\n    public void doSomething() {\r\n        s.doP();\r\n        s.doQ();\r\n        s.doR();\r\n    }\r\n}\r\n\r\nclass B extends A {\r\n    public static A createB() {\r\n        return new B();\r\n    }\r\n\r\n    @Override public void doP() { \/* do P the B way *\/}\r\n    @Override public void doQ() { \/* do Q the B way *\/}\r\n    @Override public void doR() { \/* do R the B way *\/}\r\n}\r\n\r\nclass C extends A {\r\n    public static A createC() {\r\n        return new C();\r\n    }\r\n\r\n    @Override public void doP() { \/* do P the C way *\/}\r\n    @Override public void doQ() { \/* do Q the C way *\/}\r\n    @Override public void doR() { \/* do R the C way *\/}\r\n}\r\n<\/pre>\n<p><strong>Step 3<\/strong>; pass the strategies into the superclass instead of using <code>this<\/code>:<\/p>\n<pre lang=\"java\">\r\ninterface S {\r\n    void doP();\r\n    void doQ();\r\n    void doR();\r\n}\r\n\r\nfinal class A {\r\n    private final S s;\r\n\r\n    public A(final S s) {\r\n        this.s = s;\r\n    }\r\n\r\n    public void doSomething() {\r\n        s.doP();\r\n        s.doQ();\r\n        s.doR();\r\n    }\r\n}\r\n\r\nclass B implements S {\r\n    public static A createB() {\r\n        return new A(new B());\r\n    }\r\n\r\n    public void doP() { \/* do P the B way *\/}\r\n    public void doQ() { \/* do Q the B way *\/}\r\n    public void doR() { \/* do R the B way *\/}\r\n}\r\n\r\nclass C implements S {\r\n    public static A createC() {\r\n        return new A(new C());\r\n    }\r\n\r\n    public void doP() { \/* do P the C way *\/}\r\n    public void doQ() { \/* do Q the C way *\/}\r\n    public void doR() { \/* do R the C way *\/}\r\n}\r\n<\/pre>\n<h3>Advantage of the compositional style<\/h3>\n<h4>Less fragile<\/h4>\n<p>Changes to <code>A<\/code> such as new methods are much less likely to break the strategies in the compositional style.<\/p>\n<h4>Easier to Test<\/h4>\n<p>In the compositional style, <code>class A<\/code> can be tested by itself using Mocks. As can <code>class B<\/code> and <code>class C<\/code>. In the inheritance style <code>class B<\/code> and <code>class C<\/code> cannot be tested without also testing <code>class A<\/code>. This leads to duplication in the tests, as features of class A are re-tested for every subclass.<\/p>\n<h4>Easier to Reuse<\/h4>\n<p>In the compositional style <code>class B<\/code> and <code>class C<\/code> can be reused in other contexts where A is not relevant. In the inheritance style this type of reuse is not possible.<\/p>\n<h4>Emergent Model\/Domain concepts<\/h4>\n<p>If <code>class B<\/code> or <code>class C<\/code> are reused in a different context, it may turn out that during subsequent refactoring they develop a new role within the system. I may even discover an important new domain concept.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is going to look at how to implement a parameterizable algorithm so it can both conform generally to the open\/closed principal and also most effectively be tested. A common pattern for code reuse, implementation selection, or extension, is &hellip; <a href=\"https:\/\/www.lexicalscope.com\/blog\/2011\/09\/18\/compositional-patterns-for-test-driven-development\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,8],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-patterns","category-refactoring"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p2e3P7-q","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":11,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":87,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions\/87"}],"wp:attachment":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}