Skip to main content
Rollback to Revision 2
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

    // A static instance of an Earth object
    private static Earth earth;

    // Some earthly variables
    private long ageInYears = 4500000000L;
    private float daysForFullOrbit = 365.26f;
    private float degreesOfAxisTilt = 23.4f;
    private long population = 7046000000L;

    // Prevent client from instantiating new Earth objects
    private Earth() {
    }

    /**
     * Global access point (no pun intended :)
     * 
     * Synchronized so its thread safe.
     */
    public static synchronized Earth getInstance() {

        // "Lazy load" an Earth Object
        if (earth == null) {
            earth = new Earth();
        }

        return earth;
    }

    // EDIT: SynchronizedBasic getters and setters
    public synchronized double getAgeInYears() {
        return ageInYears;
    }

    public synchronized void setAgeInYears(long ageInYears) {
        this.ageInYears = ageInYears;
    }

    public synchronized float getdaysForFullOrbit() {
        return daysForFullOrbit;
    }

    public synchronized void setdaysForFullOrbit(float daysForFullRotation) {
        this.daysForFullOrbit = daysForFullRotation;
    }

    public synchronized float getDegreesOfAxisTilt() {
        return degreesOfAxisTilt;
    }

    public synchronized void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
        this.degreesOfAxisTilt = degreesOfAxisTilt;
    }

    public synchronized double getPopulation() {
        return population;
    }

    public synchronized void setPopulation(int population) {
        this.population = population;
    }
 
    //EDIT: Bad code, Make sure you don't create setter for your Singleton object refrence!
    //public static void setEarth(Earth earth) {
    //    Earth.earth = earth;
    // }
}
package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

    // A static instance of an Earth object
    private static Earth earth;

    // Some earthly variables
    private long ageInYears = 4500000000L;
    private float daysForFullOrbit = 365.26f;
    private float degreesOfAxisTilt = 23.4f;
    private long population = 7046000000L;

    // Prevent client from instantiating new Earth objects
    private Earth() {
    }

    /**
     * Global access point (no pun intended :)
     * 
     * Synchronized so its thread safe.
     */
    public static synchronized Earth getInstance() {

        // "Lazy load" an Earth Object
        if (earth == null) {
            earth = new Earth();
        }

        return earth;
    }

    // EDIT: Synchronized getters and setters
    public synchronized double getAgeInYears() {
        return ageInYears;
    }

    public synchronized void setAgeInYears(long ageInYears) {
        this.ageInYears = ageInYears;
    }

    public synchronized float getdaysForFullOrbit() {
        return daysForFullOrbit;
    }

    public synchronized void setdaysForFullOrbit(float daysForFullRotation) {
        this.daysForFullOrbit = daysForFullRotation;
    }

    public synchronized float getDegreesOfAxisTilt() {
        return degreesOfAxisTilt;
    }

    public synchronized void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
        this.degreesOfAxisTilt = degreesOfAxisTilt;
    }

    public synchronized double getPopulation() {
        return population;
    }

    public synchronized void setPopulation(int population) {
        this.population = population;
    }
 
    //EDIT: Bad code, Make sure you don't create setter for your Singleton object refrence!
    //public static void setEarth(Earth earth) {
    //   Earth.earth = earth;
    // }
}
package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

    // A static instance of an Earth object
    private static Earth earth;

    // Some earthly variables
    private long ageInYears = 4500000000L;
    private float daysForFullOrbit = 365.26f;
    private float degreesOfAxisTilt = 23.4f;
    private long population = 7046000000L;

    // Prevent client from instantiating new Earth objects
    private Earth() {
    }

    /**
     * Global access point (no pun intended :)
     * 
     * Synchronized so its thread safe.
     */
    public static synchronized Earth getInstance() {

        // "Lazy load" an Earth Object
        if (earth == null) {
            earth = new Earth();
        }

        return earth;
    }

    // Basic getters and setters
    public double getAgeInYears() {
        return ageInYears;
    }

    public void setAgeInYears(long ageInYears) {
        this.ageInYears = ageInYears;
    }

    public float getdaysForFullOrbit() {
        return daysForFullOrbit;
    }

    public void setdaysForFullOrbit(float daysForFullRotation) {
        this.daysForFullOrbit = daysForFullRotation;
    }

    public float getDegreesOfAxisTilt() {
        return degreesOfAxisTilt;
    }

    public void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
        this.degreesOfAxisTilt = degreesOfAxisTilt;
    }

    public double getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public static void setEarth(Earth earth) {
        Earth.earth = earth;
    }
}
added 220 characters in body
Source Link
Shijima
  • 407
  • 4
  • 10
package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

    // A static instance of an Earth object
    private static Earth earth;

    // Some earthly variables
    private long ageInYears = 4500000000L;
    private float daysForFullOrbit = 365.26f;
    private float degreesOfAxisTilt = 23.4f;
    private long population = 7046000000L;

    // Prevent client from instantiating new Earth objects
    private Earth() {
    }

    /**
     * Global access point (no pun intended :)
     * 
     * Synchronized so its thread safe.
     */
    public static synchronized Earth getInstance() {

        // "Lazy load" an Earth Object
        if (earth == null) {
            earth = new Earth();
        }

        return earth;
    }

    // BasicEDIT: Synchronized getters and setters
    public synchronized double getAgeInYears() {
        return ageInYears;
    }

    public synchronized void setAgeInYears(long ageInYears) {
        this.ageInYears = ageInYears;
    }

    public synchronized float getdaysForFullOrbit() {
        return daysForFullOrbit;
    }

    public synchronized void setdaysForFullOrbit(float daysForFullRotation) {
        this.daysForFullOrbit = daysForFullRotation;
    }

    public synchronized float getDegreesOfAxisTilt() {
        return degreesOfAxisTilt;
    }

    public synchronized void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
        this.degreesOfAxisTilt = degreesOfAxisTilt;
    }

    public synchronized double getPopulation() {
        return population;
    }

    public synchronized void setPopulation(int population) {
        this.population = population;
    } 

    //EDIT: Bad code, Make sure you don't create setter for your Singleton object refrence!
    //public static void setEarth(Earth earth) {
     //   Earth.earth = earth;
    // }
}
package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

    // A static instance of an Earth object
    private static Earth earth;

    // Some earthly variables
    private long ageInYears = 4500000000L;
    private float daysForFullOrbit = 365.26f;
    private float degreesOfAxisTilt = 23.4f;
    private long population = 7046000000L;

    // Prevent client from instantiating new Earth objects
    private Earth() {
    }

    /**
     * Global access point (no pun intended :)
     * 
     * Synchronized so its thread safe.
     */
    public static synchronized Earth getInstance() {

        // "Lazy load" an Earth Object
        if (earth == null) {
            earth = new Earth();
        }

        return earth;
    }

    // Basic getters and setters
    public double getAgeInYears() {
        return ageInYears;
    }

    public void setAgeInYears(long ageInYears) {
        this.ageInYears = ageInYears;
    }

    public float getdaysForFullOrbit() {
        return daysForFullOrbit;
    }

    public void setdaysForFullOrbit(float daysForFullRotation) {
        this.daysForFullOrbit = daysForFullRotation;
    }

    public float getDegreesOfAxisTilt() {
        return degreesOfAxisTilt;
    }

    public void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
        this.degreesOfAxisTilt = degreesOfAxisTilt;
    }

    public double getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public static void setEarth(Earth earth) {
        Earth.earth = earth;
    }
}
package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

    // A static instance of an Earth object
    private static Earth earth;

    // Some earthly variables
    private long ageInYears = 4500000000L;
    private float daysForFullOrbit = 365.26f;
    private float degreesOfAxisTilt = 23.4f;
    private long population = 7046000000L;

    // Prevent client from instantiating new Earth objects
    private Earth() {
    }

    /**
     * Global access point (no pun intended :)
     * 
     * Synchronized so its thread safe.
     */
    public static synchronized Earth getInstance() {

        // "Lazy load" an Earth Object
        if (earth == null) {
            earth = new Earth();
        }

        return earth;
    }

    // EDIT: Synchronized getters and setters
    public synchronized double getAgeInYears() {
        return ageInYears;
    }

    public synchronized void setAgeInYears(long ageInYears) {
        this.ageInYears = ageInYears;
    }

    public synchronized float getdaysForFullOrbit() {
        return daysForFullOrbit;
    }

    public synchronized void setdaysForFullOrbit(float daysForFullRotation) {
        this.daysForFullOrbit = daysForFullRotation;
    }

    public synchronized float getDegreesOfAxisTilt() {
        return degreesOfAxisTilt;
    }

    public synchronized void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
        this.degreesOfAxisTilt = degreesOfAxisTilt;
    }

    public synchronized double getPopulation() {
        return population;
    }

    public synchronized void setPopulation(int population) {
        this.population = population;
    } 

    //EDIT: Bad code, Make sure you don't create setter for your Singleton object refrence!
    //public static void setEarth(Earth earth) {
    //   Earth.earth = earth;
    // }
}
added 472 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Review of a Java Simple Java Singleton

Below is a Singleton that I designed as a piece of sample code for a website I'm working on for fun. The goal is to use the Singleton to hold an EarthEarth object since Earth is unique and there is only 1 of them. Please let me know your thoughts on the implementation.

package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

    // A static instance of an Earth object
    private static Earth earth;

    // Some earthly variables
    private long ageInYears = 4500000000L;
    private float daysForFullOrbit = 365.26f;
    private float degreesOfAxisTilt = 23.4f;
    private long population = 7046000000L;

    // Prevent client from instantiating new Earth objects
    private Earth() {
    }

    /**
     * Global access point (no pun intended :)
     * 
     * Synchronized so its thread safe.
     */
    public static synchronized Earth getInstance() {

        // "Lazy load" an Earth Object
        if (earth == null) {
            earth = new Earth();
        }

        return earth;
 
    }

    // Basic getters and setters
    public double getAgeInYears() {
        return ageInYears;
    }

    public void setAgeInYears(long ageInYears) {
        this.ageInYears = ageInYears;
    }

    public float getdaysForFullOrbit() {
        return daysForFullOrbit;
    }

    public void setdaysForFullOrbit(float daysForFullRotation) {
        this.daysForFullOrbit = daysForFullRotation;
    }

    public float getDegreesOfAxisTilt() {
        return degreesOfAxisTilt;
    }

    public void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
        this.degreesOfAxisTilt = degreesOfAxisTilt;
    }

    public double getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public static void setEarth(Earth earth) {
        Earth.earth = earth;
    }
 
}
package design.patterns;

public class Client {

    public static void main(String args[]){ 

    
     Earth earth = Earth.getInstance();

        //Invoke Earth's methods from the instance created on line 7
        System.out.printf("Earth is over %1$,.0f Days old. \n", earth.getAgeInYears()); 

    
     //Invoke Earth's methods from the Singleton directly
        System.out.println("Earth's orbit takes " + Earth.getInstance().getdaysForFullOrbit() + " days to complete.");

        //Invoke Earth's methods both ways
        System.out.println("Earth sits on a " + earth.getDegreesOfAxisTilt() + " degree tilt.");
        System.out.printf("As of 1/20/2014, there are %1$,.0f people on Earth!", + Earth.getInstance().getPopulation());
    
 }
 
}

Review of a Java Simple Singleton

Below is a Singleton that I designed as a piece of sample code for a website I'm working on for fun. The goal is to use the Singleton to hold an Earth object since Earth is unique and there is only 1 of them. Please let me know your thoughts on the implementation.

package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

// A static instance of an Earth object
private static Earth earth;

// Some earthly variables
private long ageInYears = 4500000000L;
private float daysForFullOrbit = 365.26f;
private float degreesOfAxisTilt = 23.4f;
private long population = 7046000000L;

// Prevent client from instantiating new Earth objects
private Earth() {
}

/**
 * Global access point (no pun intended :)
 * 
 * Synchronized so its thread safe.
 */
public static synchronized Earth getInstance() {

    // "Lazy load" an Earth Object
    if (earth == null) {
        earth = new Earth();
    }

    return earth;
 
}

// Basic getters and setters
public double getAgeInYears() {
    return ageInYears;
}

public void setAgeInYears(long ageInYears) {
    this.ageInYears = ageInYears;
}

public float getdaysForFullOrbit() {
    return daysForFullOrbit;
}

public void setdaysForFullOrbit(float daysForFullRotation) {
    this.daysForFullOrbit = daysForFullRotation;
}

public float getDegreesOfAxisTilt() {
    return degreesOfAxisTilt;
}

public void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
    this.degreesOfAxisTilt = degreesOfAxisTilt;
}

public double getPopulation() {
    return population;
}

public void setPopulation(int population) {
    this.population = population;
}

public static void setEarth(Earth earth) {
    Earth.earth = earth;
}
 
}
package design.patterns;

public class Client {

public static void main(String args[]){
    
     Earth earth = Earth.getInstance();

    //Invoke Earth's methods from the instance created on line 7
    System.out.printf("Earth is over %1$,.0f Days old. \n", earth.getAgeInYears());
    
     //Invoke Earth's methods from the Singleton directly
    System.out.println("Earth's orbit takes " + Earth.getInstance().getdaysForFullOrbit() + " days to complete.");

    //Invoke Earth's methods both ways
    System.out.println("Earth sits on a " + earth.getDegreesOfAxisTilt() + " degree tilt.");
    System.out.printf("As of 1/20/2014, there are %1$,.0f people on Earth!", + Earth.getInstance().getPopulation());
    
 }
 
}

Simple Java Singleton

Below is a Singleton that I designed as a piece of sample code for a website I'm working on for fun. The goal is to use the Singleton to hold an Earth object since Earth is unique and there is only 1 of them. Please let me know your thoughts on the implementation.

package design.patterns;

 /**
  * This class is a Singleton, we can only have 1 instance of it no matter what!
  * 
  * We mark the class final so that no other classes try to extend it. Some
  * designs would want the Singleton to be extendable but we do not!
  */
public final class Earth {

    // A static instance of an Earth object
    private static Earth earth;

    // Some earthly variables
    private long ageInYears = 4500000000L;
    private float daysForFullOrbit = 365.26f;
    private float degreesOfAxisTilt = 23.4f;
    private long population = 7046000000L;

    // Prevent client from instantiating new Earth objects
    private Earth() {
    }

    /**
     * Global access point (no pun intended :)
     * 
     * Synchronized so its thread safe.
     */
    public static synchronized Earth getInstance() {

        // "Lazy load" an Earth Object
        if (earth == null) {
            earth = new Earth();
        }

        return earth;
    }

    // Basic getters and setters
    public double getAgeInYears() {
        return ageInYears;
    }

    public void setAgeInYears(long ageInYears) {
        this.ageInYears = ageInYears;
    }

    public float getdaysForFullOrbit() {
        return daysForFullOrbit;
    }

    public void setdaysForFullOrbit(float daysForFullRotation) {
        this.daysForFullOrbit = daysForFullRotation;
    }

    public float getDegreesOfAxisTilt() {
        return degreesOfAxisTilt;
    }

    public void setDegreesOfAxisTilt(float degreesOfAxisTilt) {
        this.degreesOfAxisTilt = degreesOfAxisTilt;
    }

    public double getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public static void setEarth(Earth earth) {
        Earth.earth = earth;
    }
}
package design.patterns;

public class Client {

    public static void main(String args[]){ 

        Earth earth = Earth.getInstance();

        //Invoke Earth's methods from the instance created on line 7
        System.out.printf("Earth is over %1$,.0f Days old. \n", earth.getAgeInYears()); 

        //Invoke Earth's methods from the Singleton directly
        System.out.println("Earth's orbit takes " + Earth.getInstance().getdaysForFullOrbit() + " days to complete.");

        //Invoke Earth's methods both ways
        System.out.println("Earth sits on a " + earth.getDegreesOfAxisTilt() + " degree tilt.");
        System.out.printf("As of 1/20/2014, there are %1$,.0f people on Earth!", + Earth.getInstance().getPopulation());
    }
}
Source Link
Shijima
  • 407
  • 4
  • 10
Loading