Created
March 19, 2017 13:12
-
-
Save fermopili/626df82ab2845f6f8284fcd8e651ecd8 to your computer and use it in GitHub Desktop.
com.javarush.task.task15.task1522
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Earth implements Planet | |
{ | |
private static Earth instance; | |
private Earth() | |
{ | |
} | |
public static Earth getInstance() | |
{ | |
if (instance == null) | |
instance = new Earth(); | |
return instance; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Moon implements Planet | |
{ | |
private static Moon instance; | |
private Moon() | |
{ | |
} | |
public static Moon getInstance() | |
{ | |
if (instance == null) | |
instance = new Moon(); | |
return instance; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Planet { | |
static String SUN = "sun"; | |
static String MOON = "moon"; | |
static String EARTH = "earth"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Закрепляем Singleton pattern | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
} | |
public static Planet thePlanet; | |
//add static block here - добавьте статический блок тут | |
static | |
{ | |
// Solution. | |
try | |
{ | |
readKeyFromConsoleAndInitPlanet(); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
public static void readKeyFromConsoleAndInitPlanet() throws IOException | |
{ | |
// implement step #5 here - реализуйте задание №5 тут | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
String key = reader.readLine(); | |
if (Planet.EARTH.contains(key)) | |
thePlanet=Earth.getInstance(); | |
else if (Planet.MOON.contains(key)) | |
thePlanet=Moon.getInstance(); | |
else if (Planet.SUN.contains(key)) | |
thePlanet=Sun.getInstance(); | |
else thePlanet=null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Sun implements Planet | |
{ | |
private static Sun instance; | |
private Sun() | |
{ | |
} | |
public static Sun getInstance() | |
{ | |
if (instance == null) | |
instance = new Sun(); | |
return instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment