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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
|
With the chain of responsibility design pattern, a complex process is broken down into many individual steps. 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.
...
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); } |
All that generics parameterization just ensures that the command interface matches the items in the list, and confirms that a single instance of the command interface will be returned.
...
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); } |
Here, the behavior of the MyChainService is defined by its configuration: an ordered list of MyChainService commands that are contributed by one or more modules.
...
ChainBuilder will reuse the fabricated class for any number of chains of the same command interface.
Scrollbar |
---|