不知道我的战略设计模式有什么问题

时间:2015-12-15 15:13:15

标签: java strategy-pattern

我正在为我的软件工程课程使用策略设计模式,但我不确定它有什么问题。我在getPath()'中遇到的语法错误'重复方法Route并且'令牌公共上的语法错误,在'之后预期的类和'返回类型丢失'但我确实有回报。

package wpi.cs509.routeFinder;

import wpi.cs509.dataModel.*;
import wpi.cs509.dataManager.*;
import wpi.cs509.routeFinder.*;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;

public interface Route {
   public ArrayList<Point> getPath();
}

public DikstraRoute implements Route {
   public DikstraRoute(Point start, Graph g, Point destination) {
        if(source.getId()==destination.getId()){
            ArrayList<Point> point=new ArrayList<Point>();
            point.add(source);
            return point;
        }

        source.minDistance = 0;
        PriorityQueue<Point> pointQueue = new PriorityQueue<Point>();
        pointQueue.add(source);

    while (!pointQueue.isEmpty()) {
        Point u = pointQueue.poll();

            // Visit each edge exiting u
            for (Edge e : g.getNeighbors(u))
            {
                try
                {

                Point v = g.getPointById(e.getePointId()!=u.getId()?e.getePointId():e.getsPointId());

                float weight = e.getWeight();
                double distanceThroughU = u.minDistance + weight;
        if (distanceThroughU < v.minDistance) {
            pointQueue.remove(v);
            v.minDistance = distanceThroughU ;
            v.previous = u;
            pointQueue.add(v);
  //  System.out.println("u.id is"+u.getId());
                }
                }catch(NullPointerException e1)
                {
            System.out.println(e.getId()+"**********"+u.getName());
                }
            }
        }
   }

   @Override
   public ArrayList<Point> getPath() {
        ArrayList<Point> path = new ArrayList<Point>();
        for (Point vertex = destination; vertex != null; vertex = vertex.previous){
            path.add(vertex);
        }

        Collections.reverse(path);
        return path;
   }
}

public PrimsRoute implements Route {
       public PrimsRoute(Point start, Graph g, Point destination) {
        if(source.getId()==destination.getId()){
            ArrayList<Point> point=new ArrayList<Point>();
            point.add(source);
            return point;
        }

        source.minDistance = 0;
        pointQueue.insert(source, source.minDistance);
        while (!pointQueue.isEmpty()) {
            int v = pointQueue.remove();
            scan(Graph G, v);
        }
   }

    @Override
    public ArrayList<Point> getPath() {
        ArrayList<Point> path = new ArrayList<Point>();
        for (Point vertex = destination; vertex != null; vertex = vertex.previous){
            path.add(vertex);
        }

        Collections.reverse(path);
        return path;
   }
}

1 个答案:

答案 0 :(得分:-1)

您的班级声明中遗漏了关键字class。就像您将Route声明为interface一样,您必须将DikstraRoutePrimsRoute声明为class es。例如:

public class DikstraRoute implements Route {
    ...
}