對于一個專案,我需要從 Excel 檔案創建報告。
我有一個很大的設計問題,我自己無法解決,我還沒有在網上找到解決方案。
這是我正在嘗試做的事情:
- 讀取 Excel 檔案的內容
- 使用該內容初始化一個新的 Report 類
- 利潤!
問題是我第一次寫的時候,我把所有的東西都放在了我的 Report 類中(我的意思是讀取檔案、格式??化欄位等),最后我得到了一個大類。
我對此并不滿意,所以我嘗試做一些更好的事情并創建了一個 ReportReader 類,其中包含我所有的閱讀檔案內容以及用于初始化 Report 類的 getter。這是個好主意還是我應該堅持一門課?
另外,在 ReportHeader 中創建一個 createReport 方法而不是使公共 getter 可用是一個好主意嗎?
public class ReportReader {
private final File file;
private final Sheet sheet;
public ReportReader(File file) throws InvalidFormatException
, IOException {
}
public ArrayList<String> getFields() {
}
private String formatField(String input) {
}
public String getName() {
}
public Cell[][] getContent() {
}
public String getType() throws IOException {
}
}
和:
public class Report {
private String name;
private String type;
private ArrayList<String> fields;
private Cell[][] content;
public Report(String name, String type, ArrayList<String> fields,
Cell[][] content) throws IOException {
}
public void saveFieldsModel() throws IOException {
}
public String getFieldsAsCsv() {
}
}
uj5u.com熱心網友回復:
是的,按職責創建一個班級是一種很好的做法。這是SOLID的原則之一。有一篇關于澄清單一責任的好帖子。
在 ReportHeader 中創建一個 createReport 方法而不是使公共 getter 可用是個好主意嗎?
看起來CreateReport與 無關ReportHeader。類Report將是CreateReport()方法更合適的地方。此外,如果您將方法CreateReport()放在Report類中,那么您可以呼叫它Create,而不是CreateReport:
public class Report {
public void Create(){}
}
然后在用戶類中它看起來像這樣:
Report report = new Report();
report.Create(); // not "CreateReport()" :)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/476334.html
