Java 构造函数有许多元素。如何重构(工厂模式)
我有一个工厂模式的构造函数。我正在传递许多参数。如何重构它Java 构造函数有许多元素。如何重构(工厂模式),java,constructor,factory-pattern,Java,Constructor,Factory Pattern,我有一个工厂模式的构造函数。我正在传递许多参数。如何重构它 ServerFactory serverFactory = new ServerFactory(); CalendarResults calResults= serverFactory.getResults(serverName,locale, Day, week, month,vdate,results,uri, EmailShare, inc, upperLimit,
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;
}
}
ServerFactory ServerFactory=newserverfactory();
CalendarResults calResults=serverFactory.getResults(服务器名称、区域设置、日期、星期、,
月份,vdate,结果,uri,EmailShare,inc,上限,
endLimit,exchWD,YearMonthDay,WeekMonthDate);
结果=calResults.serverNameDay(服务器名称、区域设置、日期、周、月、vdate、,
结果、uri、EmailShare、inc、上限、endLimit、exchWD、YearMonthDay);
公共类服务器工厂{
public CalendarResults getResults(字符串服务器名、字符串区域设置、字符串日期、,
弦周,弦月,
布尔值vdate、ArrayList结果、字符串uri、,
列出emailShare、int inc、int上限、,
int-endLimit,NexWebDav exchWD,字符串yearMonth,布尔值
周(月日){
CalendarResults calresults=null;
if(serverName.equals(“www.google.com”)){
calresults=newgoogle();
}否则{
calresults=新交换();
}
返回计算结果;
}
}
将所有字符串参数字符串化,并将它们拆分为构造函数。但我推荐你目前的做法。它更可读。< /P> < P>而不是发送这么多参数,考虑创建一个类,实例化并将该实例作为参数发送。 < P>可以在“代码> Server Cuths//Cube”上有多个SETTER方法,并且在调用<代码> GETMeRESe> <代码>之前必须调用这些设置方法。
如果未调用这些函数,getResults
将引发异常
顺序如下:
serverFactory.setServerDetails(servername, locale);
serverFactory.setCalendarDetails(day, week, month, vdate, yearmonthday, weekmonthday);
...
...
serverFactory.getResults(results);
代码的另一种方法是对所有
String
类型参数使用String[],在其位置上几乎不交换,如下所示:
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
....
某种程度的简化。很难说,因为您没有显示所有使用这些参数的代码(我假设) 但这里有一些事情需要考虑:
- 将基元类型的参数替换为值对象,这样可以更清楚地知道在什么位置需要什么参数
- 将多个相关参数组合到值对象。一个明显的例子是上限、endLimit(可能还有inc),它可能最终出现在一个名为Intervall的对象中
- 用不同的方法替换布尔值。布尔函数通常用于执行不同的算法。如果是这样的话,放弃布尔运算,改为使用单独的方法
- 如果您仍然有许多令人困惑的参数,您可能希望使用构建器模式。这篇和以下博客文章可能会很有帮助:
CalendarResults calResults= serverFactory.getResults(vdate,results,
EmailShare, inc, upperLimit, endLimit,exchWD, WeekMonthDate,
serverName, locale, Day, week, month, uri,YearMonthDay);