views:

919

answers:

5

Hi all

I'm creating a 2d tile based sim game. I have a 2d array of gridSquares, which are accessed and changed from many different classes and methods. Should I pass the 2d array of gridSquares each time, or make it a global? Which is best practice?

I was thinking, would it be an option to create a class which just contains a set of variables which all classes could extend? Is that a good or bad idea / not good practice?

I'm still fairly new to java so I'm still learning lots!

Thanks in advance.

Rel

+1  A: 

If you absolutely need to, in your grid-containing class (A) declare the grid as public and then import the A class statically (import static A;). This will enable you to interact with the grid without extending the A class, at least.

Your code should not be accessing the grid from too many places. Consider re-factoring your code to avoid having to manipulate the grid from all over the place. Separate your concerns. And it's definitely not a good idea to use inheritance, like you mentioned.

Peter Perháč
+2  A: 

If your certain your only going to ever have one '2d array of gridSquares' you could always use the singleton pattern, essentailly making it global.

There are arguements for and against this though, you may find one day you want to be able to preload maps, but the singleton gets in the way (cant create a second instance of a singleton).

Adam
hum. i may eventually extend it to have multiple floors. I guess that way would not be best for this situation
Relequestual
Even if you have multiple floors (essentially going from 2d to 3d), there is still only one global game state. Hence a singleton would still work.
KarstenF
Indeed. I will have to read up on singletons and how to use them. I have not dealt with them before or been taught. Thanks again
Relequestual
can anyone point me to a good singleton pattern tutorial please?
Relequestual
+4  A: 

Pass them in constructors and hold them in member variables.

If you access them from too many places you probably have a design problem.

starblue
may I ask what you mean by "member variables"?Sorry I'm still rather new to java and programming.
Relequestual
These are the per-object variables. Sometimes they are also called fields.
starblue
Sorry I think I'm being really dim here. Could you give me an example please?
Relequestual
oh sorry, I believe I understand now. like int or char. as opposed to objects Integer or String.
Relequestual
+6  A: 

You should not be designing in terms of a data structure. Java's an object-oriented language. Try thinking about your problem as objects interacting. It's not a 2D array; it's a Board object. Build the behavior for manipulating its state into the problem and hide the fact that you happen to have chosen a 2D array.

I don't have all the details of a Board worked out, but it would start like this:

public class Board
{
    // This is what you're passing around now; Board hides it.
    // Square is the abstraction of a position on the Board
    private Square[][] grid;

    public Board(int nRows, int nCols)        
    {
       this.grid = new Square[nRows][];
       for (int i = 0; i < this.grid[i].length; ++i)
       {
           this.grid[i] = new Square[nCols];
       }
    }

    // Now add methods for adding Pieces to the Board, rules for moving them, etc.
}
duffymo
interesting. I have already created a class called GirdSquare for each of the squares. have a 2d array of GirdSquare's allows me to interact with the path finding algorithms with much more ease.Even if I had a Board object, I still need to access individual grid squares, and the best way I thought of doing this was using a 2d array, like grid references.
Relequestual
Maybe, but a Board abstraction would hide the fact completely. For example, if I wrote a computer representation for Monopoly it might be better represented as a LinkedList of Monopoly properties, because you always traverse the board in that way. A Board abstraction would still be useful in that case; your 2D grid would not.
duffymo
I am unsure what you mean by this. I am new to the idea of abstractions and interface classes! Maybe you could give me a bit more detail? What would a Board abstraction look like and how would I use it?
Relequestual
Thanks for the example! Made it much more clear! This way I could also devise ways of keeping the way the grids are accessed for each level, but just have a change level method. :)
Relequestual
As I understand, I would need to make this a singleton class. I'm researching atm, but if anyone finds an example of how to do such a thing and use singleton classes, please let me know.
Relequestual
Final comment, for anyone interested, here is a good tut on making a singleton class! http://www.javacoffeebreak.com/articles/designpatterns/index.html
Relequestual
+1  A: 

What I would recommend it to make it static but create one definite class to read the data from it, say GridSquaresAcessor. In this class you write all the methods to access the array. Even better, make it a private static field of this class to avoid any other code to manipulate in a way not defined in this class.

In every class you need access to the array you can pass one GridSquaresAcessor as a parameter on the constructor and keep it stored in a local variable.

I personally don't like singletons because they make it very hard to test code...

If your code is multithreaded, be sure to make the 2D-array synchronized.

arodrigues
That was sort of what my idea was, except you worded it better.Anyone else think this is a good way of doing it?
Relequestual