I have a class Response
which contains a HTTP response with a HTTP status code like 200 or 404 and few other things like a view name and a domain object. But lets focus on the status code. I could use a single class and pass the status as a parameter:
public class Response {
private int status;
public Response(int status) {
this.status = status;
}
}
// in a handler method:
return new Response(HttpStatus.OK);
The other way would be to create a new class for every status code (41 status codes in HTTP 1.1). Like this:
public class Ok extends Response {
public Ok() {
super(HttpStatus.OK);
}
}
// in a handler method:
return new Ok();
public class Created extends Response {
public Created() {
super(HttpStatus.CREATED);
}
}
// in a handler method:
return new Created();
In reality there will be usually more parameters like the view name and the domain object, like this new Response(HttpStatus.OK, "customer", customer)
respective new Ok("customer", customer)
.