您希望在责任链的 Java 实现中纠正和/或改进哪些内容?

发布于 2024-09-05 18:41:52 字数 2697 浏览 11 评论 0原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦太阳 2024-09-12 18:41:52

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

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

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

I 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.

苏佲洛 2024-09-12 18:41:52

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

Lambda isn't very descriptive (to most developers). Is it something you are pulling in from functional language theory?

策马西风 2024-09-12 18:41:52

我可能会摆脱“控制”类,并将各个处理程序直接相互连接 - 基本上更多地使用 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);
            }
        }
    }

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文