In java, use a different class depending on the operating system

2

I have a system that works in multi-platform, and to be able to design it I need to know how I could run a class depending on the operating system.

I have created the following simple function Public Boolean esandroid(){....} where if the system is android is true but fale, This way I have handled well placing if everywhere, but it is not practical, I want to know if there is any way to make classes depending on the operating system for example.

Class reproducir (){ 
...Codigo para reproducir en android...
}
Class reproducir (){
...Codigo para reproducir que no sea en android...
}

The idea is that depending on the operating system that executes the corresponding class.

Having two classes in the previous example, reference is made to the same class, and this executes the one corresponding to the operating system where it currently operates. It's possible?

    
asked by Bernardo Harreguy 01.02.2018 в 15:47
source

2 answers

2

Object-oriented programming provides you with a very useful tool in these cases called "polymorphism" .

For your case you have a class called reproducir that will have certain methods like empezar , parar , pausa , getNumCancion , getPosicion , etc. common:

class reproducir {
  private boolean reproduciendo;
  private int posicion, cancion;

  public reproducir() {
    /* Constructor inicializando parámetros comunes */
    reproduciendo = false;
    posicion = 0;
    cancion = 0;
  }

  public void empezar() {
    /* Actualización común de registros internos o estado de
      reproducción */
    reproduciendo = true;
    /* ... */
  }

  public void parar() {
    /* Actualización común de registros internos o estado de
      reproducción */
    posicion = 0;
    reproduciendo = false;
    /* ... */
  }

  public void pausa() {
    /* Actualización común de registros internos o estado de
      reproducción */
    reproduciendo = false;
    /* ... */
  }

  public void pausa() {
    /* Actualización común de registros internos o estado de
      reproducción */
    reproduciendo = true;
    /* ... */
  }

  public int getNumCancion() {
    return cancion;
  }

  public int getPosicion() {
    return posicion;
  }
}

Now you only have to create the implementations dependent on the platform.

Example Android:

class reproducirAndroid extends reproducir {
  public reproducirAndroid() {
    /* Llamamos al constructor padre */
    super();
  }

  @Override
  public void empezar() {
    /* Llamamos a la implementación común de comienzo */
    super.empezar();
    /* Implementamos aquí el método en android */
  }

  @Override
  public void parar() {
    /* Llamamos a la implementación común de parada */
    super.parar();
    /* Implementamos aquí el método en android */
  }

  @Override
  public void pausa() {
    /* Llamamos a la implementación común de pausa */
    super.pausa();
    /* Implementamos aquí el método en android */
  }

  /* No es necesario implementar getNumCancion ni getPosicion */
}

Example Java SE:

class reproducirJava extends reproducir {
  public reproducirJava() {
    /* Llamamos al constructor padre */
    super();
  }

  @Override
  public void empezar() {
    /* Llamamos a la implementación común de comienzo */
    super.empezar();
    /* Implementamos aquí el método en java se */
  }

  @Override
  public void parar() {
    /* Llamamos a la implementación común de parada */
    super.parar();
    /* Implementamos aquí el método en java se */
  }

  @Override
  public void pausa() {
    /* Llamamos a la implementación común de pausa */
    super.pausa();
    /* Implementamos aquí el método en java se */
  }

  /* No es necesario implementar getNumCancion ni getPosicion */
}

You only have to manage which instance to actually create:

reproducir getInstance() {
  if (esandroid()) {
    return new reproducirAndroid();
  } else {
    return new reproducirJava();
  }
}

That instance reproducir returned can do empezar , parar and pausa depending on each platform without having to repeat again and again conditions if within your code and, in addition, you will not have to repeat in each implementation the common code as getNumCancion or getPosicion or common parts of the reproduction state in empezar , parar and pausa .

    
answered by 01.02.2018 / 16:01
source
3

Use the factory design pattern. This pattern allows you to create instances of the same interface with one implemented in concrete but hiding the implementation and creating the objects based on certain parameters (if necessary).

For example, define an interface called Reproductor.java :

public interface Reproductor
{
  void reproducir(string archivo);
  void pausar();
  void cerrar();

}

So now based on the types of operating systems that you would initially support, you create a class that implements the interface.

For Linux:

public ReproductorLinux implements Reproductor
{
  public void reproducir(string archivo)
  {
     // codigo para reproducir en el OS linux
  }

  public void pausar()
  {
     // codigo para  pausar en el OS linux
  }

  public void cerrar()
     // codigo para cerrar en el OS linux
  }
}

Windows:

public ReproductorWindows implements Reproductor
{
  public void reproducir(string archivo)
  {
     // codigo para reproducir en el OS Windows
  }

  public void pausar()
  {
     // codigo para  pausar en el OS Windows
  }

  public void cerrar()
     // codigo para cerrar en el OS Windows
  }
}

Android:

public ReproductorAndroid implements Reproductor
{
  public void reproducir(string archivo)
  {
     // codigo para reproducir en el OS Android
  }

  public void pausar()
  {
     // codigo para  pausar en el OS Android
  }

  public void cerrar()
     // codigo para cerrar en el OS Android
  }
}

Now create another class that would be responsible for creating a specific instance according to the operating system:

public class ReproductorFactory
{
  public static Reproductor crearReproductor()
  {
        if(esWindow)
        {
            return new ReproductorWindows();
        }
        else if(esAndroid)
        {
            return new ReproductorAndroid();
        }
        else if(esLinux)
        {
            return new ReproductorLinux();
        }

        else{
            throw new Exception("Este sistema operativo no es soportado");
        }
  }
}

Then the use is the simplest. Just call the ReproductorFactory.crearReproductor() method whenever you need the player:

Reproductor reproductor = ReproductorFactory.crearReproducto();
reproductor.reproducir("c:/sonido.mp3");

Note the non-existence of if . And if one day you end up supporting another OS, you just have to add the implementation of it and add it to the crearReproductor() method and voila, you will not have to make any more modifications to your entire program besides that.

    
answered by 01.02.2018 в 16:01