Advertisement
fcamuso

Singleton in Java

Jul 4th, 2022
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.reflect.Constructor; //reflection
  3. import java.lang.reflect.InvocationTargetException; //reflection
  4.  
  5. public class Main {                        //per Class.forName
  6.     public static void main(String[] args) throws ClassNotFoundException {
  7. //        Singleton s1 = Singleton.getInstance();
  8. //        Singleton s2 = Singleton.getInstance();
  9.  
  10.           //Class.forName("SingletonNoLazy");
  11.           //Class.forName("Singleton");
  12.  
  13.         //credits: Keval Patel on Medium
  14.         //Singleton istanza1 = Singleton.getInstance();
  15.  
  16.         //otteniamo una seconda istanza con la reflection
  17. //        Singleton istanza2 = null;
  18. //        try {
  19. //            Class<Singleton> ref = Singleton.class;
  20. //            Constructor<Singleton> costruttore = ref.getDeclaredConstructor();
  21. //            costruttore.setAccessible(true);
  22. //            istanza2 = costruttore.newInstance();
  23. //        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
  24. //            e.printStackTrace();
  25. //        } //fine reflection
  26.  
  27.         //otteniamo una seconda istanza con serializzazione / deserializzazione della prima
  28.         //credits: Keval Patel on Medium
  29. //        try {
  30. //            ObjectOutput out = null;
  31. //
  32. //            //  serializziamo
  33. //            out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
  34. //            out.writeObject(istanza1);
  35. //            out.close();
  36. //
  37. //            //deserializziamo da file a un un oggetto
  38. //            ObjectInput in = new ObjectInputStream(new FileInputStream("filename.ser"));
  39. //            Singleton istanza2 = (Singleton) in.readObject();
  40. //            in.close();
  41. //
  42. //            System.out.println("instance1 hashCode=" + istanza1.hashCode());
  43. //            System.out.println("instance2 hashCode=" + istanza2.hashCode());
  44. //
  45. //        } catch (ClassNotFoundException | IOException e) {
  46. //            e.printStackTrace();
  47. //        }
  48.  
  49. //        Singleton istanza2 = Singleton.getInstance();
  50.  
  51. //        EnumSingleton enumSingleton = EnumSingleton.INSTANCE;
  52. //
  53. //        EnumSingleton istanza1 = EnumSingleton.INSTANCE;
  54. //        EnumSingleton istanza2 = EnumSingleton.INSTANCE;
  55. //
  56. //        System.out.println("instance1 hashCode=" + istanza1.hashCode());
  57. //        System.out.println("instance2 hashCode=" + istanza2.hashCode());
  58. //
  59. //        System.out.println(enumSingleton.getValue());
  60. //        enumSingleton.setValue(300);
  61. //        System.out.println(enumSingleton.getValue());
  62.  
  63.           EnumSingleton.f();
  64.  
  65.     }
  66. }
  67. //--------------------------------------------- fine main --------------------------------------------
  68.  
  69.     Singleton() {
  70.         // costruttore privato
  71.         System.out.println("Invocato il costruttore");
  72.     }
  73.  
  74.     public static Singleton getInstance() {
  75.         return ContenitoreIstanza.instance;
  76.     }
  77.  
  78.     private static class ContenitoreIstanza {
  79.         private static Singleton instance = new Singleton();
  80.     }
  81. }
  82. //---------------------------------------------------------------------------------
  83. public class SingletonNoLazy {
  84.     private SingletonNoLazy() {
  85.         System.out.println("invocato costruttore no Lazy");
  86.     }
  87.  
  88.     private static SingletonNoLazy instance = new SingletonNoLazy();
  89.  
  90.     public static SingletonNoLazy getInstance() {
  91.         return instance;
  92.     }
  93. }
  94.  
  95. //-----------------------------------------------------------------------
  96. public enum EnumSingleton {
  97.     INSTANCE;
  98.     EnumSingleton() {System.out.println("ciao");}
  99.  
  100.     //public static void f() {}
  101.  
  102.     int value;
  103.  
  104.     public int getValue() {
  105.         return value;
  106.     }
  107.  
  108.     public void setValue(int value) {
  109.         this.value = value;
  110.     }
  111.  
  112. }
  113.  
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement