How to deal with distinction of parameters in Java? -
i need method expects different parameters, , behave differently depending on parameters, in example:
public void dosomething(a a, b b, c c) { dosomegeneralstuff(); if a.ispresent() { dosomethingwitha(); } if b.ispresent() { dosomethingwithb(); } if c.ispresent() { dosomethingwithc(); } }
an alternative this:
public void dosomething(a a) { dosomegeneralstuff(); dosomethingwitha(); } public void dosomething(b b) { dosomegeneralstuff(); dosomethingwithb(); } public void dosomething(c c) { dosomegeneralstuff(); dosomethingwithc(); }
but have bunch of redundant code in second example, that's why i'm still not satisfied. other patterns deal problem?
you can use inheritance. have common interface of a
, b
, c
.
public void dosomething(common abc) { dosomegeneralstuff(); abc.dosomethingwithabc(); } interface common { void dosomethingwithabc(); } class implements common { class b implements common { class c implements common {
Comments
Post a Comment