[Java Design Pattern] [Creational Pattern] - Singleton
Wednesday, June 12, 2019
Creational Pattern - Singleton
Purpose: The singleton ensures that a class only one instance and provides a global point of access to it.
CarHelper.java
public class CarHelper {
private static CarHelper _instance = null;
private int distance;
public static synchronized CarHelper getInstance() {
if (_instance == null) {
_instance = new CarHelper();
}
return _instance;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance += distance;
}
}
SingletoneMain.java
public class SingletonMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
CarHelper.getInstance().setDistance(10);
System.out.println(CarHelper.getInstance().getDistance());
CarHelper.getInstance().setDistance(20);
System.out.println(CarHelper.getInstance().getDistance());
CarHelper.getInstance().setDistance(30);
System.out.println(CarHelper.getInstance().getDistance());
}
}
Result:
10
30
60
We can see that each time get instance from CarHelper, it same an instance.
Purpose: The singleton ensures that a class only one instance and provides a global point of access to it.
This article show how to use Singletone design pattern.
CarHelper.java
public class CarHelper {
private static CarHelper _instance = null;
private int distance;
public static synchronized CarHelper getInstance() {
if (_instance == null) {
_instance = new CarHelper();
}
return _instance;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance += distance;
}
}
SingletoneMain.java
public class SingletonMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
CarHelper.getInstance().setDistance(10);
System.out.println(CarHelper.getInstance().getDistance());
CarHelper.getInstance().setDistance(20);
System.out.println(CarHelper.getInstance().getDistance());
CarHelper.getInstance().setDistance(30);
System.out.println(CarHelper.getInstance().getDistance());
}
}
Result:
10
30
60
We can see that each time get instance from CarHelper, it same an instance.
Example: we not allow init Statistics object by constructor.
public class Statistics implements Serializable {
// Definition for the class instance
private static volatile Statistics instance = new Statistics();
private List teams = new ArrayList();
/**
* Constructor has been made private so that outside classes do not have
* access to instantiate more instances of Statistics.
*/
private Statistics() {
}
/**
* Accessor for the statistics class. Only allows for one instance of the
* class to be created.
*
* @return
*/
public static Statistics getInstance() {
synchronized (Statistics.class) {
if (instance == null) {
instance = new Statistics();
}
}
return instance;
}
/**
* @return the teams
*/
public List getTeams() {
return teams;
}
/**
* @param teams
* the teams to set
*/
public void setTeams(List teams) {
this.teams = teams;
}
protected Object readResolve() {
return instance;
}
}
// Definition for the class instance
private static volatile Statistics instance = new Statistics();
private List teams = new ArrayList();
/**
* Constructor has been made private so that outside classes do not have
* access to instantiate more instances of Statistics.
*/
private Statistics() {
}
/**
* Accessor for the statistics class. Only allows for one instance of the
* class to be created.
*
* @return
*/
public static Statistics getInstance() {
synchronized (Statistics.class) {
if (instance == null) {
instance = new Statistics();
}
}
return instance;
}
/**
* @return the teams
*/
public List getTeams() {
return teams;
}
/**
* @param teams
* the teams to set
*/
public void setTeams(List teams) {
this.teams = teams;
}
protected Object readResolve() {
return instance;
}
}
public static void main(String[] args) {
Statistics a = Statistics.getInstance();
List<String> team = new ArrayList<String>();
team.add("Deposit");
team.add("Channel");
a.setTeams(team);
Statistics b = Statistics.getInstance();
System.out.println(b.getTeams());
}
Statistics a = Statistics.getInstance();
List<String> team = new ArrayList<String>();
team.add("Deposit");
team.add("Channel");
a.setTeams(team);
Statistics b = Statistics.getInstance();
System.out.println(b.getTeams());
}
Bài liên quan
Comments[ 0 ]
Post a Comment