OO_JAVA_JML系列第三次作业__架构之谈
2024-09-07 22:34:43
OO_JAVA_JML系列第三次作业
## ————架构之谈
出发点
操作的可分离性
总的来说,我们的计算最短路,计算最少换乘,都可以视作计算一条路径的权值之和,然后找到权值之和最小的那一条路径,返回对应的权值。
所以我的出发点就是:分离出如下两个操作
- 遍历图结构生成两点间路径
- 根据拿到的路径计算权值之和
通过分离上述两个函数,可以大大降低我的单个方法的复杂度,起码,将其分成了两个层次的功能区分的函数,具体实现可以加入更多的层级,分离处理的逻辑。
操作本身的多样性
我们的权值之和的计算,有4种方式:
- 计算最短路径
- 计算最少换乘
- 计算最小票价
- 计算最小不满意度
所以,我想到了表驱动编程,就是按照switch的逻辑,指派属性对应的操作函数,通过map哈希表的形式实现,这也是我的架构的一个核心所在,通过此种方法降低一些复杂度。
实现手段:表驱动编程
储存
T是继承自Enum类型的类型,就是枚举类型。
Operation是一个接口,功能是对给定路径计算对应的权值之和。
public class GraphStructure<T extends Enum> {
...
private HashMap<T, HashMap<SymPair<Node>, Integer>> shortestLength;
private HashMap<T, Operation> operations;
public void setOperation(T type, Operation operation) {
this.shortestLength.put(type, new HashMap<>());
this.operations.put(type, operation);
}
...
private void addShortestLengthPair(T type, Node from, Node to) {
List<List<Node>> lists = TraverseFunc.depthFirstTraversing(from, to, table);
Map<SymPair<Node>, Integer> map = shortestLength.get(type);
Operation operation = operations.get(type);
TraverseFunc.addShortestLength(from, to, lists, edgeColors, map, operation);
}
}
注册
CalculateMethod是上述的泛型,用于表示操作的属性;每个操作都有对应的实现了Operation接口的类;
通过传递键:枚举类型;值:实现Operation接口的类,来完成注册操作方法的过程。
public class MetroSystem extends MyPathContainer implements RailwaySystem {
private GraphStructure<CalculateMethod> graph;
public MetroSystem() {
super();
this.graph = new GraphStructure<>();
this.graph.setOperation(CalculateMethod.shortestRouteLength,
new CalculateRouteLength());
this.graph.setOperation(CalculateMethod.leastTicketPrice,
new CalculateTicketPrice());
this.graph.setOperation(CalculateMethod.leastTransferLength,
new CalculateTransferLength());
this.graph.setOperation(CalculateMethod.leastUnpleasementCount,
new CalculateUnpleasantCount());
}
...
}
最新文章
- ahk之路:利用ahk在window7下实现窗口置顶
- OpenGL在什么样的领域才是主角?
- HttpModule在Web.config的配置和动态配置
- Libcurl笔记三
- 教程-脚本之Python
- linux下tcpdump命令详解
- Spring IOC 之个性化定制the nature of a bean
- reference file contains errors
- HttpURLConnection发送请求
- jQuery学习之旅 Item2 选择器【二】
- vue(3)—— vue的全局组件、局部组件
- FPM四:用OVP做查询跳转到明细
- SQL server 的身份验证模式
- docker之harbor仓库注意事项
- c++ 面试题(网络类)
- java十年技术栈[总结复习用]
- Nginx+Tomcat简单集群
- Dubbo与Zookeeper、Spring整合使用
- 使用UICollectionView
- 读取Excel里面的内容转为DataTable
热门文章
- 常量&;&;变量
- Datagird样式
- 什么是maven与maven的使用过程(例如在idea创建maven工程(重点讲讲idea创建使用maven管理的web工程,并且部署到tomcat上))
- Docker 容器间的单向连接
- 洛谷P1125——笨小猴(简易模拟)
- 用java代码遍历excel文件并回显
- Java基础系列(16)- Scanner进阶使用
- Nginx系列(6)- nginx: [error] CreateFile() ";D:\nginx-1.20.1/logs/nginx.pid"; failed (2: The system cannot find the file specified)
- chrome浏览器中安装以及使用Elasticsearch head 插件
- 怎么使用chrome浏览器查看内存是否有泄漏