繁体   English   中英

一种接口方法中的新参数,但与所有实现无关

[英]New parameter in one interface method but not relevant for all implementations

我正在使用具有某种复杂性的服务,因此我无法对其进行太多更改。 我需要在方法 method1() 接口 Pay 中包含一个新的 Boolean 字段,尽管它仅在 1 个实现中相关,我应该在 rest 中忽略它:

基本上我有这样的事情:

public interface Pay {
    method1();
    FormatPay formatPay();
    methodN();
}


public class PayClass1 implements Pay  {
    method1(); // Implemented with a lot of validation among other things
    FormatPay formatPay(); // Implemented
    methodN(); // Implemented
}

public class PayClass2 implements Pay  {
    method1(); // Implemented with a lot of validation among other things
    FormatPay formatPay(); // Implemented
    methodN(); // Implemented
}

public class PayClassN implements Pay  {

在客户端的某处执行此操作:

getPay(值).method1(); // 这会根据值获取 PayClassN 的实例

我正在考虑两种不同的方法,但任何一种都能说服我:

1) getPay(value).method1(newField);
public interface Pay {
    method1(Boolean newField);

public class PayClass1 implements Pay  {
    method1(Boolean newField) {} // Implemented. I need to do a change here

public class PayClass2 implements Pay  {
    method1(Boolean newField) {} // Implemented. I DON'T need to do a change here so I'm not going to use newField and I don't like to have a field and not using it, probably it's an anti pattern.

2) Second approach it would be to define a new method in the interface but it's the same, it'd only be implemented in PayClass1 and nothing to do in PayClass2

知道我应该如何处理吗? 提前致谢

有几种可能性。 最简单的方法是将这两种方法都添加到接口中,并且只使用您需要的一种方法。

更好的选择是使接口更加模块化,这样它就不仅仅是一个包含所有方法的接口,而是一个或两个方法的接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM