I have a class that should have slightly different method implementations based on a parameter I would pass to the class constructor. However, I would like to select which implementation to run at the time of the object creation, and not by always testing a specified parameter. Something of this sort:
public class SomeClass(){
int parameter=-1;
SomeClass(int param){
this.parameter = param;
}
public methodSpecific(){ //For when parameter==1;
Do stuff...
}
public methodSpecific(){ //For when parameter==2;
Do stuff...
}
}
Instead of:
public class SomeClass(){
int parameter=-1;
SomeClass(int param){
this.parameter = param;
}
public methodForAll(){
switch(parameter){
case 1:
Do something...
case 2:
Do something else...
}
}
}
Is there some way I can achieve this?
EDIT: This edit is to clarify the situation and context. Please consider the figure bellow. Rectangles of the same color mean that the implementation of those methods are the same. Rectangles with different colors means even though the name is the same the implementation differs. I need to find a way to implement classA and classB, but without duplicated code, and still allow for class "main" to know that both classA and classB have the same method names and variables.
