Java的异常处理机制是“责任链”设计模式的一个例子吗? [英] Is Java's exception handling mechanism an example of the Chain of Responsibility design pattern?

查看:300
本文介绍了Java的异常处理机制是“责任链”设计模式的一个例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于责任链 设计模式的设计模式,其基本上允许请求的发送者和请求的接收者之间进行解耦。该请求可以沿链传递,直到有人准备好使用该请求。现在在Java中,当我们指定一个方法能够抛出一个 Exception 时,我们让方法的调用者处理异常。如果不感兴趣,可以在链上进一步传播异常。那么这个过程可以被引用为 设计模式的应用

I have been reading about the Chain of Responsibility design pattern which basically allows a decoupling between the sender of a request and receiver of the request. The request can be passed along a chain until someone is ready to consume the request. Now in Java when we specify that a method is capable of throwing an Exception we let the caller of the method handle the exception. If it is not interested it can further propagate the exception up the chain. So can this process be cited as an application of the Chain of Responsibility design pattern?

推荐答案

你是对的Java中的异常处理基于责任链模式
在此模式中:

You are right. Exception handling in Java is based on Chain of responsibility pattern In this pattern:


  1. 发件人不会知道链中的哪个对象将为其
    请求提供

  2. 链中的每个节点都可以决定提供请求 - >捕获异常,并且

    将其包含在特定应用程序中例外

  3. 每个节点都可以将请求 - >抛出异常转发给直接调用者

  4. 节点不能提供请求 - >叶调用者的工作

  1. Sender will not know which object in the chain will serve its request
  2. Each node in the chain may decide to serve the request --> catching an exception and
    wrapping it with an Application specific exception
  3. Each node can forward the request --> throwing exception to the immediate caller
  4. None of the node can serve the request --> Leaves the job with the caller

因此,异常处理是责任模式链

Hence, Exception handling is a Chain of responsibility pattern

这篇关于Java的异常处理机制是“责任链”设计模式的一个例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆