user9179966
user9179966

Reputation:

How to avoid if

I have a Request object with field request_type and number of other fields. request_type can be 'additionRequest' , 'deletionRequest' 'informationRequest'. Based on request_type other fields in Request object are processed differently.

My simple minded approach is

if additionRequest
algorithm1
else if deletionRequest
algorithm2
else if deletionRequest
algorithm3
end

How I can avoid these if statements and still apply proper algorithm?

Upvotes: 1

Views: 101

Answers (5)

Robert Bräutigam
Robert Bräutigam

Reputation: 7744

The object-oriented solution is always to include the logic with the data. In this case include whatever you want a request to do in the request object itself, instead of (presumably) passing out pure data.

Something like

public interface Request {
    Response apply();
}

Anything else, like creating a map of functions, creating a thin abstraction layer or applying some pattern is a workaround. These are suitable only if the first solution can not be applied. This might be the case if the Request objects are not under your control, for example generated automatically or third party classes.

Upvotes: 0

frenziedherring
frenziedherring

Reputation: 2265

Seems to me that perhaps a Command Pattern could come in handy here. If you make an object structure of these commands and encapsulate the algorithm that you want to execute within the object, then you can construct the specific sub-objects and later on use "execute" method of the command to invoke the algorithm. Just make sure you are using polymorphic references:

if additionRequest
  algorithm1
else if deletionRequest
  algorithm2
else if deletionRequest
  algorithm3
end

will become

void theRequestExecutor(Reqest req) {
    req.executeAlgorithm()
}

https://en.wikipedia.org/wiki/Command_pattern

Upvotes: 0

Kaushal
Kaushal

Reputation: 1349

You can create Map of key:String, value :Function of RequestType ReturnType. Depending on type of request it will call corresponding function.

Example:

  Map<String, Function<RequestType, ResponseType> requestProcessors = new HashMap<>;
  requestProcessors.add("additionRequest", this::methodToHandleAddRequest);
  requestProcessors.add("deletionRequest", this::methodToHandleDeleteRequest);

Inside request handler do

 return this.requestProcessors.get(request.request_type).apply(request);

Note you may have to create response interface if different responses are different. Different response type will inherit from response interface

Upvotes: 0

Jordan Motta
Jordan Motta

Reputation: 188

Use HashMap<RequestType, RequestHandler> for this case. RequestHandler can be an interface which will be implemented for each situation you want to handle.

Hope this help.

Upvotes: 0

Jack
Jack

Reputation: 133577

If you want to avoid conditional statements then you can leverage object oriented features such as:

Map<String, Function<Request, Result>> parsers = new HashMap<>();

parsers.put("additionRequest", request -> {
  // parse and generate Result here
  return result;
});

Result result = parsers.get(request.request_type).apply(request);

Upvotes: 4

Related Questions