OO_JAVA_JML系列第三次做业__架构之谈

OO_JAVA_JML系列第三次做业


## ————架构之谈

出发点

操做的可分离性

总的来讲,咱们的计算最短路,计算最少换乘,均可以视做计算一条路径的权值之和,而后找到权值之和最小的那一条路径,返回对应的权值。编程

因此个人出发点就是:分离出以下两个操做架构

  1. 遍历图结构生成两点间路径
  2. 根据拿到的路径计算权值之和

经过分离上述两个函数,能够大大下降个人单个方法的复杂度,起码,将其分红了两个层次的功能区分的函数,具体实现能够加入更多的层级,分离处理的逻辑。函数

操做自己的多样性

咱们的权值之和的计算,有4种方式:this

  1. 计算最短路径
  2. 计算最少换乘
  3. 计算最小票价
  4. 计算最小不满意度

因此,我想到了表驱动编程,就是按照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());
    }
 
    ...
    
}
相关文章
相关标签/搜索