File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Where to put method that creates core of a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Where to put method that creates core of a class?" Watch "Where to put method that creates core of a class?" New topic
Author

Where to put method that creates core of a class?

Christoph Czon
Greenhorn

Joined: Jun 20, 2009
Posts: 15
I'm programming a Risk-like strategy game (basically an emulation of the existing board game. i do it only for fun, no commercial interest behind it.)
A main feature of my implementation is that players can design their own maps. To achieve this the game reads in a local text file and creates the territories and continents on its own. So the players only have to supply the game with a correct text file.
Now a big issue that keeps bugging me for days already is the following: I don't know where it is best to put the method that creates the board. I have moved it around the project several times already and I always found something that I didn't like. In my latest implementation I moved it directly into the constructor of class Board. The constructor looks like this now: Board(File inputFile). But then I think it would be better if there was an external class like this: which has a method:

But with this implementation I think again that the board now relies on another class to be created which is not perfect either.
What the method basically does is the following: it reads in the minimum and maximum number of players allowed for the game, the name of the territories, their direct neighbors and their continent. I put the territories in a hashmap with their names as key. The min and max players are final variables that cannot be changed throughout the game. They are important to check whether the game is started with the right amount of players. So as you can see all these things are vital aspects of the board. Thats why I don't like the idea of setters because after reading the input file I dont want that information to be changed.
The reason why I dont want to put the method into class Board but only want to put it directly into its constructor is that there are various members i want to finalize. So I dont want to use setters for that. Thats why I also dont like GameHelper implementation. But with the implementation inside the constructor of class Board I am unhappy too because then the only way to create a board is by using an input file. Of course I could overload the constructor but then I basically provide additional functionality when in the end I will only use the implementation with the input file.
So I keep moving in circles and this takes up much time I could already spend on other stuff of the game.
I would appreciate your ideas on this. So far my classes are all relatively independent from each other and they all serve a single purpose. The only thing is with this method. Thanks!
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187

http://en.wikipedia.org/wiki/Factory_method_pattern

Christoph Czon
Greenhorn

Joined: Jun 20, 2009
Posts: 15
sounds good.
thanks!
 
wood burning stoves
 
subject: Where to put method that creates core of a class?