目錄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()); } ... }