Domanda

Ho più di "quanto è una domanda troppo". Ho una classe Java che definisce diversi getter / setter per l'uso da lezioni esterne (circa 30 complessivamente). Tuttavia, la classe Java stesso richiede l'uso di queste variabili anche in alcuni casi.

Comprendo il concetto di utilizzo dei campi membri anziché dei metodi Gettter all'interno di una classe, ma i getter in questo caso eseguono una funzione (smascherare un numero intero per essere specifico) per creare il valore da restituire.

Allora da una prospettiva di riduzione delle prestazioni e della memoria, per le poche chiamate all'interno della classe che necessitano di tali valori, sono curioso se dovrei ...

a. Basta chiamare il getter

b. Fai il simbolo ovunque abbia bisogno dei valori in tutta la classe, proprio come Getter

c. Creare variabili per contenere quei valori, caricarle chiamando tutti i getter all'avvio e utilizzare quelli all'interno della classe (30 o così numeri interi potrebbero non essere un serio rischio di memoria, ma avrei anche bisogno di aggiungere al mio codice per tenerli Aggiornato se un utente imposta nuovi valori ... poiché il valore viene aggiornato e mascherato).

Qualche idea è apprezzata!

È stato utile?

Soluzione

a) Call the getter - as you pointed out it's the right and clean way in your case.

b) and c) would be premature optimization and most likely do more harm than good (unless you REALLY know that this particular spot will be a hot spot in your code AND your JIT-compiler will not be able to optimize it for you).

If you really hit performance problems at some point, then profile the application and optimize only hot spots manually.

Altri suggerimenti

A. Just call the getter.

From a performance and memory reduction perspective, there is really little to no impact here on constantly re-using the same functions. That's what code reuse is all about.

From a high level execution/performance view, we do something like this:

code: myGetter()
program : push the program state (very few cycles)
program : jump to mygetter (1 clock cycle)
program : execute mygetter (don't know your code but probably very few cycles)
program : save the result ( 1 clock cycle)
program : pop the program state ( very few cycles )
program : continue to next line of code ( 1 clock cycle)

In performance, the golden rule of thumb is to spend your time optimizing what really makes a difference. For all general purposes, disk I/O takes up the most time and resources.

Hope this helps!

Don't Repeat Yourself is the guiding principle here. Trying to save a function call by repeating the same unmasking code throughout a class is a recipe for disaster. I would just call the getter within the class.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top