Advertisement
Guest User

singletonImpleentation

a guest
Aug 22nd, 2011
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Kamil
  4.  * Implementacja wzorca projektowego Singleton w języku JAVA
  5.  */
  6. public class Singleton {
  7.     // Instancja klasy jako prywatne pole statyczne
  8.     private static Singleton instance = null;                                  
  9.     // Konstruktor prywatny - brak możliwości tworzenia obiektu za pomocą niego
  10.     private Singleton(){
  11.        
  12.     }
  13.     // punkt dostępu zapewniony po przez statyczną metodę getInstance
  14.     public static Singleton getInstance(){
  15.         //
  16.         // Warunek pdwójnego sprawdzania w wypadku utworzenia obiektu przez inny wątek
  17.         if(instance == null){
  18.             synchronized(Singleton.class){
  19.                 if(instance == null) instance = new Singleton();
  20.             }
  21.         }
  22.        
  23.         return instance;
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement