interidea 做网站,wordpress 插件交互,做pc端网站代理商,如何seo网站推广接着上一篇文章#xff0c;账薄显示出来之后#xff0c;为了提高软件的可扩展性和灵活性#xff0c;我们应用策略设计模式。这不仅仅是为了提高代码的维护性#xff0c;而是因为明细分类账账薄显示的后面有金额分析这个功能#xff0c;从数据库后台分析及结合Java语言特性…接着上一篇文章账薄显示出来之后为了提高软件的可扩展性和灵活性我们应用策略设计模式。这不仅仅是为了提高代码的维护性而是因为明细分类账账薄显示的后面有金额分析这个功能从数据库后台分析及结合Java语言特性类似数据转置也是软件复杂度提出的一个客观优化需求。
定义策略接口
为了软件的简易性我们采用拖拖拉拉就可形成各种界面元素的设计方式它虽然降低的前端开发人员的难度可以轻松应用CSS配置文件提高视图界面的灵活性但后台的处理多了更多的规则。大的基本结构是一个控制器中同时又镶嵌多个控制器。所以我们定义策略模式的接口如下
/*** 为了方便扩展抽象显示方式目前子类应该包括总分类账和明细分类账*/
public interface ShowZhangBen {/**** param showZongZhangKeMu 要显示的账薄科目* param keMuController 主科目界面的控制器* throws IOException 由于从FXML文件加载要处理异常*/void showZhangBo(KeMu showZongZhangKeMu, KeMuController keMuController) throws IOException;}
实现基本的总分类账和明细分类账的接口实现类
当下业务需要二级科目已经满足要求所以我们先实现这两个策略实现类。按模式惯例在科目主控制器中增加策略成员接口属性然后点击科目根据点击的科目不同去自动的调用策略类的对应实现方式。之所以将策略做为成员属性是因为不同的策略不光是显示方式的不同包括后台处理和其它应该会有很多扩展类都会用引用到所以没有从形式参数方法注入而是从相应的策略实现类中去设置科目控制器成员属性另一个考虑的这样做的原因是在策略实现类中有一次控制器重新从FXML文件加载的过程这样加策略引用交付给主控制器更好的体现了子控制器不仅是一个控制器同时是一个策略的双重角色让主界面控制器的掌控能力更加充分。
具体的实现过程代码如下 /*** 显示总分类账* return 显示面板*/Overridepublic void showZhangBo(KeMu showZongZhangKeMu, KeMuController keMuController) throws IOException {if (showZongZhangKeMu ! null showZongZhangKeMu.getKeMuLevel().equals(1)) //总分类账{keMuController.setStrategyShowZhangBen(new ZhangBenController());}else if (showZongZhangKeMu ! null showZongZhangKeMu.getKeMuLevel().equals(2)) {keMuController.setStrategyShowZhangBen(new MingXiZhangController());keMuController.getStrategyShowZhangBen().showZhangBo(showZongZhangKeMu,keMuController);return;}FXMLLoader loader2 new FXMLLoader();InputStream in ZhangBenController.class.getResourceAsStream(/zhangBen/zhangBen.fxml);loader2.setBuilderFactory(new JavaFXBuilderFactory());loader2.setLocation(ZhangBenController.class.getResource(/zhangBen/zhangBen.fxml));AnchorPane page;try {page (AnchorPane) loader2.load(in);keMuController.setStrategyShowZhangBen(loader2.getController());} finally {in.close();}/*** 控制器自转移*/lbeZhangbBoName((ZhangBenController)keMuController.getStrategyShowZhangBen()).getLbeZhangbBoName();lvZongZhang((ZhangBenController)keMuController.getStrategyShowZhangBen()).lvZongZhang;apZongZhang((ZhangBenController)keMuController.getStrategyShowZhangBen()).apZongZhang;apZongZhangBo((ZhangBenController)keMuController.getStrategyShowZhangBen()).apZongZhangBo;setShowKeMu(showZongZhangKeMu);lbeZhangbBoName.setText(showKeMu.getKeMuName()总分类账);ListZongZhang listData getDataFromSpiZongZhang(showKeMu.getStrDbId());data FXCollections.observableArrayList(listData);//给表格加上行数 2022-08-25TableColumn idCol new TableColumn();idCol.setText(日期);idCol.setCellValueFactory(new PropertyValueFactory(recordDate));// idCol.setVisible(false);TableColumn taskIdCol new TableColumn();taskIdCol.setText(凭证号数);taskIdCol.setCellValueFactory(new PropertyValueFactory(strPingZhengNo));TableColumn costNameCol new TableColumn();costNameCol.setText(摘要);costNameCol.setCellValueFactory(new PropertyValueFactory(strZhaiYao));TableColumn thisAmountCol new TableColumn();thisAmountCol.setText(√);thisAmountCol.setMinWidth(200);thisAmountCol.setCellValueFactory(new PropertyValueFactory(strCheckFlag));TableColumn taskDateCol new TableColumn();taskDateCol.setText(借方);taskDateCol.setMinWidth(200);taskDateCol.setCellValueFactory(new PropertyValueFactory(strJieFangAmount));TableColumn employeeCol new TableColumn();employeeCol.setText(贷方);employeeCol.setMinWidth(200);employeeCol.setCellValueFactory(new PropertyValueFactory(strDaiFangAmount));TableColumn operDateCol new TableColumn();operDateCol.setText(借或贷);operDateCol.setMinWidth(200);operDateCol.setCellValueFactory(new PropertyValueFactory(strAmountDirect));TableColumn yuErCol new TableColumn();operDateCol.setText(余额);operDateCol.setMinWidth(200);operDateCol.setCellValueFactory(new PropertyValueFactory(strYuAmount));// final TableView tableView new TableView();lvZongZhang.getColumns().clear();lvZongZhang.setItems(data);lvZongZhang.getColumns().addAll(idCol, taskIdCol, costNameCol, thisAmountCol, taskDateCol, employeeCol, operDateCol,yuErCol);apZongZhangBo.getChildren().remove(lvZongZhang);apZongZhangBo.getChildren().add(lvZongZhang);setKeMuController(keMuController);keMuController.showDetailAnchorPaneView(apZongZhangBo);}之所以将科目层级和科目账薄类型的判断下移到策略实现类中通过策略路由的方式去处理为了在主界面控制器中增加新的策略时减少或没有代码的修改虽然你增加了一点点代码换来了主界面程序的扩展性这可以根据自己需要决定你认为直接在主界面中去判断然后确定策略类这种效率更高那也是可以的。眼下我追求的只是扩展性和健壮能性相应的明细分类账的实现类如下 /*** 显示总分类账* return 显示面板*/Overridepublic void showZhangBo(KeMu showZongZhangKeMu, KeMuController keMuController) throws IOException {// 使用 Context 来查看当它改变策略 Strategy 时的行为变化。if (showZongZhangKeMu ! null showZongZhangKeMu.getKeMuLevel().equals(1)) //总分类账{keMuController.setStrategyShowZhangBen(new ZhangBenController());keMuController.getStrategyShowZhangBen().showZhangBo(showZongZhangKeMu,keMuController);return;}else if (showZongZhangKeMu ! null showZongZhangKeMu.getKeMuLevel().equals(2)) {keMuController.setStrategyShowZhangBen(new MingXiZhangController());showZongZhangKeMu keMuController.convertChooseToParentKeMu(showZongZhangKeMu);}FXMLLoader loader2 new FXMLLoader();InputStream in MingXiZhangController.class.getResourceAsStream(/zhangBen/mingXiZhang.fxml);loader2.setBuilderFactory(new JavaFXBuilderFactory());loader2.setLocation(MingXiZhangController.class.getResource(/zhangBen/mingXiZhang.fxml));AnchorPane page;try {page (AnchorPane) loader2.load(in);keMuController.setStrategyShowZhangBen(loader2.getController());} finally {in.close();}/*** 控制器自转移*/lbeZhangbBoName((MingXiZhangController)keMuController.getStrategyShowZhangBen()).getLbeZhangbBoName();apZongZhangBo((MingXiZhangController)keMuController.getStrategyShowZhangBen()).apZongZhangBo;lvMingXiZhang((MingXiZhangController)keMuController.getStrategyShowZhangBen()).lvMingXiZhang;apMingXiZhang((MingXiZhangController)keMuController.getStrategyShowZhangBen()).apMingXiZhang;setShowKeMu(showZongZhangKeMu);lbeZhangbBoName.setText(showKeMu.getKeMuName()明细账);ListMingXiZhang listData getDataFromSpiMingXiZhang(showKeMu.getStrDbId());data FXCollections.observableArrayList(listData);//给表格加上行数 2022-08-25TableColumn idCol new TableColumn();idCol.setText(日期);idCol.setCellValueFactory(new PropertyValueFactory(recordDate));// idCol.setVisible(false);TableColumn taskIdCol new TableColumn();taskIdCol.setText(凭证号数);taskIdCol.setCellValueFactory(new PropertyValueFactory(strPingZhengNo));TableColumn costNameCol new TableColumn();costNameCol.setText(摘要);costNameCol.setCellValueFactory(new PropertyValueFactory(strZhaiYao));TableColumn taskDateCol new TableColumn();taskDateCol.setText(借方);taskDateCol.setMinWidth(200);taskDateCol.setCellValueFactory(new PropertyValueFactory(strJieFangAmount));TableColumn daiFangCol new TableColumn();daiFangCol.setText(贷方);daiFangCol.setMinWidth(200);daiFangCol.setCellValueFactory(new PropertyValueFactory(strDaiFangAmount));TableColumn directCol new TableColumn();directCol.setText(借或贷);directCol.setMinWidth(20);directCol.setCellValueFactory(new PropertyValueFactory(strAmountDirect));TableColumn yuErCol new TableColumn();yuErCol.setText(余额);yuErCol.setMinWidth(200);yuErCol.setCellValueFactory(new PropertyValueFactory(strYuAmount));TableColumn[] AmountCol new TableColumn[13];for(int i0;i13;i){AmountCol[i]new TableColumn();AmountCol[i].setText(金额分析String.valueOf(i1));AmountCol[i].setMinWidth(200);AmountCol[i].setCellValueFactory(new PropertyValueFactory(amountFenXiString.valueOf(i1)));}// final TableView tableView new TableView();lvMingXiZhang.getColumns().clear();lvMingXiZhang.setItems(data);lvMingXiZhang.getColumns().addAll(idCol, taskIdCol, costNameCol, taskDateCol, daiFangCol, directCol,yuErCol);for (int i0;iAmountCol.length;i){lvMingXiZhang.getColumns().add(AmountCol[i]);}apZongZhangBo.getChildren().remove(lvMingXiZhang);apZongZhangBo.getChildren().add(lvMingXiZhang);setKeMuController(keMuController);keMuController.showDetailAnchorPaneView(apZongZhangBo);}至此基本功能完成。
策略模式环境角色的客户端调用 这儿策略模式环境角色的特别之处在于它本身可能是通过FXML文件加载进去的类大概率情况如下所以我们不用在意从构造方法中去注入策略接口去赋值通过get和set方法是最佳途径这儿的代码就比较简单了不过也要注意捕捉异常代码如下 Overridepublic void changed(ObservableValue? extends TreeItemKeMu observable, TreeItemKeMu oldValue, TreeItemKeMu newValue) {System.out.println(选择的新值是 newValue.getValue());KeMu chooseKeMu newValue.getValue();try {keMuController.getStrategyShowZhangBen().showZhangBo(chooseKeMu, keMuController);} catch (IOException e) {throw new RuntimeException(e);}}
完成后运行程序效果如下图。至此账薄的基本显示功能完成了这只是相当于买回来两个账薄价值也就是二三十无人民币大量的工作才刚刚开始让我们逐笔开始记录吧。