簡體   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