我对在设计类时使用的一些技术有疑问。我将它的一些成员声明为public final而不是private,并且构造函数调用可重写的方法。我知道这些通常被认为是不好的做法,但我认为它们在我的情况下可能是合理的,我想知道别人是怎么想的。
这是我的班级:
/** A monster that fights other monsters in my program */
public abstract class Creature
{
/**
* Keeps track of the different values determining how powerful a Creature
* is.
*/
public static class Stats
{
//subclass definition here.
//This subclass contains a lot of public static members like this:
public final CurMax health;
}
public final static Random RANDOM = new Random();
//These are the public final members that I'm wondering about.
public final Stats stats;
public final Attack[] attacks;
public final Stopwatch turnStopwatch;
private String name;
//This is the constructor I'm wondering about.
public Creature()
{
initMembers();
stats = generateStats();
name = RandomValues.NAMES[RANDOM.nextInt(RandomValues.NAMES.length)];
attacks = generateAttacks();
turnStopwatch = new Stopwatch(false);
}
/**
* Define any member variables for subclasses in Creature in this method,
* not in the subclasses' constructors.
*
* Using the Creature class requires the Constructor to call overridable
* methods. This is deliberate because the constructor assigns values to
* final member variables, but I want the values of these variables to be
* determined by subclasses. This method allows subclasses to assign values
* to their own member variables while still controlling the values of the
* final variables in the Creature class.
*
* @see #generateAttacks()
* @see #generateStats()
*/
protected abstract void initMembers();
protected abstract Stats generateStats();
protected abstract Attack[] generateAttacks();
public boolean isDead()
{
return stats.health.getCurrent() <= 0;
}
public String getName()
{
return name;
}
}
我将成员变量声明为public final,因为我计划经常使用它们,而创建方法来控制对它们的访问将会很繁琐。例如,我计划在整个程序中编写这样的行:creature.stats.health.getCurrent();
creature.stats.health.resetMax();
为了避免让公众访问属性和生命值,需要在整个Creature类中编写getCurrentHealth()
和resetMaxHealth()
之类的方法。除了构造函数之外,CurMax类还有10个方法,并且stats类有12个与CurMax类型相似的成员,因此这将需要在Creature类中编写100多个额外的函数。鉴于此,我使用公共最终成员的方式是否合适?如果不是,还有什么其他的技术会更令人满意?
如果使用public final成员是可以的,那么在构造函数中使用可重写的方法呢?我想允许生物的每个子类确定自己的算法来创建状态和攻击数组。例如,一个生物可能会从列表中选择一些随机攻击,而另一个生物可能会选择对另一个特定生物有效的攻击。由于stats
和attacks
是final变量,它们必须在Creature的构造函数中定义。我的解决方法是让构造函数调用可重写的方法,以允许子类确定stats
和attacks
的值,同时将实际赋值留在构造函数中。
我知道,在构造函数中使用可重写方法的主要风险是,在子类有机会定义自己的数据成员之前,将调用被重写的方法。我认为在我的情况下可以避免这种情况,因为generateStats()和generateAttacks()方法只在构造函数中使用。此外,我还添加了另一个抽象方法initMembers,它在Creature构造函数中被调用。子类可以在generateStats()和generateAttacks()被调用之前在这个函数中定义任何成员变量。
Stats和Attack的构造函数很大,所以我不能简单地将Stats对象和攻击数组传递给构造函数。在子类的构造函数中对super()的调用将长得不可接受。
我在生物构造函数中使用可重写方法的方式是合理的吗?如果不是,我还应该怎么做?
为什么不为Stats
, Health
等提供getter呢?这样就不需要使用公共实例变量,调用也不会有太大的不同:
creature.getStats().getHealth().getCurrent();
如果您使用IDE,那么它将为您创建getter和setter,所以在我看来,没有理由不限制对实例变量的访问。这也是一个惯例问题。人们只是不习惯这种事情,其他人使用你的代码会更容易感到困惑。
关于在构造函数中调用可重写的方法:如果你将某种抽象工厂对象从子类传递给父类,你总是可以绕过这个问题。您说您的子类知道如何选择自己的状态和攻击-而不是使用(抽象的)可重写方法的实现,而是将工厂接口的实例从具体实现传播到父类:
public interface CreatureFactory {
public Stats newStats();
public Attack newAttack();
}
public class Creature {
public Creature(CreatureFactory f) {
this.stats = f.newStats();
this.attack = f.newAttack();
}
}
public class Monster extends Creature {
public Monster() {
super(new MonsterFactory());
}
}
您可以根据需要在Factory方法中定义参数,这样可以根据需要定制具体的生物类。
为什么不能使generateStats()和generateAttack()方法抽象?