假设我有一个接口和它的具体实现,客户端代码使用该实现。现在,使用实现该接口的代理模式,可以将对接口的请求路由到网络上。网络连接可能会失败,这可能会引发异常。
使用接口的客户端代码将会收到意外的异常。我猜在这种情况下违反了LSP原则。
但是如果不允许将网络异常传播出接口,如何处理这种情况呢?
以下是一些Java代码,以澄清我的意思:
我需要更改接口以使异常传播出去吗?
使用接口的客户端代码将会收到意外的异常。我猜在这种情况下违反了LSP原则。
但是如果不允许将网络异常传播出接口,如何处理这种情况呢?
以下是一些Java代码,以澄清我的意思:
interface Interface
{
abstract void method1();
abstract void method2();
abstract void method3();
}
class Implementation implements Interface
{
void method1();
void method2();
void method3();
}
class ProxyOverNetwork implements Interface
{
// I can't add the NetworkException as it is not part of the Interface
// and would violate LSP, but how to handle the network problems then,
// when the ProxyOverNetwork might not be the right place to do so?
void method1() throws(NetworkException);
void method2() throws(NetworkException);
void method3() throws(NetworkException);
}
我需要更改接口以使异常传播出去吗?