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>In the <a href="http://en.wikipedia.org/wiki/Chain_of_responsibility_pattern">Chain of Responsibility Pattern</a>, objects using and implementing an interface (one or more methods) are intentionally loosely coupled. A set of objects that <em>implement</em> the interface are organised in a list (or in rare cases a tree). Objects using the interface make requests from the first <em>implementor</em> object. It will decide whether to perform any action itself and whether to pass the request further down the line in the list (or tree). Sometimes a default implementation for some request is also coded into the pattern if none of the implementors respond to the request.</p> <h3>Example</h3> <p>In this example, the script sends requests to the <code>lister</code> object. The <code>lister</code> points to a <code>UnixLister</code> object. If it can't handle the request, it sends the request to the <code>WindowsLister</code>. If it can't handle the request, it sends the request to the <code>DefaultLister</code>.</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 UnixLister { private nextInLine UnixLister(next) { nextInLine = next } def listFiles(dir) { if (System.getProperty('os.name') == 'Linux') println "ls $dir".execute().text else nextInLine.listFiles(dir) } } class WindowsLister { private nextInLine WindowsLister(next) { nextInLine = next } def listFiles(dir) { if (System.getProperty('os.name') == 'Windows XP') println "cmd.exe /c dir $dir".execute().text else nextInLine.listFiles(dir) } } class DefaultLister { def listFiles(dir) { new File(dir).eachFile{ f -> println f } } } def lister = new UnixLister(new WindowsLister(new DefaultLister())) lister.listFiles('Downloads') </pre></td></tr></table> <p>The output will be a list of files (with slightly different format depending on the operating system).</p> <p>Here is a UML representation:</p> <p><img class="confluence-embedded-image" src="/download/attachments/231080197/ChainOfResponsibilityClasses.gif?version=1&modificationDate=1369483117935" data-image-src="/download/attachments/231080197/ChainOfResponsibilityClasses.gif?version=1&modificationDate=1369483117935" data-linked-resource-id="231376101" data-linked-resource-type="attachment" data-linked-resource-default-alias="ChainOfResponsibilityClasses.gif" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231080197" title="null > ChainOfResponsibilityClasses.gif"></p> <p>Variations to this pattern:</p> <ul> <li>we could have an explicit interface, e.g. <code>Lister</code>, to statically type the implementations but because of <em>duck-typing</em> this is optional</li> <li>we could use a chain tree instead of a list, e.g. <code>if (animal.hasBackbone())</code> delegate to <code>VertebrateHandler</code> else delegate to <code>InvertebrateHandler</code></li> <li>we could always pass down the chain even if we processed a request</li> <li>we could decide at some point to not respond and not pass down the chain</li> <li>we could use Groovy's meta-programming capabilities to pass unknown methods down the chain</li> </ul>
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