mercredi 13 septembre 2017

JSONObject Using Singleton

In my project I am using JSON object to sent responce to client.
My JSONObject:

JSONObject jsonObj = new JSONObject();

Every time I am creating an json Object using new key word. I don't want to create using new key word. for this Can I implement Singleton pattern for this??

My Singletone Class code:

    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;
            }

        }
    }

to Create an instance I'll use:

JSONObject DBCon = SingletonInstance.getInstance();

Is this the correct way??

Aucun commentaire:

Enregistrer un commentaire