1

我正在尝试这个问题以准备测试。根据我的理解,这是我最好的答案,但我觉得我可能遗漏了一些重要的东西。我认为我已经过多地改变了 Navigator 的职责,但我看不到更好的方法。

问题是:

public class Navigator
{
    private Route theRoute;

    public Navigator(UserInterface ui) {
        String destination = ui.getDestEntry().getText();
        theRoute = new Route(GPS.getLocation(), destination);
        theRoute.calculateRoute();
    }

    public void display() {
        MapView theMap = new MapView();
        theMap.plot(theRoute);
    }
}

public class GPS {
        public static String getLocation() { ... }
    }

“{ ... }” stands for an algorithm that we do not need to examine, for our purposes.

重构 Navigator 和 GPS 类以符合依赖注入模式。不要改变他们的基本职责。

(a) 您重构的 Navigator 和 GPS 类:(在实际测试中您将有更多空间。)

(b) 注入器代码(就像一个语句序列)

我的回答:

(一种)

public class Navigator {
   private Route theRoute;
   private MapView theMap;

   public Navigator (Route inRoute) {
      theRoute = inRoute;
      theRoute.calculateRoute();
   }

   public void display(MapView inMap) {
      theMap = inMap;
      theMap.plot(theRoute);
   }
}

public class GPS {
    public GPS(); //constructor

    public String getLocation(){...}
}

(二)

喷油器代码:

UserInterface ui = new UserInterface;
String destination = new String(ui.getDestEntry().getText());
GPS gps = new GPS;
Route theRoute = new Route (GPS.getLocation(), destination);
new Navigator(theRoute);
4

3 回答 3

1

有待改善。

public class Navigator {

  private final Route theRoute;
  private final MapView theMap;

  public Navigator(Route inRoute, MapView theMap) {
    theRoute = inRoute;
    this.theMap = theMap;
  }

  public void setup() {
    theRoute.calculateRoute();
  }

  public void display() {
    theMap.plot(theRoute);
  }

}

b) 您的喷油器代码不完整或错误

于 2013-10-27T08:38:17.640 回答
0

具有对的Navigator依赖关系 GPS,因此您需要向导航器添加属性

public class Navigator
{
    private GPS gps;
    private UserInterface ui;

    public Navigator(UserInterface ui, GPS gps) {
        this.ui = ui;
        this.gps = gps;
    }

    public void display() {
        String destination = ui.getDestEntry().getText();
        Route theRoute = new Route(gps.getLocation(), destination);
        theRoute.calculateRoute();
        MapView theMap = new MapView();
        theMap.plot(theRoute);
    }
}

public class GPS {
   public String getLocation() { ... }
}
于 2013-10-27T09:02:03.820 回答
0

我的 c# 重构变体如下所示:

public class ClientCode
{
    void DoNavigations(IDestinationEntry ui, IGPS gps)
    {
        String destination = ui.getDestEntry().getText();
        IRoute theRoute = new Route(gps.getLocation(), destination);
        INavigator nv = new Navigator(theRoute);
        nv.display();
    }
}

public class Navigator : INavigator
{
    private IRoute _theRoute;

    public Navigator(IRoute theRoute)
    {
        _theRoute = theRoute;
        _theRoute.calculateRoute();
    }

    public void display()
    {
        MapView theMap = new MapView();
        theMap.plot(_theRoute);
    }
}

public interface IGPS
{
    string getLocation();
}

public interface INavigator
{
    void display();
}

public interface IDestinationEntry
{
    DestinationEntry getDestEntry();
}

public interface IRoute
{
    void calculateRoute();
}
于 2013-10-27T14:24:16.163 回答