Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why not provide getters for <code>Stats</code>, <code>Health</code>etc., too? Then you don't need to use public instance variables and the calls are not too different:</p> <pre><code>creature.getStats().getHealth().getCurrent(); </code></pre> <p>If you work with an IDE, then it will create getters and setters for you, so in my opinion there is no real excuse for not keeping access to instance variables restricted. It is also a matter of conventions. People are just not used to this kind of thing and others working with your code will be confused much more easily.</p> <p>Concerning calling overridable methods in the constructor: you can always circumvent this if you pass some kind of <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern" rel="noreferrer">Abstract Factory</a> object from the subclass to the parent. You say that your subclasses know how to choose their own Stats and Attacks - instead of using an implementation of an (abstract,) overridable method you propagate an instance of a Factory interface from the concrete implementation to the parent:</p> <pre><code>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()); } } </code></pre> <p>You can define parameters as needed in the Factory methods and this way customize your concrete creature classes as needed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload