package design.pattern.behavioral;
import design.pattern.behavioral.ChainOfResponsibility.*;
public class ChainOfResponsibility {
public static class Chain {
private Request[] requests = null;
private Handler[] handlers = null;
public Chain(Handler[] handlers, Request[] requests){
this.handlers = handlers;
this.requests = requests;
}
public void start() {
for(Request r : requests)
for (Handler h : handlers)
if(h.handle(r)) break;
}
}
public static class Request {
private int value;
public Request setValue(int value){
this.value = value;
return this;
}
public int getValue() {
return value;
}
}
public static class Handler<T> {
private Command<T> command = null;
public Handler(Command<T> command) {
this.command = command;
}
public boolean handle(T request) {
return command.execute(request);
}
}
public static abstract class Command<T>{
public abstract Boolean execute(T request);
}
}
class TestChainOfResponsibility {
public static void main(String[] args) {
new TestChainOfResponsibility().test();
}
private void test() {
new Chain(new Handler[]{ // chain of responsibility
new Handler<Request>(
new Command<Request>(){ // command
public Boolean execute(Request condition) {
boolean result = condition.getValue() >= 600;
if (result) System.out.println("You are rich: " + condition.getValue() + " (id: " + condition.hashCode() + ")");
return result;
}
}
),
new Handler<Request>(
new Command<Request>(){
public Boolean execute(Request condition) {
boolean result = condition.getValue() >= 100;
if(result) System.out.println("You are poor: " + condition.getValue() + " (id: " + condition.hashCode() + ")");
return result;
}
}
),
},
new Request[]{
new Request().setValue(600), // chaining method
new Request().setValue(100),
}
).start();
}
}
views:
97answers:
3I don't think there is a meaningful answer to such a general question. Design patterns don't exist in isolation and don't have a "perfect form": they live in a context.
A pattern is a solution to a problem in a context.
So without knowing the context of your solution, there is not much we can say about it. What is the concrete problem you are trying to resolve with it? What forces are in play? What are your constraints? Do you have any problems / issues with the current solution? If you give more details about these, maybe we can give a better answer.
Lambda isn't very descriptive (to most developers). Is it something you are pulling in from functional language theory?
I'd probably just get rid of the 'controlling' class, and wire the individual handlers up to each other directly - use more of an IoC approach, basically.
Example (in C#, forgive me) per request...
public interface IIntMessage
{
void HandleMesasge(int i);
}
public class EvenPrinter : IIntMessage
{
private IIntMessage m_next;
public EvenPrinter(IIntMessage next)
{
m_next = next;
}
public void HandleMesasge(int i)
{
if(i % 2 == 0)
{
System.Console.WriteLine("Even!");
}
else
{
m_next.HandleMesasge(i);
}
}
}
public class OddPrinter : IIntMessage
{
private IIntMessage m_next;
public OddPrinter(IIntMessage next)
{
m_next = next;
}
public void HandleMesasge(int i)
{
if(i%2 == 1)
{
System.Console.WriteLine("Odd!");
}
else
{
m_next.HandleMesasge(i);
}
}
}
Note that we get rid of the "controlling" class altogether, and simply allow the request handlers to directly chain to each other, without having to go through an intermediary.
Also, I could probably extract out a 'base' chain-of-command request handler, removing some of the duplicate code.