I have two classes that need each other, that is, I have an A class with a Class B object and in Class B I have an object of Class A.
What is the best way to do it?
In particular:
I have a class A that manages beacons
(input and output) and on the other hand I have class B that manages the operation of a game. When a beacon
(class A) arrives, it uses game methods (class B), for example iniciarPartida()
and when an event occurs in the game, for example the game ends, it sends a beacon
through the class that manages them that would be class B.
What I thought would be like this:
public Class A{
private B b;
public A(B b){
this.b = b;
b.putA(this)
}
public void iniciarPartida(){
...
}
private finalizarPartida(){
b.enviarBeacon(beacon)
}
}
public Class B{
private A a;
public B(){
a = null;
}
public void putA(A a){
this.a = a;
}
public void beaconRecibido(Beacon beacon){
if(beacon==xxx)
a.iniciarPartida();
}
public void enviarBeacon(Beacon beacon){
beacon.enviar();
}
}
Would this be fine? It may be worth but visually it seems very ugly ... and I do not know if it is the best or the best.