mardi 27 août 2019

How to remove multiple if blocks using design patterns

I have following functions in my class.

  1. GetAData()
  2. GetBData()
  3. GetCData()

and following Input format:-

public class Input
{
    bool IsGetA{get;set;}
    bool IsGetB{get;set;}
    bool IsGetC{get;set;}
}

Now based on input if have to return the response. a simple solution is

var result;
if(Input.IsGetA)
{
    result+=GetAData();
}
if(Input.IsGetB)
{
    result+=GetBData();
}
if(Input.IsGetC)
{
    result+=GetCData();
}
return result;

How can i remove if conditions using design patterns.

Aucun commentaire:

Enregistrer un commentaire