// File: randomroll.jsl
// this represents on strategy of loading a die
public class RandomRoll implements RollStrategy
{
public RandomRoll(int i, int j)
{
from = i;
through = j;
}
public int roll()
{
java.util.Random r = new java.util.Random();
int ceiling = through + 1;
int i = from + r.nextInt(ceiling);
return i;
}
private int from;
private int through;
}
// File: dicefactory.jsl
// this class handles the creation of dice
// it loads the die with a rolling strategy as
// part of the initialization of a die
public class DiceFactory
{
public static Rollable create(int i)
{
Rollable d = new Dice(i);
RollStrategy r = new RandomRoll(1, i);
d.load(r);
return d;
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/