The original post was published here.
I recently read this Pietro’s post about a possible adaptation of the chain of responsability pattern: the “Set of responsibility”. This is very similar to its “father” because each Handler
handles the responsibility of a Request
but in this case it doesn’t propagate the check of responsibility to other handlers. There is a responsibility without chain!
In this article I’d like to present the usage of this pattern in an IOC Container, where the Handlers
aren’t added to the HandlerSet
list but provided from the container. In this way you can add new responsibility to the system simply adding a new Handler
in the container without changing other parts of implemented code (es. the HandlerSet
), in full compliance with the open closed principle.
For coding I’ll use the Spring Framework (JAVA) because it has a good IOC Container and provides a set of classes to manage with that. Inversion of control principle and the Dependency Injection are first class citizens in the SpringFramework.
Here is the UML class diagram with 3 responsabilities X,Y,Z and a brief description of the solution adopted.
1 |
|
CtxHandlerManager
works like a Handler
dispatcher. The handle
method finds the Handler
and calls its handle
method which invokes the doSomething
method of the Request
.
In the findHandler
method I use the Spring ClassPathScanningCandidateComponentProvider
object with an AssignableTypeFiler
to the Handler
class. I call the findCandidateComponent
on a basePackage (the value is set by @Value
Spring annotation) and for each candidate the canHandle
method check the responsibility. And that’s all!
In Sender
class the HandlerProvider
implementation (CtxHandlerManager
)is injected by Spring IOC by “Autowiring”:
1 |
|
This solution let you to add new responsibility simply creating new Request
implementation and a new Handler implementation to manage it. By applying @Component
annotation on the Handler
you allow Spring to autodetect the class for dependency injection when annotation-based configuration and classpath scanning is used. On application reboot, this class can be provided by the IOC Container and instantiated simply invoking the HandlerManager
.
In the next post I’d like present a possible implementation of a Component Factory using the set of responsability pattern in conjunction with another interesting pattern, the builder pattern.
Happy coding!