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/Delegation_pattern">Delegation Pattern</a> is a technique where an object's behavior (public methods) is implemented by delegating responsibility to one or more associated objects.</p> <p>Groovy allows the traditional style of applying the delegation pattern, e.g. see <a class="confluence-link" href="/display/GROOVY/Replace+Inheritance+with+Delegation" data-linked-resource-id="78709" data-linked-resource-type="page" data-linked-resource-default-alias="Replace Inheritance with Delegation" data-base-url="http://docs.codehaus.org">Replace Inheritance with Delegation</a>.</p> <h1>Implement Delegation Pattern using ExpandoMetaClass </h1> <p>The <a class="confluence-link" href="/display/GROOVY/ExpandoMetaClass" data-linked-resource-id="79517" data-linked-resource-type="page" data-linked-resource-default-alias="ExpandoMetaClass" data-base-url="http://docs.codehaus.org">ExpandoMetaClass</a> allows usage of this pattern to be encapsulated in a library. This allows Groovy to emulate similar libraries available for the Ruby language.</p> <p>Consider the following library class:</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> class Delegator { private targetClass private delegate Delegator(targetClass, delegate) { this.targetClass = targetClass this.delegate = delegate } def delegate(String methodName) { delegate(methodName, methodName) } def delegate(String methodName, String asMethodName) { targetClass.metaClass."$asMethodName" = delegate.&"$methodName" } def delegateAll(String[] names) { names.each { delegate(it) } } def delegateAll(Map names) { names.each { k, v -> delegate(k, v) } } def delegateAll() { delegate.class.methods*.name.each { delegate(it) } } } </pre></td></tr></table> <p>With this in your classpath, you can now apply the delegation pattern dynamically as shown in the following examples. First, consider we have the following classes:</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> class Person { String name } class MortgageLender { def borrowAmount(amount) { "borrow \$$amount" } def borrowFor(thing) { "buy $thing" } } def lender = new MortgageLender() def delegator = new Delegator(Person, lender) </pre></td></tr></table> <p>We can now use the <em>delegator</em> to automatically borrow methods from the <em>lender</em> object to extend the <em>Person</em> class. We can borrow the methods as is or with a rename:</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> delegator.delegate 'borrowFor' delegator.delegate 'borrowAmount', 'getMoney' def p = new Person() println p.borrowFor('present') // => buy present println p.getMoney(50) // => borrow $50 </pre></td></tr></table> <p>The first line above, adds the <em>borrowFor</em> method to the <em>Person</em> class by delegating to the <em>lender</em> object. The second line adds a <em>getMoney</em> method to the <em>Person</em> class by delegating to the <em>lender</em> object's <em>borrowAmount</em> method.</p> <p>Alternatively, we could borrow multiple methods like this:</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> delegator.delegateAll 'borrowFor', 'borrowAmount' </pre></td></tr></table> <p>Which adds these two methods to the <em>Person</em> class.</p> <p>Or if we want all the methods, like this:</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> delegator.delegateAll() </pre></td></tr></table> <p>Which will make all the methods in the delegate object available in the <em>Person</em> class.</p> <p>Alternatively, we can use a map notation to rename multiple methods:</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> delegator.delegateAll borrowAmount:'getMoney', borrowFor:'getThing' </pre></td></tr></table> <h1>Implement Delegation Pattern using @Delegate annotation </h1> <p>Since version 1.6 you can use the built-in delegation mechanism which is based on AST transformation.<br /> This make delegation even easier:</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> class Person { def name @Delegate MortgageLender mortgageLender = new MortgageLender() } class MortgageLender { def borrowAmount(amount) { "borrow \$$amount" } def borrowFor(thing) { "buy $thing" } } def p = new Person() assert "buy present" == p.borrowFor('present') assert "borrow $50" == p.borrowAmount(50) </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