1
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();
    }
}
4

3 回答 3

4

我认为对于这样一个笼统的问题没有有意义的答案。设计模式不是孤立存在的,也没有“完美的形式”:它们存在于上下文中。

模式是在上下文中解决问题的方法。

因此,在不了解您的解决方案的上下文的情况下,我们无话可说。你试图用它解决的具体问题是什么?有哪些力量在起作用?你的限制是什么?您对当前解决方案有任何问题/问题吗?如果您提供有关这些的更多详细信息,也许我们可以给出更好的答案。

于 2010-06-15T19:23:02.653 回答
2

Lambda 的描述性不是很强(对大多数开发人员而言)。这是您从功能语言理论中汲取的东西吗?

于 2010-06-15T19:52:22.460 回答
0

我可能会摆脱“控制”类,并直接将各个处理程序相互连接 - 基本上使用更多的 IoC 方法。

每个请求的示例(在 C# 中,请原谅我)...

    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);
            }
        }
    }

请注意,我们完全摆脱了“控制”类,只允许请求处理程序直接相互链接,而无需通过中介。

此外,我可能会提取出一个“基本”命令链请求处理程序,删除一些重复的代码。

于 2010-06-15T19:54:09.697 回答