See also ebooksgratis.com: no banners, no cookies, totally FREE.

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
Privacy Policy Cookie Policy Terms and Conditions
Lazy initialization - Wikipedia

Lazy initialization

Da Wikipedia, l'enciclopedia libera.

Nella programmazione software, si dice Lazy initialization (inizializzazione pigra) la tattica di instanziare un oggetto, inizializzare una variabile, effettuare un calcolo od eseguire un processo solo nel momento in cui tale operazione è richiesta. Tipicamente, questo si ottiene memorizzando in un flag l'avvenimento di un determinato processo. Ogni volta che avviene un certo evento, si esamina il flag. Se questo è abbassato, si continua, altrimenti si inizializza una certa variabile o si instanzia un certo oggetto.

[modifica] La "lazy factory"

Dal punto di vista dei design pattern, la lazy initialization si usa spesso con un factory method. Questo combina tre idee:

  • usare un factory method per instanziare una classe;
  • memorizzare l'istanza di una mappa, in modo tale da poter riprendere la stessa istanza la volta successiva che si richiede la stessa con certi parametri (confronta con un singleton);
  • usare la lazy initialization per istanziare un oggetto la prima volta che è richiesto.


Un piccolo esempio (in Java). La classe Fruit non fa nulla qui, questo è solo un esempio per mostrare l'architettura. La variabile types è una mappa usata per memorizzare le istanze di Fruit per tipo.

import java.util.*;
 
 public class Fruit {
     private static Map types = new HashMap(); 
     private String type;      
 
     // si usa un costruttore privato per forzare l'uso del metodo factory.
     private Fruit(String type){
         this.type=type;
         types.put(type, this);
     }
 
     /**
      * Lazy Factory method, gets the Fruit instance associated with a
      *   certain type. Instantiates new ones as needed.
      * @param   type    Any string that describes a fruit type, e.g. "apple"
      * @return          The Fruit instance associated with that type. 
      */
     public static Fruit getFruit(String type){
         Fruit f;
         if (types.containsKey(type)){
             f = (Fruit) types.get(type); // get the instance for that type   
         } else {
             f = new Fruit(type); // lazy initialization
         }
         return f;
     }
 }

La tecnica può essere usata anche in un linguaggio non-object-oriented.

[modifica] Voci correlate

Altre lingue


aa - ab - af - ak - als - am - an - ang - ar - arc - as - ast - av - ay - az - ba - bar - bat_smg - bcl - be - be_x_old - bg - bh - bi - bm - bn - bo - bpy - br - bs - bug - bxr - ca - cbk_zam - cdo - ce - ceb - ch - cho - chr - chy - co - cr - crh - cs - csb - cu - cv - cy - da - de - diq - dsb - dv - dz - ee - el - eml - en - eo - es - et - eu - ext - fa - ff - fi - fiu_vro - fj - fo - fr - frp - fur - fy - ga - gan - gd - gl - glk - gn - got - gu - gv - ha - hak - haw - he - hi - hif - ho - hr - hsb - ht - hu - hy - hz - ia - id - ie - ig - ii - ik - ilo - io - is - it - iu - ja - jbo - jv - ka - kaa - kab - kg - ki - kj - kk - kl - km - kn - ko - kr - ks - ksh - ku - kv - kw - ky - la - lad - lb - lbe - lg - li - lij - lmo - ln - lo - lt - lv - map_bms - mdf - mg - mh - mi - mk - ml - mn - mo - mr - mt - mus - my - myv - mzn - na - nah - nap - nds - nds_nl - ne - new - ng - nl - nn - no - nov - nrm - nv - ny - oc - om - or - os - pa - pag - pam - pap - pdc - pi - pih - pl - pms - ps - pt - qu - quality - rm - rmy - rn - ro - roa_rup - roa_tara - ru - rw - sa - sah - sc - scn - sco - sd - se - sg - sh - si - simple - sk - sl - sm - sn - so - sr - srn - ss - st - stq - su - sv - sw - szl - ta - te - tet - tg - th - ti - tk - tl - tlh - tn - to - tpi - tr - ts - tt - tum - tw - ty - udm - ug - uk - ur - uz - ve - vec - vi - vls - vo - wa - war - wo - wuu - xal - xh - yi - yo - za - zea - zh - zh_classical - zh_min_nan - zh_yue - zu -