jeudi 9 avril 2015

Java avoid Repeated checks in beginning of routes

I have in my java application inside my controllers repeated checks in almost the beginning of every route according to the client input and if the somehow any of checks was false i would return a different http response code according to the condition.


example in a route (pseudo code speaking)



if(!user.exists)
return notFound;
if(!user.isAdmin)
return unAuthorized
.
.
.
.


Except actually that i'm checking in many models rather than user for example.


So I'm trying to make this encapsulated and unified as much as possible and i thought of builder pattern but it has one small issue.


example



public class check
{
private Status status;
public checkUser(int userId)
{
if(status==null)
if(!userId.exists)
status=NotFound;
return this;
}
public checkUserVehicle(int userVehicleId)
{
if(status==null)
if(!userVehicleId.exists)
status=NotFound;
return this;
}
public status getStatus()
{
return status;
}
}


then for example use it in each route use it like this:



status s=new Check().checkUser(5).checkUserVehicle(9);
if(s!=null)
return s;


but this has a problem of checking in each method that status is not null but i actually need to stop checking methods when ever the status is not null.


Is there any design patterns recommended for such problem ?


It accept that it would be handled totally different from using the above implementation.


Aucun commentaire:

Enregistrer un commentaire