Java Code Samples

Chain of responsabilities

Posted in Code samples, Old stuff by Cristian on May 11, 2011

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.

About these ads

One Response

Subscribe to comments with RSS.

  1. […] Chain of responsibility […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: