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/Strategy_pattern">Strategy Pattern</a> allows you to abstract away particular algorithms from their usage. This allows you to easily swap the algorithm being used without having to change the calling code. The general form of the pattern is:</p> <p><img class="confluence-embedded-image" src="/download/attachments/231080095/StrategyClasses.gif?version=1&modificationDate=1369318334111" data-image-src="/download/attachments/231080095/StrategyClasses.gif?version=1&modificationDate=1369318334111" data-linked-resource-id="231375535" data-linked-resource-type="attachment" data-linked-resource-default-alias="StrategyClasses.gif" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231080095" title="null > StrategyClasses.gif"></p> <p>In Groovy, because of its ability to treat code as a first class object using anonymous methods (which we loosely call <em>Closures</em>), the need for the strategy pattern is greatly reduced. You can simply place algorithms inside Closures.</p> <h3>Example</h3> <p>First let's look at the traditional way of encapsulating the Strategy Pattern.</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> interface Calc { def execute(n, m) } class CalcByMult implements Calc { def execute(n, m) { n * m } } class CalcByManyAdds implements Calc { def execute(n, m) { def result = 0 n.times{ result += m } return result } } def sampleData = [ [3, 4, 12], [5, -5, -25] ] Calc[] multiplicationStrategies = [ new CalcByMult(), new CalcByManyAdds() ] sampleData.each{ data -> multiplicationStrategies.each{ calc -> assert data[2] == calc.execute(data[0], data[1]) } } </pre></td></tr></table> <p>Here we have defined an interface <code>Calc</code> which our concrete strategy classes will implement (we could also have used an abstract class). We then defined two algorithms for doing simple multiplication: <code>CalcByMult</code> the normal way, and <code>CalcByManyAdds</code> using only addition (don't try this one using negative numbers - yes we could fix this but it would just make the example longer). We then use normal <a href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming">polymorphism</a> to invoke the algorithms.</p> <p>Here is the Groovier way to achieve the same thing 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> def multiplicationStrategies = [ { n, m -> n * m }, { n, m -> def result = 0; n.times{ result += m }; result } ] def sampleData = [ [3, 4, 12], [5, -5, -25] ] sampleData.each{ data -> multiplicationStrategies.each{ calc -> assert data[2] == calc(data[0], data[1]) } } </pre></td></tr></table>
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