Classe Singleton


/ Published in: Java
Save to your folder(s)

Classe Singleton


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.