As you can see, I've created (instantiated?) a static array of Corner objects in the object Corner. Is this good form? I want all the Corner object to have access to all the other Corner objects.
package Main;
public class Corner {
private String biome;
private static Corner[][] corners;
private float elevation, moisture, heat;
private boolean isRiver, isLake;
private int x, y;
public void createArray(int width, int height) {
corners = new Corner[width][height];
}
public String getBiome() { return biome; }
public void setBiome(String biome) {
this.biome = biome;
}
public float getElevation() { return elevation; }
public void setElevation(float elevation) {
this.elevation = elevation;
}
public float getMoisture() {
return moisture;
}
public void setMoisture(float moisture) {
this.moisture = moisture;
}
public float getHeat() { return heat; }
public void setHeat(float heat) { this.heat = heat; }
public boolean isRiver() {
return false;
}
public boolean isLake() {
return false;
}
public static Corner[][] getCorners() {
return corners;
}
}
There are no more details to add.
If the amount of Corner
s changes you need to create a new bigger array and copy all the Corner
s from the old array to the new bigger one. This should indicate to you that you might want to ave a different data structure than an array. One that can grow like a List
or a Set
.
In general, a Corner
should not need to know of other Corner
s. A different type should manage all the Corner
s and handle dependencies between them.
You did not wrote why 'I want all the Corner
object to have access to all the other Corner objects' so I cannot recommend how this managing type could look like.
First of all, taking in not what Kevin has said, you should change
public void createArray(int, int);
to
public static void createArray(int, int);
I do not understand the need to have a two-dimensional array for accessing other Corner objects.
Also array is not a good structure type for dynamic allocation. List is better alternative and in this case List. So you should implement it as -
You should create a list as -
private static List<Corner>;
In general, static is an abnormality in good OO design. You only use it if there are very good reasons to do so.
Your example doesn't look like such a case. You see, you are mixing up things that don't belong together. A "corner" is simply that a "corner". It doesn't know about other "corners".
In the model that you are actually creating, you have some "enclosing" thing that deals with corners in plural. That enclosing thing could have some List<Corner>
field that is used to track all Corner objects belonging to the enclosing entity.
static often looks like an easy, convenient way to solve such problems. But in reality, you are just creating a lot of problems by implementing something like this. It works initially, but it breaks as soon you try to enhance your program.
Is this good form?
No.
There are two reasons:-
Reason 1 :- Single responsibility principle. Every object should be responsible about its concern . So its not the concern of corner object to maintain the list of other corner object. Maintaining corner object can go under some util class method or singleton object maintaining the corner cache(which will be easy to test also)
Reason 2 :-
unit testing. Say you want to write unit test for static method you won't be able to do it easily until and unless you provide any third party lib that provides static mocking like jmockit,