I execute some tests within my application, but before doing that I have to initialize my response information (Execution directory, used resources..etc).
After a specific point I will be able to identify the test type I’m about to execute so now I have to have an appropriate response instance, but this forces me to copy the fields from the old object to the new one manually which causes bugs when things change.
For example, assume I have the following classes:
class BaseResponse{
private int port;
private int trials;
protected BaseResponse(BaseResponse response){
this.port = response.port;
this.trials = response.port;
}
}
class TcpResponse extends BaseResponse{
public TcpResponse(BaseResponse response){
super(response);
}
private int tcpSpecificMetric;
}
So as a start I initialize the BaseResponse class, fill it up, then when I find out I’ll be running a TCP test, I have to initialize a new TcpResponse !
Would someone advice about a good design for this ?
Thanks in advance.
Write a factory (aka virtual constructor) or builder that knows how to create different types of
BaseResponsebased on the parameters passed to it.