Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level15.lesson12.home04;
- /* Закрепляем Singleton pattern
- 1. Найти в гугле пример для - Singleton pattern Lazy initialization.
- 2. По образу и подобию в отдельных файлах создать три синглтон класса Sun, Moon, Earth.
- 3. Реализовать интерфейс Planet для классов Sun, Moon, Earth.
- 4. В статическом блоке класса Solution вызвать метод readKeyFromConsoleAndInitPlanet.
- 5. Реализовать функционал метода readKeyFromConsoleAndInitPlanet:
- 5.1. С консоли считать один параметр типа String.
- 5.2. Если параметр равен одной из констант интерфейса Planet,
- то создать соответствующий объект и присвоить его Planet thePlanet, иначе обнулить Planet thePlanet.
- 5.3. Сравнивать введенный параметр можно только с константами из Planet, нельзя создавать свои строки.
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Solution {
- public static Planet thePlanet;
- //add static block here - добавьте статический блок тут
- static {
- try
- {
- readKeyFromConsoleAndInitPlanet();
- }
- catch (IOException e) {}
- }
- public static void readKeyFromConsoleAndInitPlanet() throws IOException{
- // implement step #5 here - реализуйте задание №5 тут
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String userInput="";
- for (int i = 0; i<=1; i++) {
- userInput = reader.readLine();
- i++;
- }
- if (userInput.equals(Planet.SUN)) {
- thePlanet = Sun.getInstance();
- }
- else if (userInput.equals(Planet.EARTH)){
- thePlanet = Earth.getInstance();
- }
- else if (userInput.equals(Planet.MOON)) {
- thePlanet = Moon.getInstance();
- }
- else {
- thePlanet = null;
- }
- }
- }
- /*-------------------------------------------------------------------------------------------------*/
- package com.javarush.test.level15.lesson12.home04;
- public interface Planet {
- static String SUN = "sun";
- static String MOON = "moon";
- static String EARTH = "earth";
- }
- /*-------------------------------------------------------------------------------------------------*/
- package com.javarush.test.level15.lesson12.home04;
- /**
- * Created by Roma on 18.07.2015.
- */
- public class Sun implements Planet
- {
- private Sun() {
- }
- private static Sun instance;
- public static Sun getInstance() {
- if (instance==null) {
- instance = new Sun();
- }
- return instance;
- }
- }
- /*-------------------------------------------------------------------------------------------------*/
- package com.javarush.test.level15.lesson12.home04;
- /**
- * Created by Roma on 18.07.2015.
- */
- public class Moon implements Planet
- {
- private Moon() {
- }
- private static Moon instance;
- public static Moon getInstance() {
- if (instance==null) {
- instance = new Moon();
- }
- return instance;
- }
- }
- /*-------------------------------------------------------------------------------------------------*/
- package com.javarush.test.level15.lesson12.home04;
- /**
- * Created by Roma on 18.07.2015.
- */
- public class Earth implements Planet
- {
- private Earth() {
- }
- private static Earth instance;
- public static Earth getInstance() {
- if (instance==null) {
- instance = new Earth();
- }
- return instance;
- }
- }
- /*-------------------------------------------------------------------------------------------------*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement