【问题标题】:Design pattern recommendation for chain-of-responsibility with task delegation任务委托的责任链设计模式推荐
【发布时间】:2017-08-10 23:22:42
【问题描述】:

我有一个接口,假设有 2 种方法,并有 3 种不同的实现。

public interface IFace {
    public void method1(Param param1);
    public void method2(Param param2);
}

public class A implements IFace {
    public void method1(Param param1){}
    public void method2(Param param2){}
}

public class B implements IFace {
    public void method1(Param param1){}
    public void method2(Param param2){}
}

public class C implements IFace {
    public void method1(Param param1){}
    public void method2(Param param2){}
}

现在我有一个要求,其中只有这两种方法中的一种需要修改,并且有一些先决条件,我决定调用特定的实现,而模式的方式是我可能必须通过那些一一实现。这看起来非常适合责任链,我创建了这样的:

public interface IChain {
    public void method1(Param param1);
    public void setNextChain(IChain iChain);
} 

public class AA implements IChain {
    private IChain chain;

    private IFace a;

    public void method1(Param param1){
        if (thisConditionIsSatisfied(param1)) {
            a.method1(param1);
        } else {
            chain.method1();
        }
    }

    public void setNextChain(IChain chain){
        this.chain = chain
    }

    public void setA(IFace a) {
        this.a = a;
    }
}

public class BB implements IChain {
    private IChain chain;

    private IFace b;

    public void method1(Param param1){
        if (thisConditionIsSatisfied(param1)) {
            b.method1(param1);
        } else {
            chain.method1();
        }
    }

    public void setNextChain(IChain chain){
        this.chain = chain
    }

    public void setA(IFace b) {
        this.b = b;
    }
}

public class CC implements IChain {
    private IChain chain;

    public void method1(Param param1){
        if (thisConditionIsSatisfied(param1)) {
            //process it here
        } else {
            throw new RuntimeException("Couldn't process request.")
        }
    }

    public void setNextChain(IChain chain){
        this.chain = chain
    }

}

如您所见,CC 没有委托给实现 IFace 的类 C,它是一个全新的实现。

这是我展示的一个非常小的部分,但实际上问题是相同的,我在条件评估、异常处理、调用接口的正确实现等方面看到了很多重复。

虽然这比尝试扩展现有接口要好得多,但我想知道是否有任何建议可以使其在未来的可扩展性或 OOPS 模式遵循方面变得更好。

【问题讨论】:

标签: java oop design-patterns object-oriented-analysis


【解决方案1】:

我不这么认为。尽管它看起来好像可以用更少的重复来适应更好的设计,但它似乎并不存在。不过,您的解决方案非常好,在我看来,这可能是最好的方法。

【讨论】:

    【解决方案2】:

    我觉得这不是软件工程问题,而是与设计相关的问题。我的回答如下。

    这总体遵循责任链 (COR) 设计模式(CC 类违规除外)。在 COR 中,所有具体的处理程序(AA、BB、CC)都应该是抽象处理程序类型(最好将其作为通用/默认处理程序代码的位置),绑定在一个链中。在您的实施中,CC 违反了此合同,并且不会将机会传递给下一个处理程序。如果在 N 个处理程序的动态链中 CC 为头,则仅处理 CC 类型的请求。除了抛出异常,CC 可以调用 super.handle() 并委托给后继者(如下代码所示)。

    作为您的主要观点,重复代码来自合同以将其委托给后继者(如果处理程序无法处理,则委托给后继者,如果存在)此代码可以放置在抽象处理程序中,如图所示并由 super.handle() 调用.后继实例也与抽象处理程序一起保存。

    handle() 方法中每个处理程序的作用与 COR 相关代码无关。通过从抽象处理程序扩展,只有每个具体处理程序应该表现得像诚实处理程序。

    public class AbsChain implements IChain {
        private IChain successor; // as each handler has it
        @Override
        public void method1(Param param1) {
            if (getSuccessor() != null) {
                getSuccessor().method1(param1);
            } else {
                // Control comes here for totally unhandled request
            }
        }
    }
    
    public class AA extends AbsChain {
        private IFace a;
    
        public void method1(Param param1){
            // Handle if you can 
            if (thisConditionIsSatisfied(param1)) {
                a.method1(param1);
            } else {
                //else allow next one to try (default behavior)
                super.handleRequest(param1);
            }
        }
    }
    
    public class CC implements IChain {
    
        public void method1(Param param1){
            if (thisConditionIsSatisfied(param1)) {
                //process it here
            } else {
                //throwing Ex here breaks the chain
                super.handleRequest(param1); // keep passing it
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多