Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed bad links due to copy-paste from cwiki-test

...

Scrollbar

...

The ChainBuilder Service is a built-in service used to implement of one of the most useful of the Gang Of Four design patterns, the chain of responsibility.

Div
stylefloat:right
titleRelated Articles
classaui-label
Content by Label
showLabelsfalse
showSpacefalse
titleRelated Articles
cqllabel in ("chain-of-command","service-builders") and space = currentSpace()

With the chain of responsibility design

Chain of Command

Wiki Markup
{float:right}
{info:title=Related Article}
[IoC Cookbook: Chain of Command|IoC cookbook - patterns#chainofcommand]
{info}
{float}

One of the most useful of the Gang Of Four Design Patterns is the command pattern.

With the command pattern, a complex process is broken down into many individual steps. The steps are the commands in the Each step is a command (see command pattern). A key part of this is that the commands are expected to implement some common interface. The commands are also carefully arrainged arranged into a specific order.

The process operates by working down the list of commands. Each , and each command is given a chance to operate. A In the ChainBuilder service, a command can terminate the process either by throwing an exception, or by returning true.

...

This is a useful pattern because it makes it very easy to extend a given process, simply by providing new commands and specifying where they fit into the overall process. Most often chain of command is combined with an ordered configuration to define what the list of commands are (and in what order they should execute).

ChainBuilder Service

Because this pattern is used so often inside Tapestry, a built-in service exists to create implementations of the pattern as needed. The ChainBuilder service takes care of all the work:

Code Block
java
languagejava

public interface ChainBuilder
{
  <T> T build(Class<T> commandInterface, List<T> commands);
}

...

This can be used inside a service builder method. Nothing says a service builder method just has to instantiate a class; it is only required to return an appropriate object. We can just let the ChainBuilder service create that object.

Code Block
java
languagejava

  public static MyChainService build(List<MyChainService> commands,
    @InjectService("ChainBuilder")
    ChainBuilder chainBuilder)
  {
     return chainBuilder.build(MyChainService.class, commands);
  }

...

ChainBuilder will reuse the fabricated class for any number of chains of the same command interface.

...

 

Scrollbar