目录java
## ————架构之谈
总的来讲,咱们的计算最短路,计算最少换乘,均可以视做计算一条路径的权值之和,而后找到权值之和最小的那一条路径,返回对应的权值。编程
因此个人出发点就是:分离出以下两个操做架构
经过分离上述两个函数,能够大大下降个人单个方法的复杂度,起码,将其分红了两个层次的功能区分的函数,具体实现能够加入更多的层级,分离处理的逻辑。函数
咱们的权值之和的计算,有4种方式:this
因此,我想到了表驱动编程,就是按照switch的逻辑,指派属性对应的操做函数,经过map哈希表的形式实现,这也是个人架构的一个核心所在,经过此种方法下降一些复杂度。code
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接口的类;get
经过传递键:枚举类型;值:实现Operation接口的类,来完成注册操做方法的过程。it
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()); } ... }