user63898
user63898

Reputation: 30885

What pattern to use for many API's invocations

i what to ask your advice about which Design Pattern to use in this scenario. i have server that will accept requests with parameters.
in the server i will get those parametrs and according to them i need to invoke different API's (in house apis).
for example :

i got parameters 
x=3,y=0,z=9

i need to invoke API's
if(x>3 && y>0)
{
invoke inner api 1
invoke inner api 2
invoke inner api 3
write_to_log()
if(failed)
  invoke inner api 9 

}
else if(x==3)
{
invoke inner api 5
invoke inner api 6
invoke inner api 7
write_to_log()
 if(failed)
  invoke inner api 9 
}
else if(z<9 && x <3)
{
invoke inner api 6
invoke inner api 1
write_to_log()
if(failed)
  invoke inner api 9 
}
..
..
..

and so on i have something like 10 conditions very long if else . how can i encapsulate it to Design pattern so when in the futurei will have another condition it will be easy to add and maintain

Upvotes: 1

Views: 100

Answers (1)

mpm
mpm

Reputation: 20155

You can try the "chain of responsability" pattern with the "command" and the "macro command pattern".

http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

the chain will decide which service is called based on a condition encapsuled in a responsability. if the responsability cannot handle the request , then it will pass the request to another responsability. If none of the responsabilites can handle the request then you can throw an error. You can make the responsabilities call macro commands with the command pattern.

Upvotes: 1

Related Questions