Reputation: 12295
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
Upvotes: 3
Views: 5272
Reputation: 308743
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.
}
Upvotes: 7
Reputation: 5
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.
Upvotes: 1
Reputation: 56752
Pass them in constructors and hold them in member variables.
If you access them from too many places you probably have a design problem.
Upvotes: 4
Reputation: 87
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).
Upvotes: 2
Reputation: 20782
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.
Upvotes: 1