Java 将条件移动到类时出错

Java 将条件移动到类时出错,java,refactoring,Java,Refactoring,我正在开发一个硬件,它涉及到实现一些重构技术,我遇到了一个我似乎无法发现的错误。我有下面的方法,最初是在Fish类中,后来我转移到一个名为Strategy public class Strategy { public void move (Fish fish, Pond pond) { if (fish.getHunger() < 0.2) { if (fish.getSize() < 7.0)

我正在开发一个硬件,它涉及到实现一些重构技术,我遇到了一个我似乎无法发现的错误。我有下面的方法,最初是在
Fish
类中,后来我转移到一个名为
Strategy

public class Strategy
{
    public void move (Fish fish, Pond pond)
    {
        if (fish.getHunger() < 0.2)
        {
            if (fish.getSize() < 7.0)
            {
                double[] location = pond.findNearestPlant(fish.getX(), fish.getY());
                fish.swimTowards(location[0], location[1]);
            }
            else
            {
                double[] location = pond.findNearestSmallFish(fish.getX(), fish.getY());
                fish.swimTowards(location[0], location[1]);
            }
        }
        else
        {
            // There are more conditionals in this block.  I haven't done anything with them yet so I didn't put them in.
        }
    }
}
回到
Fish
类中,我添加了几行代码来确定要调用哪个类:

import java.lang.Math;
import java.util.Random;

public class Fish
{
    private static Random random = new Random();
    private static int numberOfFish = 0;

    private double hunger;
    private double size;
    private double x;
    private double y;
    private int id;

    // I added this line
    private Strategy strategy;

    private FishReport myFishReport = null;

    public Fish(double x, double y, FishReport report)
    {
        hunger = 0.9;
        size = 1.0;

        // Put it in the pond
        this.x = x;
        this.y = y;

        id = numberOfFish;
        numberOfFish++;

        // I added this code
        if (hunger < 0.2 && size < 7.0)
            this.strategy = new SmallFish();
        else if (hunger < 0.2 && size >= 7.0)
            this.strategy = new BigFish();
        else
            this.strategy = new Strategy();

        myFishReport = report;
        if(myFishReport != null)
        {
            myFishReport.updateHunger(hunger);
            myFishReport.updateSize(size);
            myFishReport.updateLocation(x, y);
        }
    }

    public void age(double timePassed)
    {
        double deltaSize = size * (1 + hunger * Math.exp(-size * timePassed));
        size = size + deltaSize;

        hunger = hunger * Math.exp(-deltaSize/size);

        myFishReport.updateHunger(hunger);
        myFishReport.updateSize(size);   
    }

    public void move(Pond pond)
    {
        strategy.move(this, pond);
    }

    // More methods below that I did not touch.  Left out to keep code as short as possible.
}
我能想到的唯一一件事是,我对
Fish
构造函数中的
BigFish
类的条件是不准确的。我已经盯着它看了一个多小时了,所以我想我需要另一双眼睛来指出我哪里出了问题。如果有比
Fish
构造函数中的条件更极端的东西,请尝试给出与答案相反的提示。就像我说的,这是硬件任务的一部分

提前谢谢你

edit1-我忘了在
Fish
类中添加已更改的
move()
方法。现在修好了


编辑2-已发布测试失败。测试在
Fish
类中调用
age()
方法,因此我也添加了该方法。

不知道代码的其余部分是什么样子,但是如果鱼的
大小或
饥饿程度在构建后发生变化,您将不再使用正确的策略。(目前,您的所有鱼都将使用
策略
策略,而无论是
大鱼
还是
小鱼


将策略选择保留在
move
方法中而不是构造函数中可能更有意义。

你能发布失败的测试吗。好的,我对这些重构模式还是很陌生,所以我只是跟随一些课堂幻灯片中的类似示例。该示例在构造函数中有条件。只要用于策略选择的基础数据是不可变的,这是完全正确的。好的,因为鱼的位置正在改变,所以我应该将条件保留在
move()
函数中?您没有使用位置来确定策略。只有
大小
饥饿
。如果其中任何一个发生了变化,你的策略可能也需要更新。好的,我在重新阅读作业时发现了一些东西:执行此操作后,在Fish中创建一个私有方法UpdateMovestStrategy,该方法根据饥饿和大小变量设置/创建要使用的策略。在Fish中,应该在age方法结束时调用此方法。如果做得好,所有的测试都应该通过。”听起来和你说的一模一样。
import java.lang.Math;
import java.util.Random;

public class Fish
{
    private static Random random = new Random();
    private static int numberOfFish = 0;

    private double hunger;
    private double size;
    private double x;
    private double y;
    private int id;

    // I added this line
    private Strategy strategy;

    private FishReport myFishReport = null;

    public Fish(double x, double y, FishReport report)
    {
        hunger = 0.9;
        size = 1.0;

        // Put it in the pond
        this.x = x;
        this.y = y;

        id = numberOfFish;
        numberOfFish++;

        // I added this code
        if (hunger < 0.2 && size < 7.0)
            this.strategy = new SmallFish();
        else if (hunger < 0.2 && size >= 7.0)
            this.strategy = new BigFish();
        else
            this.strategy = new Strategy();

        myFishReport = report;
        if(myFishReport != null)
        {
            myFishReport.updateHunger(hunger);
            myFishReport.updateSize(size);
            myFishReport.updateLocation(x, y);
        }
    }

    public void age(double timePassed)
    {
        double deltaSize = size * (1 + hunger * Math.exp(-size * timePassed));
        size = size + deltaSize;

        hunger = hunger * Math.exp(-deltaSize/size);

        myFishReport.updateHunger(hunger);
        myFishReport.updateSize(size);   
    }

    public void move(Pond pond)
    {
        strategy.move(this, pond);
    }

    // More methods below that I did not touch.  Left out to keep code as short as possible.
}
public void testMoveStarving()
{
    // Create a new fish and report
    FishReport report = new FishReport();
    Fish fish = new Fish(10, 20, report);

    // Make a pond
    Pond pond = new Pond();

    // Grow the fish until starving
    fish.age(20);
    fish.age(20);
    fish.age(20);
    fish.age(20);

    // Move the fish, and check the new location
    fish.move(pond);

    // Check the new location
    assertEquals("Starving fish should move", 10.2981, report.getLocation()[0], 0.001);
    assertEquals("Starving fish should move", 20.5963, report.getLocation()[1], 0.001);
}