How can I bind together two Java classes?
I'm programming a 3D opengl program where I have several different classes. I know how to bind classes to the main class, but I can't bind two separate classes together. I have a class Terrain where I have a list containing some data about the terrain. Then I need that data in another class called Figure which isn't the main class. I have tried to bind these classes together l开发者_如何学运维ike this:
In Terrain class I have:
Figure fig;
public void bindClasses(Figure fg) {
fig = fg;
}
And then in the Figure class I have:
Terrain ter;
public void bindTerrain(Terrain tr) {
ter = tr;
}
And then in both classes I call those functions. Shouldn't that bind them and their variables? At least that's how I have bound my classes with the main class.
Just to start off with terminology. A class is a blueprint that tells you how an instantiated object is - the moment you write new Figure()
, you've created an instance, an object of the class Figure
(often you have several objects of one class). So when you are "binding" above, you are not actually binding classes, you are binding objects.
The above code is fine. By convention, you often write that sort of things with setters, it's not necessary to call them that, but a very common pattern is:
public class Terrain {
Figure fig
public void setFigure(Figure fig) {
this.fig = fig;
}
}
public class Figure {
Terrain ter
public void setTerrain(Terrain ter) {
this.ter = ter;
}
}
To associate the two together you would in your main class do, which, as you see, is pretty much exactly what you would have already, just using the conventional names.
public void init() {
Terrain ter = new Terrain(); // create object of class Terrain
Figure fig = new Figure(); // create object of class Figure
ter.setFigure(fig);
fig.setTerrain(ter);
}
If there's a relationship between the two objects, for instance, you can't create a figure without having a terrain to put it in. Which would make terrain almost "own" figure. You could indicate this by using the constructor on figure.
public class Figure {
Terrain ter
// constructor requires an instance of Terrain,
// since the figure must always be placed in a terrain
public Figure(Terrain ter) {
this.ter = ter;
// let terrain know this is the main figure.
ter.setFigure(this);
}
}
Now your init code would instead look:
public void init() {
Terrain ter = new Terrain(); // create object of class Terrain
Figure fig = new Figure(ter); // create object of class Figure in the terrain
// no setters needed, since figure constructor sets up
// the relationship.
}
two way binding like yours is possible but is "tricky" from a design points view as well as technical.
a way to achieve it is:
public void addFigureToTerrain(Figure f){
//addFigureToTerrainList
f.registerTerrain(this);
}
now I would recommend you think carefully if you need this kind of linkage between the classes ?
usually it is simpler and more practical to have a general object (terrain) that contains a list of objects(figures). then the general object interogates/updates the list (for examples: figure.hasRecievedEventAttack(attack) or figure.whatDoYouWantToDo()) and then the general object(terrain) analyses a answer
For this situation you can use Inversion of Control (IoC) design pattern. An example is Dependency Injection which is a specific implementation of IoC
With this pattern you can avoid the coupling you have between your Terrain and Figure class. (Terrain depends on Figure and Figure depends on Terrain).
In few words you have a Container that will take care of "injecting" the attributes needed for each object so you don't have to hardcode it.
Spring is a container that does this.
精彩评论