Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

When To Use The Chain of Responsabilities Pattern

DZone's Guide to

When To Use The Chain of Responsabilities Pattern

· Java Zone ·
Free Resource

Automist automates your software deliver experience. It's how modern teams deliver modern software.

Description:

The pattern chains the receiving objects together, and then passes any request messages from object to object until it reaches an object capable of handling the message. The number and type of handler objects isn’t known a priori, they can be configured dynamically. The chaining mechanism uses recursive composition to allow an unlimited number of handlers to be linked.

Chain of Responsibility simplifies object interconnections. Instead of senders and receivers maintaining references to all candidate receivers, each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain.

Benefits:

  •  Reduced coupling
  •  Added flexibility in assigning responsibilities to objects
  •  Allows a set of classes to behave as a whole, because events producedin one class can be sent on to other handler classes within the composite

When to use:

  • More than one object can handle a request, and the handler isn’t known.
  • You want to issue a request to one of several objects without specifying the receiver explicitly.
  • The set of objects that can handle a request should be specified dynamically.

Diagram:

Code samples can be downloaded here.

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}