How to avoid big if statements?

I am sure there is another way to do this, but I am not sure how to do it. I have different Report objects (for example, Report1, Report2, Report3, etc.). I have a service object that will accept these report objects as two arguments in a method. In the method, I have a big if else block statement that determines which two report objects were passed to the method. This logic does not seem like OO, there is no better way to determine which two reports I went through and then provide the correct logic for these two reports, so I don’t have an if-else block that goes out of control and ends up lasting more than 100 lines? Will the command template work well or is there something better that is more object oriented?

Report1 r1 = new Report1(); Report2 r2 = new Report2(); Report3 r3 = new Report3(); Report4 r4 = new Report4(); etc... SomeServiceObject serviceObj = new SomeServiceObject(); var returnedData1 = serviceObj.GetReportLogic(r1, r2); var returnedData2 = serviceObj.GetReportLogic(r1, r3); var returnedData3 = serviceObj.GetReportLogic(r3, r4); etc.. public GetReportLogic(object someReport1, object someReport2) { if ((someReport1 as Report1) and (someReport2 as Report1)) { DoSomething(); } else if ((someReport1 as Report1) and (someReport2 as Report2)) { DoSomethingElse(); } else if ((someReport1 as Report1) and (someReport2 as Report3)) { DoSomethingElseAgain(); } etc... } 
+4
source share
2 answers

Ok, my C # is a little rusty, but can you not use method overloading to achieve this? That is, define several methods

 public GetReportLogic(Report1 someReport1, Report1 someReport2) public GetReportLogic(Report1 someReport1, Report2 someReport2) public GetReportLogic(Report2 someReport1, Report2 someReport2) 

...

with different implementations for different arguments?

You can also have a method called, for example, a combination (Report r) defined in your Report classes, and then each report determines how to combine with other reports.

+8
source

Check if chain of responsibility template is right for you (metacode):

 public class ChainOfResponsibility { ChainOfResponsibility _next; Type _t1; Type _t2; Action _action; public ChainOfResponsibility(Type t1, Type t2, Action action) { _t1 = t1; _t2 = t2; _action = action; } public void Execute(object o1, object o2) { if(CriteriaMatches(o1, o2)) { _action(); return; } if(_next != null) _next.Execute(o1,o2); } public ChainOfResponsibility SetNext(Type t1, Type t2, Action action) { _next = new ChainOfResponsibility(t1,t2,action); return _next; } private bool CriteriaMatches(object o1, object o2) { return (o1 as t1) and (o2 as t2); } } 

And the use will be:

 public GetReportLogic(object someReport1, object someReport2) { var chain = new ChainOfResponsibility(typeof(Report1), typeof(Report1), DoSomething) .SetNext(typeof(Report1), typeof(Report2), DoSomethingElse) .SetNext(typeof(Report1), typeof(Report3), DoSomethingElseAgain); chain.Execute(someReport1, someReport2); } 

I wrote a version of the template for the code that you provided. Feel free to adapt it to your real needs.

0
source

Source: https://habr.com/ru/post/1486444/


All Articles