lundi 12 mars 2018

design pattern: static method with extra parameter vs non static

I have two functions one static and one non-static.

solution1

private static int hashCode(int key, int i, int size){
    return ((hashFunc1(key)+i*hashFunc2(key)) % size);
}

solution2

private int size;

private int hashCode(int key, int i){
    return ((hashFunc1(key)+i*hashFunc2(key)) % this.size);
}

If I declare function static it will be shared amongst all the instances of my class and thus will not be implemented separately for each one; however in that case I will have to pass argument size for particular object size every time I call it. In the second solution, I will have a private size variable (of course I will initialize it in a constructor); thus every instance of the class will have its 'own' hashCode function and I will not pass argument size every time. From an memory management and design perspective which solution should be preferred?

Aucun commentaire:

Enregistrer un commentaire