document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  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. }
');