Consider the following hypothetical Singleton class DateSingleton
which currently looks like the following:
public class DateSingleton {
private static DateSingleton instance = new DateSingleton();
private String year;
private String month;
private String day;
private DateSingleton() {
}
public static DateSingleton getInstance(){
return instance;
}
}
This Singleton can be thought of as a date keeper, and only one of them will exist for my entire web application. I would like to be able to make the following type of builder-style call to assign my state:
DateSingleton.getInstance().year("2015").month("February").day("25");
Can someone suggest what code I would need to add to the DateSingleton
class in order to make this possible? Is it considered bad practice to make a Singleton a builder?
I realize that I could just add getters and setters to accomplish the same funtionality. But I want to move away from this, as there may be many properties in this Singleton and the builder pattern makes the code much easier to use and read.
Aucun commentaire:
Enregistrer un commentaire