将条件移动到 Class 时出错
Error With Moving Conditional To Class
我正在开发涉及实施一些重构技术的硬件,但我 运行 遇到了一个我似乎无法发现的错误。我有以下方法,该方法最初位于 Fish
class 中,我移入了名为 Strategy
的 class
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.
}
}
}
我正在尝试将 if
块中的条件转换为 2 个单独的 classes,它们也位于我的 Strategy.java
文件中。
class SmallFish extends Strategy
{
public void move (Fish fish, Pond pond)
{
double[] location = pond.findNearestPlant(fish.getX(), fish.getY());
fish.swimTowards(location[0], location[1]);
}
}
class BigFish extends Strategy
{
public void move (Fish fish, Pond pond)
{
double[] location = pond.findNearestSmallFish(fish.getX(), fish.getY());
fish.swimTowards(location[0], location[1]);
}
}
回到 Fish
class 我添加了几行代码来确定调用哪个 class:
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.
}
我的测试套件在添加 SmallFish
class 后仍然通过,但在添加 BigFish
class:
后我失败了
JUnit Test Results:
testMoveStarving(FishTests): Starving fish should move expected:<10.2981> but was:<10.3965129880783>
Total number of tests: 8
Total number of failures: 1
这是失败的测试:
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);
}
我唯一能想到的是我在 Fish
构造函数中对 BigFish
class 的条件不准确。我已经盯着这个看一个多小时了,所以我想我只需要另一双眼睛来指出我哪里出错了。如果有比 Fish
构造函数中的条件更极端的东西,请尝试给出提示而不是答案。就像我说的,这是硬件作业的一部分。
先谢谢你
编辑 1 - 我忘记在 Fish
class 中添加更改后的 move()
方法。现在已经修复了。
编辑 2 - 失败的测试已发布。测试调用 Fish
class 中的 age()
方法,所以我也添加了它。
不知道你的代码的其余部分是什么样子,但如果鱼的 size
或 hunger
在构造后发生变化,你将不再使用正确的策略。 (就目前而言,您所有的鱼都将使用 Strategy
策略,而不是 BigFish
和 SmallFish
。)
将策略选择保留在 move
方法中而不是构造函数中可能更有意义。
我正在开发涉及实施一些重构技术的硬件,但我 运行 遇到了一个我似乎无法发现的错误。我有以下方法,该方法最初位于 Fish
class 中,我移入了名为 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.
}
}
}
我正在尝试将 if
块中的条件转换为 2 个单独的 classes,它们也位于我的 Strategy.java
文件中。
class SmallFish extends Strategy
{
public void move (Fish fish, Pond pond)
{
double[] location = pond.findNearestPlant(fish.getX(), fish.getY());
fish.swimTowards(location[0], location[1]);
}
}
class BigFish extends Strategy
{
public void move (Fish fish, Pond pond)
{
double[] location = pond.findNearestSmallFish(fish.getX(), fish.getY());
fish.swimTowards(location[0], location[1]);
}
}
回到 Fish
class 我添加了几行代码来确定调用哪个 class:
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.
}
我的测试套件在添加 SmallFish
class 后仍然通过,但在添加 BigFish
class:
JUnit Test Results:
testMoveStarving(FishTests): Starving fish should move expected:<10.2981> but was:<10.3965129880783>
Total number of tests: 8
Total number of failures: 1
这是失败的测试:
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);
}
我唯一能想到的是我在 Fish
构造函数中对 BigFish
class 的条件不准确。我已经盯着这个看一个多小时了,所以我想我只需要另一双眼睛来指出我哪里出错了。如果有比 Fish
构造函数中的条件更极端的东西,请尝试给出提示而不是答案。就像我说的,这是硬件作业的一部分。
先谢谢你
编辑 1 - 我忘记在 Fish
class 中添加更改后的 move()
方法。现在已经修复了。
编辑 2 - 失败的测试已发布。测试调用 Fish
class 中的 age()
方法,所以我也添加了它。
不知道你的代码的其余部分是什么样子,但如果鱼的 size
或 hunger
在构造后发生变化,你将不再使用正确的策略。 (就目前而言,您所有的鱼都将使用 Strategy
策略,而不是 BigFish
和 SmallFish
。)
将策略选择保留在 move
方法中而不是构造函数中可能更有意义。