当前位置: 首页 > news >正文

优化策略模式,提高账薄显示的灵活性和扩展性

接着上一篇文章,账薄显示出来之后,为了提高软件的可扩展性和灵活性,我们应用策略设计模式。这不仅仅是为了提高代码的维护性,而是因为明细分类账账薄显示的后面有金额分析这个功能,从数据库后台分析及结合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()+"总分类账");List<ZongZhang> 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()+"明细账");List<MingXiZhang> 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 i=0;i<13;i++){AmountCol[i]=new TableColumn();AmountCol[i].setText("金额分析"+String.valueOf(i+1));AmountCol[i].setMinWidth(200);AmountCol[i].setCellValueFactory(new PropertyValueFactory("amountFenXi"+String.valueOf(i+1)));}// final TableView tableView = new TableView();lvMingXiZhang.getColumns().clear();lvMingXiZhang.setItems(data);lvMingXiZhang.getColumns().addAll(idCol, taskIdCol, costNameCol, taskDateCol, daiFangCol, directCol,yuErCol);for (int i=0;i<AmountCol.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 TreeItem<KeMu>> observable, TreeItem<KeMu> oldValue, TreeItem<KeMu> newValue) {System.out.println("选择的新值是:" + newValue.getValue());KeMu chooseKeMu = newValue.getValue();try {keMuController.getStrategyShowZhangBen().showZhangBo(chooseKeMu, keMuController);} catch (IOException e) {throw new RuntimeException(e);}}

完成后,运行程序,效果如下图。至此,账薄的基本显示功能完成了,这只是相当于买回来两个账薄,价值也就是二三十无人民币,大量的工作才刚刚开始,让我们逐笔开始记录吧。

相关文章:

  • java排课管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目
  • python从小白到大师-第一章Python应用(三)应用领域与常见包-数据可视化
  • docker安装、运行
  • 不止于浏览器:掌握Node.js,开启全栈开发新篇章!
  • 学习记录691@spring面试之bean的作用域
  • 《UE5_C++多人TPS完整教程》学习笔记5 ——《P6 在线子系统(Online Subsystem)》
  • 现代浏览器对 es模块 【esm】原生支持
  • C语言第二十四弹---指针(八)
  • 假期2.13
  • Stable Diffusion教程——使用TensorRT GPU加速提升Stable Diffusion出图速度
  • localStorage、sessionStorage、cookie区别
  • Windows 安装和连接使用 PgSql数据库
  • 【Git】Windows下通过Docker安装GitLab
  • 【PyQt】08 - 编辑Tab顺序
  • springboot集成Sa-Token及Redis的redisson客户端
  • 【node学习】协程
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • css的样式优先级
  • Java编程基础24——递归练习
  • Java教程_软件开发基础
  • js数组之filter
  • markdown编辑器简评
  • SOFAMosn配置模型
  • vue:响应原理
  • 大整数乘法-表格法
  • 基于组件的设计工作流与界面抽象
  • 聚簇索引和非聚簇索引
  • 前端临床手札——文件上传
  • 设计模式 开闭原则
  • 设计模式走一遍---观察者模式
  • 用Visual Studio开发以太坊智能合约
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 浅谈sql中的in与not in,exists与not exists的区别
  • # Java NIO(一)FileChannel
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • (12)目标检测_SSD基于pytorch搭建代码
  • (6)STL算法之转换
  • (篇九)MySQL常用内置函数
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .desktop 桌面快捷_Linux桌面环境那么多,这几款优秀的任你选
  • .md即markdown文件的基本常用编写语法
  • .net core 6 集成和使用 mongodb
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • .NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态
  • .net6Api后台+uniapp导出Excel
  • .NET6实现破解Modbus poll点表配置文件
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • .sdf和.msp文件读取
  • @Bean, @Component, @Configuration简析
  • @transactional 方法执行完再commit_当@Transactional遇到@CacheEvict,你的代码是不是有bug!...
  • []sim300 GPRS数据收发程序
  • [BZOJ 1032][JSOI2007]祖码Zuma(区间Dp)
  • [C#]C# winform部署yolov8目标检测的openvino模型