简体   繁体   中英

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:

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(); // This get an instance of PayClassN depending on value

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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