私のプロジェクトでは、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