...
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
|
Chain of Command
Wiki Markup | ||
---|---|---|
{float:right|background=#eee}
{contentbylabel:title=Related Articles|showLabels=false|showSpace=false|space=@self|labels=
|
One of the most useful of the Gang Of Four Design Patterns is the command pattern.
|
With the chain of responsibility design patternWith 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 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.
...
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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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 |
---|