Posted By

kajinka13 on 02/07/12


Tagged

java Singleton


Versions (?)

Classe Singleton


 / Published in: Java
 

Classe Singleton

  1. public class Singleton {
  2. private static Singleton istance = null; // riferimento all'istanza
  3.  
  4. private Singleton() { } // costruttore
  5.  
  6. public static Singleton getIstance() {
  7. if(istance==null)
  8. synchronized(Singleton.class) {
  9. if(instance == null)
  10. istance = new Singleton();
  11. }
  12. return istance;
  13. }
  14.  
  15. public void metodo() { ... }
  16. ....
  17. }
  18.  
  19. ...
  20. Singleton sg1 = Singleton.getIstance(); // Crea l'istanza
  21. sg1.metodo();
  22. ...

Report this snippet  

You need to login to post a comment.