Reputation: 415
i am having a constructor with Factory pattern. i am passing to many parameters. how to refactor it.
ServerFactory serverFactory = new ServerFactory();
CalendarResults calResults= serverFactory.getResults(serverName,locale, Day, week,
month,vdate,results,uri, EmailShare, inc, upperLimit,
endLimit,exchWD, YearMonthDay,WeekMonthDate);
results=calResults.serverNameDay(serverName,locale, Day, week, month,vdate,
results,uri, EmailShare, inc, upperLimit, endLimit, exchWD, YearMonthDay);
public class ServerFactory {
public CalendarResults getResults(String serverName,String locale, String day,
String week, String month,
boolean vdate, ArrayList<CalendarOutput> results, String uri,
List<String> emailShare, int inc, int upperLimit,
int endLimit, NexWebDav exchWD, String yearMonth, boolean
weekMonthDate){
CalendarResults calresults=null;
if(serverName.equals("www.google.com")){
calresults=new Google();
}else{
calresults=new Exchange();
}
return calresults;
}
}
Upvotes: 0
Views: 162
Reputation: 81882
Hard to tell since you don't show all the code (I assume) where all these parameters get used.
But here are some things to consider:
Replaces arguments of primitive type with value objects, this makes it much clearer what parameter is expected in what position.
combine multiple related arguments to value objects. One example that seems obvious is upperLimit, endLimit (and maybe inc) which might end up in an object called Intervall.
replace booleans with different methods. Often booleans are used to perform different algorithms. If that's the case drop the booleans an make seperate methods instead.
If you have still to many and to confusing arguments, you might want to go with a Builder Pattern. This and the following blog posts might be helpfull: http://blog.schauderhaft.de/2012/07/29/named-parameters-in-java/
Upvotes: 1
Reputation: 34367
One other way code be to use String[] for all String
type parameters with little swap in the their position as below:
public CalendarResults getResults(boolean vdate, ArrayList<CalendarOutput> results,
List<String> emailShare, int inc, int upperLimit,
int endLimit, NexWebDav exchWD, boolean weekMonthDate,
String... stringParams){
//You may get your string params as
//stringParams[0] -> serverName
//stringParams[1] -> locale
//stringParams[2] -> day
//stringParams[3] -> week
//stringParams[4] -> month
//stringParams[5] -> uri
//stringParams[6] -> yearMonth
....
While you can use the factory pattern as of now, with only change that all string type paramters are moved towards the end e.g.
CalendarResults calResults= serverFactory.getResults(vdate,results,
EmailShare, inc, upperLimit, endLimit,exchWD, WeekMonthDate,
serverName, locale, Day, week, month, uri,YearMonthDay);
Some level of simplification.
Upvotes: 0
Reputation: 13872
You can have multiple setter methods on serverFactory
and those must be called before calling getResults
.
If those are not called, getResults
should throw an exception.
The sequence could be as follows:
serverFactory.setServerDetails(servername, locale);
serverFactory.setCalendarDetails(day, week, month, vdate, yearmonthday, weekmonthday);
...
...
serverFactory.getResults(results);
Upvotes: 1
Reputation: 6783
Instead of sending so many parameters, consider creating a Value Object class, instantiate that and send that instance as a parameter.
Upvotes: 0
Reputation: 11452
Stringify all of the String parameters and split them into the constructor. But I recommend the current way that you are doing it. It's more readable.
Upvotes: 0