An interface with different method parameters
Solution 1
you could make an operand-object
public interface Operation {
public int Add(Operand o);
}
or
public interface Operation {
public int Add(Operand... o);
}
Solution 2
A few points worth mentioning regarding the other answers:
- Having and interface with multiple function implementation (one for each set of arguments) violates the interface segregation principle.
- Making the class ignore the unused arguments violates the Liskov substitution principle.
My suggestion would be to implement something like the Command pattern. To decouple the command implementation from its implementations is precisely its intent.
Solution 3
in this case you are not using the same interface, but you can create an hereditary system in which some parameters are automatically inserted:
public class CLASSA{
public int Add(int id,String name){
//something
}
}
public class CLASSB extends CLASSA{
public int Add(String name){
super.Add(this.FIXED_ID, name);
//something
}
}
public class OPERATION extends CLASSA{
public int Add(){
super.Add(this.FIXED_ID, this.FIXED_NAME);
//something
}
}
Solution 4
A better approach would be to declare the parameter as a field in the class. Now pass it as a parameter to the constructor, so now it is specified when a new instance of the class is created.
Solution 5
Two solutions
Either define two methods in the interface:
public interface Operation {
public int addName(String name);
public int addIdName(int id, String name);
}
or use a generic paramter
public interface Operation {
public int add(HashMap<String,String> keyValueParams);
}
But for Operations this should work different.
user1874800
Updated on July 09, 2022Comments
-
user1874800 almost 2 years
I want to declare an interface be use with several classes
this classes have method with different parametersinterface:
public interface Operation { public int Add(); }
class A:
public class CLASSA implement Operation{ public int Add(int id,String name); }
class B:
public class CLASSB implement Operation{ public int Add(String name); }
how to impelement of this interface?
-
El Hocko about 11 yearsnow you can use this interface and simulate different arguments there is no other way to do this, because otherwise, you would need different interfaces
-
El Hocko about 11 yearsbut now, your interface us useless, because Add(int,String) is not defined in here... also, java-convention tells you to use lowercase method names add()
-
Aram Kocharyan about 11 years@cIph3r you're absolutely right, hadn't noticed. OP should also not use full caps on the class name.
-
Aram Kocharyan about 11 yearsThis gives flexibility but needs extra type checking, but this may be a necessary tradeoff in some situations so it's a useful solution imo. It also means you could subclass Operand and add logic for this subclass in an implementation of the interface, allowing any type of arguments to be passed. Note however that
Operands
should readOperand
if that is indeed the name of the class. -
Aram Kocharyan about 11 yearsNote however that it's generally better to add extra details within subclasses and not take them away, since any modifications will result in less maintenance required.
-
kolboc almost 5 yearslink expired but quick google for command pattern should be enough
-
Immanuel over 3 yearswikipedia for the lazy people: Command Pattern