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, ArrayList, double dt)
updatePositions(ArrayList, double dt)
etc.
As I see it, there are two ways to organize them (see code below):
define (possibly static, but not necessarily) methods and forward the arguments for each call
create a temporary object with private member variables and remove argument list.
Note that the Mover object has no private internal state and is just a bunch of algorithms that use the arguments ArrayList, ArrayList, double dt.
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, double dt){...}
/* remove the static keyword if you need polymorphism, it doesn't change the question */
public static Collisions detectCollision(ArrayList, ArrayList, double dt){...}
//etc.
}
Here is a snippet illustrating the second point:
public class Mover{
public Mover(ArrayList, ArrayList, double dt){...}
public void updatePositions(){...}
public Collisions detectCollision(){...}
//etc.
private ArrayList mobiles;
private ArrayList inerts;
//etc.
}
解决方案
I'd recommend you to go with the second variant. Besides the good readability it will also allow you to extend the class later (see SOLID -> open / closed principle). In general, I'd never create such utility classes, as it is not OOP (OOP Alternative to Utility Classes).