Advertisement
jaVer404

level15.lesson12.home04_Singlet_lazy_init

Jul 18th, 2015
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package com.javarush.test.level15.lesson12.home04;
  2.  
  3. /* Закрепляем Singleton pattern
  4. 1. Найти в гугле пример для - Singleton pattern Lazy initialization.
  5. 2. По образу и подобию в отдельных файлах создать три синглтон класса Sun, Moon, Earth.
  6.  
  7. 3. Реализовать интерфейс Planet для классов Sun, Moon, Earth.
  8.  
  9. 4. В статическом блоке класса Solution вызвать метод readKeyFromConsoleAndInitPlanet.
  10.  
  11. 5. Реализовать функционал метода readKeyFromConsoleAndInitPlanet:
  12. 5.1. С консоли считать один параметр типа String.
  13. 5.2. Если параметр равен одной из констант интерфейса Planet,
  14. то создать соответствующий объект и присвоить его Planet thePlanet, иначе обнулить Planet thePlanet.
  15. 5.3. Сравнивать введенный параметр можно только с константами из Planet, нельзя создавать свои строки.
  16. */
  17.  
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21.  
  22. public class Solution {
  23.     public static Planet thePlanet;
  24.  
  25.     //add static block here - добавьте статический блок тут
  26.     static {
  27.         try
  28.         {
  29.             readKeyFromConsoleAndInitPlanet();
  30.         }
  31.         catch (IOException e) {}
  32.     }
  33.  
  34.     public static void readKeyFromConsoleAndInitPlanet() throws IOException{
  35.         // implement step #5 here - реализуйте задание №5 тут
  36.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  37.         String userInput="";
  38.         for (int i = 0; i<=1; i++) {
  39.             userInput = reader.readLine();
  40.             i++;
  41.         }
  42.         if (userInput.equals(Planet.SUN)) {
  43.             thePlanet = Sun.getInstance();
  44.         }
  45.         else if (userInput.equals(Planet.EARTH)){
  46.             thePlanet = Earth.getInstance();
  47.         }
  48.         else if (userInput.equals(Planet.MOON)) {
  49.             thePlanet = Moon.getInstance();
  50.         }
  51.         else {
  52.             thePlanet = null;
  53.         }
  54.     }
  55. }
  56. /*-------------------------------------------------------------------------------------------------*/
  57. package com.javarush.test.level15.lesson12.home04;
  58.  
  59. public interface Planet {
  60.     static String SUN = "sun";
  61.     static String MOON = "moon";
  62.     static String EARTH = "earth";
  63. }
  64. /*-------------------------------------------------------------------------------------------------*/
  65. package com.javarush.test.level15.lesson12.home04;
  66.  
  67. /**
  68.  * Created by Roma on 18.07.2015.
  69.  */
  70. public class Sun implements Planet
  71. {
  72.     private Sun() {
  73.     }
  74.     private static Sun instance;
  75.  
  76.     public static Sun getInstance() {
  77.         if (instance==null) {
  78.             instance = new Sun();
  79.         }
  80.         return instance;
  81.     }
  82. }
  83. /*-------------------------------------------------------------------------------------------------*/
  84. package com.javarush.test.level15.lesson12.home04;
  85.  
  86. /**
  87.  * Created by Roma on 18.07.2015.
  88.  */
  89. public class Moon implements Planet
  90. {
  91.     private Moon() {
  92.     }
  93.     private static Moon instance;
  94.  
  95.     public static Moon getInstance() {
  96.         if (instance==null) {
  97.             instance = new Moon();
  98.         }
  99.         return instance;
  100.     }
  101. }
  102. /*-------------------------------------------------------------------------------------------------*/
  103. package com.javarush.test.level15.lesson12.home04;
  104.  
  105. /**
  106.  * Created by Roma on 18.07.2015.
  107.  */
  108. public class Earth implements Planet
  109. {
  110.     private Earth() {
  111.     }
  112.     private static Earth instance;
  113.  
  114.     public static Earth getInstance() {
  115.         if (instance==null) {
  116.             instance = new Earth();
  117.         }
  118.         return instance;
  119.     }
  120. }
  121. /*-------------------------------------------------------------------------------------------------*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement