mercredi 16 mars 2016

Java : preferred design when multiple methods share same arguments (class member ?)

Context: I believe that object creation and management in Java has a cost that we should bear in mind while programming. However, I don't know how big that cost is. Hence my question:

I have multiple functions that share the same arguments:

  • detectCollision(ArrayList<Mobile>, ArrayList<Inert>, double dt)
  • updatePositions(ArrayList<Mobiles>, double dt)
  • etc.

As I see it, there is two ways to organize them (see code below):

  1. define static methods and forward the arguments for each call
  2. create a temporary object with private member variables and remove argument list.

Question: Which approach is the prefered one ? Does it have a cost ? Is there a more standard alternative ?

Here is a snippet illustrating the first point:

public class Mover{
    public static void updatePositions(ArrayList<Mobiles>, double dt){...}
    public static Collisions detectCollision(ArrayList<Mobile>, ArrayList<Inert>, double dt){...}
    //etc.
}

Here is a snippet illustrating the second point:

public class Mover{
    public Mover(ArrayList<Mobile>, ArrayList<Inert>, double dt){...}
    public void updatePositions(){...}
    public Collisions detectCollision(){...}
    //etc.

    private ArrayList<Mobile> mobiles;
    private ArrayList<Inert> inerts;
    //etc.
}

Aucun commentaire:

Enregistrer un commentaire