VictorHTorres86

Singleton con subclasificación en Java

Oct 23rd, 2021 (edited)
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.lang.reflect.InvocationTargetException;
  5.  
  6. abstract class Singleton {
  7.  
  8.     private static Singleton instance;
  9.    
  10.     protected Singleton() {}   
  11.    
  12.     public static Singleton getInstance() {
  13.         if (instance == null) {
  14.             String line = Singleton.getSingletonConfigure();
  15.             try {
  16.                 instance = (Singleton)Class.forName("com.design_patterns.singleton." + line)
  17.                                 .getDeclaredConstructor().newInstance();
  18.             } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
  19.                     | InvocationTargetException | NoSuchMethodException | SecurityException
  20.                     | ClassNotFoundException e) {
  21.                 System.out.println("Falló la configuración.");
  22.             }
  23.         }
  24.         return instance;
  25.     }
  26.  
  27.     private static String getSingletonConfigure() {    
  28.         BufferedReader in = null;
  29.         String className = "";
  30.         try {
  31.             in = new BufferedReader(new FileReader("singletonConfigure"));
  32.             className = in.readLine();
  33.         } catch (IOException ex) {
  34.             System.out.println("IOException al leer: " + ex.getMessage());
  35.         } finally {
  36.             if (in != null) {
  37.                 try {
  38.                     in.close();
  39.                 } catch (IOException ex) {
  40.                     System.out.println("IOException al cerrar: "
  41.                             + ex.getMessage());
  42.                 }
  43.             }
  44.         }
  45.         return className;
  46.     }
  47.    
  48.     protected abstract void showInstance();
  49.  
  50. }
  51.  
  52. class ASingleton extends Singleton {
  53.  
  54.     protected ASingleton() {
  55.         super();
  56.     }
  57.  
  58.     @Override
  59.     protected void showInstance() {
  60.         System.out.println("ASingleton");
  61.  
  62.     }
  63.  
  64. }
  65.  
  66. class BSingleton extends Singleton {
  67.  
  68.     protected BSingleton() {
  69.         super();
  70.     }
  71.  
  72.     @Override
  73.     protected void showInstance() {
  74.         System.out.println("BSingleton");
  75.  
  76.     }
  77.  
  78. }
  79.  
  80. class Client {
  81.  
  82.     private static void execute() {
  83.         Singleton.getInstance().showInstance();
  84.     }
  85.    
  86.     public static void main(String[] args) {
  87.         execute();
  88.     }
  89.  
  90. }
  91.  
Add Comment
Please, Sign In to add comment