Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Kamil
- * Implementacja wzorca projektowego Singleton w języku JAVA
- */
- public class Singleton {
- // Instancja klasy jako prywatne pole statyczne
- private static Singleton instance = null;
- // Konstruktor prywatny - brak możliwości tworzenia obiektu za pomocą niego
- private Singleton(){
- }
- // punkt dostępu zapewniony po przez statyczną metodę getInstance
- public static Singleton getInstance(){
- //
- // Warunek pdwójnego sprawdzania w wypadku utworzenia obiektu przez inny wątek
- if(instance == null){
- synchronized(Singleton.class){
- if(instance == null) instance = new Singleton();
- }
- }
- return instance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement