{"id":467,"date":"2014-04-14T20:51:30","date_gmt":"2014-04-14T19:51:30","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=467"},"modified":"2020-09-13T20:02:09","modified_gmt":"2020-09-13T19:02:09","slug":"java-forwarding-interface-pattern","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/","title":{"rendered":"Java Forwarding-Interface Pattern"},"content":{"rendered":"<p class=\"lead\">Java 8&#8217;s default methods on interfaces means we can implement the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Decorator_pattern\">decorator pattern<\/a> much less verbosely. <\/p>\n<p>The decorator pattern allows us to add behaviour to an object without using inheritance. I often find myself using it to &#8220;extend&#8221; third party interfaces with useful additional behaviour. <\/p>\n<p>Let&#8217;s say we wanted to add a map method to List that allows us to convert from a list of one type to a list of another. There is already such a method on the <a href=\"http:\/\/download.java.net\/lambda\/b78\/docs\/api\/java\/util\/stream\/Stream.html#map(java.util.function.Function)\">Stream<\/a> interface, but it serves as an example<\/p>\n<p>We used to have to either <\/p>\n<p>a) Subclass a concrete List implementation and add our method (which makes re-use hard), or<br \/>\nb) Re-implement the <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/List.html\">considerably large<\/a> List interface, delegating to a wrapped List. <\/p>\n<p>You can ask your IDE to generate these delegate methods for you, but with a large interface like List the boilerplate tends to obscure the added behaviour.<\/p>\n<img src=\"https:\/\/benjiweber.co.uk\/b\/generate_delegates.png\"\/><br \/>\n<pre lang=\"java\">\r\nclass MappingList<T> implements List<T> {\r\n    private List<T> impl;\r\n\r\n    public int size() {\r\n        return impl.size();\r\n    }\r\n        \r\n    public boolean isEmpty() {\r\n        return impl.isEmpty();\r\n    }\r\n    \r\n    \/\/ Many more boilerplate methods omitted for brevity\r\n    \r\n    \/\/ The method we actually wanted to add.\r\n    public <R> List<R> map(Function<T,R> f) {\r\n        return list.stream().map(f).collect(Collectors.toList());\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>Guava gave us a third option <\/p>\n<p>c) Extend the the Guava <a href=\"http:\/\/docs.guava-libraries.googlecode.com\/git\/javadoc\/com\/google\/common\/collect\/ForwardingList.html\">ForwardingList<\/a> class. Unfortunately that meant you couldn&#8217;t extend any other class.<\/p>\n<p>Java 8 gives us a fourth option <\/p>\n<p>d) We can implement the forwarding behaviour in an interface, and then add our behaviour on top. <\/p>\n<p>The disadvantage is you need a public method which exposes the underlying implementation. The advantages are you can keep the added behaviour separate, and it&#8217;s easier to compose them.<\/p>\n<p>Our decorator can now be really short &#8211; something like<\/p>\n<pre lang=\"java\">\r\nclass MappableList<T> implements List<T>, ForwardingList<T>, Mappable<T> {\r\n    private List<T> impl;\r\n\r\n    public MappableList(List<T> impl) {\r\n        this.impl = impl;\r\n    }\r\n\r\n    @Override\r\n    public List<T> impl() {\r\n        return impl;\r\n    }\r\n}\r\n<\/pre>\n<p>We can use it like this<\/p>\n<pre lang=\"java\">\r\n\/\/ prints 3, twice.\r\nnew MappableList<String>(asList(\"foo\", \"bar\"))\r\n    .map(s -> s.length())\r\n    .forEach(System.out::println);\r\n\r\n<\/pre>\n<p>The new method we added is declared in its own Mappable&lt;T&gt; interface which is uncluttered.<\/p>\n<pre lang=\"java\">\r\ninterface Mappable<T> extends ForwardingList<T> {\r\n\tdefault <R> List<R> map(Function<T,R> f) {\r\n\t\treturn impl().stream().map(f).collect(Collectors.toList());\r\n\t}\r\n}\r\n<\/pre>\n<p>The delegation boilerplate we can keep in its own interface, out of the way. Since it&#8217;s an interface we are free to extend other classes\/interfaces in our decorator<\/p>\n<pre lang=\"java\">\r\ninterface ForwardingList<T> extends List<T> {\r\n    List<T> impl();\r\n\r\n    default int size() {\r\n        return impl().size();\r\n    }\r\n        \r\n    default boolean isEmpty() {\r\n        return impl().isEmpty();\r\n    }\t\r\n    \r\n    \/\/ Other methods omitted for brevity\r\n\r\n}\r\n<\/pre>\n<p>If we wanted to mix in some more functionality to our MappableList decorator class we could just implement another interface. In the above example we added a new method, so this time let&#8217;s modify one of the existing methods on List. Let&#8217;s make a List that always thinks it&#8217;s empty.<\/p>\n<pre lang=\"java\">\r\ninterface AlwaysEmpty<T> extends ForwardingList<T> {\r\n    default boolean isEmpty() {\r\n        return true;\r\n    }\r\n}\r\nclass MappableList<T> implements \r\n    List<T>, \r\n    ForwardingList<T>, \r\n    Mappable<T>, \r\n    AlwaysEmpty<T> { \/\/ Mix in the new interface\r\n    \/\/ ...\r\n}\r\n<\/pre>\n<p>Now our list always claims it&#8217;s empty<\/p>\n<pre lang=\"java\">\r\n\/\/ prints true\r\nSystem.out.println(new MappableList<String>(asList(\"foo\", \"bar\")).isEmpty());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java 8&#8217;s default methods on interfaces means we can implement the decorator pattern much less verbosely. The decorator pattern allows us to add behaviour to an object without using inheritance. I often find myself using it to &#8220;extend&#8221; third party interfaces with useful additional behaviour. Let&#8217;s say we wanted to add a map method to&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/\" class=\"more-link\" title=\"Read Java Forwarding-Interface Pattern\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Forwarding-Interface Pattern - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Java 8&#8217;s default methods on interfaces means we can implement the decorator pattern much less verbosely. The decorator pattern allows us to add behaviour to an object without using inheritance. I often find myself using it to &#8220;extend&#8221; third party interfaces with useful additional behaviour. Let&#8217;s say we wanted to add a map method to... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2014-04-14T19:51:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-13T19:02:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/benjiweber.co.uk\/b\/generate_delegates.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/benjiweber.co.uk\/b\/generate_delegates.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/\",\"name\":\"Java Forwarding-Interface Pattern - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/#primaryimage\"},\"datePublished\":\"2014-04-14T19:51:30+00:00\",\"dateModified\":\"2020-09-13T19:02:09+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/467"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=467"}],"version-history":[{"count":42,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/467\/revisions"}],"predecessor-version":[{"id":1477,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/467\/revisions\/1477"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}