简体   繁体   English

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

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

I'm working with a service with some kind of complexity so I can't change it so much.我正在使用具有某种复杂性的服务,因此我无法对其进行太多更改。 I need to include a new Boolean field in method method1() interface Pay although it's only relevant in 1 implementation and I should ignore it in the rest:我需要在方法 method1() 接口 Pay 中包含一个新的 Boolean 字段,尽管它仅在 1 个实现中相关,我应该在 rest 中忽略它:

Basically I have something like this:基本上我有这样的事情:

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  {

Somewhere in the client do this:在客户端的某处执行此操作:

getPay(value).method1(); getPay(值).method1(); // This get an instance of PayClassN depending on value // 这会根据值获取 PayClassN 的实例

I'm thinking in 2 different approaches but any convince me:我正在考虑两种不同的方法,但任何一种都能说服我:

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

Any idea how I should approach this?知道我应该如何处理吗? Thanks in advance提前致谢

There are several possibilities.有几种可能性。 The easiest way is to add both methods to the interface and use only the one method you need.最简单的方法是将这两种方法都添加到接口中,并且只使用您需要的一种方法。

The better option would be to make the interface more modular, so that it is not just one interface with all methods, but interfaces with one or two methods.更好的选择是使接口更加模块化,这样它就不仅仅是一个包含所有方法的接口,而是一个或两个方法的接口。

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

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