我的代碼片段如下:Java類設計 - 有太多的如果條件
public void execute(Parameters params) {
Long requestType = params.getRequestType();
// Based on the requestType the name of the project would be different
getName(requestType);
// Same as above
getDescription(requestType)
// Project here is a collection of different requests
Long projectId = createProject(name, description)
RequestContents requestContents = params.getRequestContents();
for(RequestContent requestcontent : requestcontents) {
Long requestId = createRequest(name, description, projectId);
updateRequest(requestId, requestContent1);
}
// Based on the requestType, mail content would differ
String mailContent = getMailContent(requestType, projectId)
sendMail(mailContent);
}
的功能sendMail
,createProject
輸出,createRequest
依賴於requestType
,等等這些功能最終將有多個if-else
條件。 建立這個班級的正確方法是什麼,以避免這種情況?
他們實際上稱之爲多態:http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism –
而不是請求,我會重構包含上面的exectue()方法的類。 –
@ jordan002謝謝,模式對我來說很難。所以你的意思是這不是一個戰略模式? –