Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>The <a href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method Pattern</a> abstracts away the details of several algorithms. The generic part of an algorithm is contained within a base class. Particular implementation details are captured within base classes. The generic pattern of classes involved looks like this:</p> <p><img class="confluence-embedded-image" src="/download/attachments/231080116/TemplateMethodClasses.gif?version=1&modificationDate=1369341812073" data-image-src="/download/attachments/231080116/TemplateMethodClasses.gif?version=1&modificationDate=1369341812073" data-linked-resource-id="231375565" data-linked-resource-type="attachment" data-linked-resource-default-alias="TemplateMethodClasses.gif" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231080116" title="null > TemplateMethodClasses.gif"></p> <h3>Example</h3> <p>In this example, <code>Accumulator</code> captures the essence of the accumulation algorithm. The base classes <code>Sum</code> and <code>Product</code> provide particular customised ways to use the generic accumulation algorithm.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> abstract class Accumulator { protected initial abstract doAccumulate(total, v) def accumulate(values) { def total = initial values.each { v -> total = doAccumulate(total, v) } total } } class Sum extends Accumulator { def Sum() { initial = 0 } def doAccumulate(total, v) { total + v } } class Product extends Accumulator { def Product() { initial = 1 } def doAccumulate(total, v) { total * v } } println new Sum().accumulate([1,2,3,4]) println new Product().accumulate([1,2,3,4]) </pre></td></tr></table> <p>The resulting output is:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 10 24 </pre></td></tr></table> <p>In this particular case, you could use Groovy's inject method to achieve a similar result using Closures:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Closure addAll = { total, item -> total += item } def accumulated = [1, 2, 3, 4].inject(0, addAll) println accumulated // => 10 </pre></td></tr></table> <p>Thanks to duck-typing, this would also work with other objects which support an add (<code>plus()</code> in Groovy) method, e.g.:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> accumulated = [ "1", "2", "3", "4" ].inject("", addAll) println accumulated // => "1234" </pre></td></tr></table> <p>We could also do the multiplication case as follows:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Closure multAll = { total, item -> total *= item } accumulated = [1, 2, 3, 4].inject(1, multAll) println accumulated // => 24 </pre></td></tr></table> <p>Using closures this way looks more like the <a class="confluence-link" href="/display/GROOVY/Strategy+Pattern" data-linked-resource-id="78100" data-linked-resource-type="page" data-linked-resource-default-alias="Strategy Pattern" data-base-url="http://docs.codehaus.org">Strategy Pattern</a> but if we realise that the built-in <code>inject</code> method is the generic part of the algorithm for our template method, then the Closures become the customised parts of the template method pattern.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced