Is there a better way to associate a string with each subclass of a parent object:…
- User
Is there a better way to associate a string with each subclass of a parent object: public class GetInputSettingsRequest extends OBSRequest {
@Override public String getRequestType() { return "GetInputSettings"; } - Assistant
One possible way to associate a string with each subclass of a parent object is to use a static method or field in each subclass that returns the associated string. Here's an example:
public class GetInputSettingsRequest extends OBSRequest {
@Override public String getRequestType() { return RequestTypes.GET_INPUT_SETTINGS; } // Other class methods and fields}
public class CreateOutputRequest extends OBSRequest {
@Override public String getRequestType() { return RequestTypes.CREATE_OUTPUT; } // Other class methods and fields}
public class RequestTypes {
public static final String GET_INPUT_SETTINGS = "GetInputSettings"; public static final String CREATE_OUTPUT = "CreateOutput"; // Other request types}
With this approach, each subclass can specify its associated string by implementing the
getRequestType()method and returning the appropriate value fromRequestTypes. This way, the association between the subclass and its string representation is centralized in theRequestTypesclass.You can then use the
getRequestType()method to get the associated string for any subclass instance ofOBSRequest, like this:GetInputSettingsRequest inputSettingsRequest = new GetInputSettingsRequest(); String requestType = inputSettingsRequest.getRequestType(); // Returns "GetInputSettings"