Reputation: 353
I'm attempting to make a port of a board game. Each player in the board game selects one of eight different characters. Each character has a special ability and some flavor text but aside from that they are more or the less the same.
Currently I have a BaseHero
class. I created a class for each of the eight different characters and had them extend BaseHero
. However, there will never be multiple instances of the same character. Is this the best way of implementing this?
My alternate idea was to create a CharacterList
class that would include the code for all of the special abilities and boolean tests to determine whether or not they exist in the game, but I feel that would be too unwieldy and distinctly un-modular.
Upvotes: 1
Views: 184
Reputation: 1327
I'd rather model this as a hero having text and some abilities. The texts origin from properties files. May you first load the common texts and than the special ones into a Properties object. I would model each ability with a distinct class. This allows you to model new character classes using different sets of abilities. Maybe the fay has flying and magic while pegasus has flying and speed or so.
Upvotes: 0
Reputation: 2184
Maybe Interfaces/abstract classes is the solution from my point of view, for example:
// the abstract interface which can be called "BaseHero"
public interface Character {
void action();
void special();
// another general methods
}
// The true implementation of the characters
public class HeroOne implements Character{
@Override
public void action() {
// do the actions
}
@Override
public void special() {
// do the specials
HeroOneShapeAfterSpecial();
// and so on
}
public void HeroOneShapeAfterSpecial(){
// Hero One's special method
}
}
// another implementation
public class HeroTwo implements Character{
@Override
public void action() {
// Hero Two action
}
@Override
public void special() {
// Hero Two Special
}
}
//an example to use them
public class Main {
// a way to use those Characters
public static void main(String[] args) {
Character heroOne = new HeroOne();
Character heroTwo = new HeroTwo();
// some commands from what ever input
heroOne.action();
heroTwo.action();
heroOne.action();
heroTwo.special();
//and so on
// another way
LinkedList<Character> charactersTeam = new LinkedList<Character>();
charactersTeam.add(heroOne);
charactersTeam.add(heroTwo);
for(Character character : charactersTeam){
character.special();
}
}
}
Generally speaking, OO Design favors Composition over inheritence (the HAS-A relations is better and more Flexible than the IS-A)
but if you want to use inheritence, you can take a look at the Decorator Pattern
and if you want the subclasses to not to be instantiated more than once, you can make your subclasses Singletons ... like :
Character heroOne = HeroOne.getInstance();
Character heroTwo = HeroTwo.getInstance();
and for the special abilities as you say, the special() method can hokd the special abilities of every character, and since it is abstract, each character will implement it
Upvotes: 1
Reputation: 78
I think BaseHero should be made abstract class and with two abstract methods specialAbility and someFlavorText. You can define the two methods as per needed in the characters own classes as needed.
Upvotes: 1
Reputation: 10540
I would make BaseHero
abstract so that you cannot instantiate it without subclassing it into different characters, while still getting some nice inheritance benefits. You could also enforce the singleton pattern so that only one of each can be created at a time, though it would also be easy to just set a flag for whether that character is taken or not like you said.
CharacterList
should be something else. If you need a list of characters you can just create a List<BaseHero>
. It wouldn't make sense to me to put all that other logic into a List class.
Also, try to follow composition over inheritance while making your characters. For example, character HASA ability instead of character ISA characterWithAbility.
Upvotes: 1
Reputation: 47373
The number of the instances isn't connected to the fact if the class should exist or not. It is completely normal to instantiate a class only once. Your design with BaseHero
sounds reasonable.
Upvotes: 1