在我的項目,我使用JSON對象發送性反應的客戶。
我JSONObject
:的JSONObject使用辛格爾頓
JSONObject jsonObj = new JSONObject();
每次我創建使用new
關鍵字的JSON對象。 我不想使用new
關鍵字創建。 爲了這個,我可以實現Singleton pattern
這個?
我Singletone
類代碼:
public class SingletonInstance {
private static SingletonInstance instance = new SingletonInstance();
private SingletonUmtInstance() {
}
// Get the only object available
public static JSONObject getInstance() {
if (instance == null) {
instance = new JSONObject();
return instance;
} else {
return instance;
}
}
}
創建一個實例,我將使用:
JSONObject DBCon = SingletonInstance.getInstance();
這是正確的做法?
我不認爲這是這個問題的適當位置 – Mritunjay
我只是想知道我是否使用正確的方法.. – ansh