I have a program that sends requests to a server. There are many different types of request, and each has their own class. For example, I have a checkServerOnlineRequest which sends a short message to the server, or a getAmountOfGoldRequest which sends a very different message.
class CheckServerOnlineReq{
static final byte requestID = 1;
byte[] message;
void setMessage(byte messageNumber){
message = new byte[2];
message[0] = messageNumber;
message[1] = requestID;
}
}
To send the requests, I have a Client class. It has static method called send which I would like to accept any type of request (i.e. a number of different classes)
My question is, how can I set up send()‘s parameters to allow any type of request to be given as an argument.
The canonical way is to declare an interface, and make the concrete request classes implement that interface:
Then the
send()method can acceptIRequestas its argument.