I have a Class A with instance of class B and class B has an instance of class A

0

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.

    
asked by JMR 16.12.2016 в 16:06
source

2 answers

2

According to what I read, I would create a 3 class that will do all that, besides applying a design pattern type Observer so that the third class is the observing class and your two classes are observable classes in that way when the class needs to execute a class B method first send a notification to the third class and execute it and in the same way for class B that notifies the class the third class that needs to execute a class A method.

Observer Design Patron:

The design pattern consists of the following, Observer Object and One or some observable objects, the observable objects will notify the observable object and the latter upon being notified will perform some action that you program. Its implementation in the easiest way is as follows.

First we need to create two interfaces, the obersable and the observer.

public interface Observador {
    public void accionObservador();
    //Pueden ser mas metodos
}
public interface Observable {
        public void notificar();
   }

Now in your case it would be implemented in the following way.

          public class C implements Observador{
    //Clase que maneja todo


    public C(){
        A claseA = new A(this);
        B claseB = new B(this);

    }

    @Override
    public void accionObservador() {
        // TODO Auto-generated method stub
        //Alguien me notifico aunque me pueden enviar un parametro para saber de que clase vino con un instance of y otro parametro
        //Para saber que haccion ejecutar de la otra clase
    }

}



      import java.util.ArrayList;

public class B implements Observable{

    private Observador miObservador;

    public B(Observador obj){
        this.miObservador=obj;

    }
    @Override
    public void notificar() {
        // TODO Auto-generated method stub

        //Notifico a mi observador
        this.miOberservador.accionObservador();
    }


    private void saludar(){
        //La Accion saludar hace que se notifique a mi observador que voy a saludar
        //En tu caso voy a llamar a un metodo del A 
        notificar();
    }

}


    public class A implements Observable{

        private Observador miOberservador;
        public A(Observador ob){
            //Le indico quien me va a observa
            this.miOberservador = ob;
        }



        @Override
        public void notificar() {
            // TODO Auto-generated method stub

            //Notifico a mi observador
            this.miOberservador.accionObservador();
        }


        private void saludar(){
            //La Accion saludar hace que se notifique a mi observador que voy a saludar
            //En tu caso voy a llamar a un metodo del B 
            notificar();
        }

    }
    
answered by 16.12.2016 / 17:09
source
0

If the methods are static:

class A {
    public static void metodoA() {}
}

class B {
    public static void metodoB() {}
}

you can call them in the following way:

You create the instance of the classes and call the methods directly:

B b = new B();
b.metodoB()

The same when instantiating class A:

A a = new A();
a.metodoA()

Update:

The user apparently wants to perform circular dependency:

  

I have two classes that need each other, that is, I have a class   A with an object of Class B and in Class B I have an object of the   class A.

     

What is the best way to do it?

A a = new A();
B b = new B();

a.setB(b);
b.setA(a);
    
answered by 16.12.2016 в 16:14