dimanche 29 mai 2016

Can we make a Singleton class by making private getInstance method and public getter setter methods?

Doesn't Singleton class just means enforcing of presence of a single instance of a class always? If that is the case, unlike the norm, can't we have getter-setter methods in it with private getInstance method? In short, is below class an example of Singleton class? Why / Why not:-

public class MySingletonClass {
private MySingletonClass mySingletonClass;
private String name;
private MySingletonClass(){

}
private MySingletonClass getSingletonClassReference(){
    if(mySingletonClass==null){
        mySingletonClass = new MySingletonClass();
    }
    return mySingletonClass;
}

public static String getName() {
    return getSingletonClassReference().name;
}

public static setName(String name) {
    getSingletonClassReference().name = name;
}}

Aucun commentaire:

Enregistrer un commentaire