Skip to content

Instantly share code, notes, and snippets.

@fermopili
Created March 19, 2017 13:12
Show Gist options
  • Save fermopili/626df82ab2845f6f8284fcd8e651ecd8 to your computer and use it in GitHub Desktop.
Save fermopili/626df82ab2845f6f8284fcd8e651ecd8 to your computer and use it in GitHub Desktop.
com.javarush.task.task15.task1522
public class Earth implements Planet
{
private static Earth instance;
private Earth()
{
}
public static Earth getInstance()
{
if (instance == null)
instance = new Earth();
return instance;
}
}
public class Moon implements Planet
{
private static Moon instance;
private Moon()
{
}
public static Moon getInstance()
{
if (instance == null)
instance = new Moon();
return instance;
}
}
public interface Planet {
static String SUN = "sun";
static String MOON = "moon";
static String EARTH = "earth";
}
/*
Закрепляем 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;
}
}
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