Saturday, November 28, 2015

Android Singleton Pattern class

Singleton Pattern class



Singleton pattern class is a static class to one object, this class is very useful when one object is needed for the whole application that we created. It’s quite a like session state in web programming, we can get object entity in every class without re declaration of the object. Just like it’s name Singleton this class is a single class with a ton entities , like a one Class Army this class can handle many entities so we don’t have to retrieve a value for every state in application from database or configuration file that required for our application.

Okay ladies, let see what is the Singleton pattern class is look like.:

In these example I use singleton pattern for my android application, 

package com.subhan.core;

import com.subhan.model.userLogin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;

/**
 * Created by Subhan on 10/19/2015.
 */
public class Singleton {
    private static Singleton  mSingleton null;
    private String mString;
    private JSONArray jsonArray;
    private JSONObject jsonObject;
    private userLogin userLogin;
    private List<userLogin> userLoginList;
    private String urlws"http://172.16.110.35:80/Login.svc";

    public List<com.subhan.model.userLogin> getUserLoginList() {
        return userLoginList;
    }

    public void setUserLoginList(JSONArray jsonArray) {
        if(jsonArray.length()>0){
            userLogin usr = new userLogin();
            for (int i=0;i<jsonArray.length();i++) {
                try {
                    usr.setValue(jsonArray.getJSONObject(i));
                catch (JSONException e) {
                    e.printStackTrace();
                }
                this.userLoginList.add(usr);
            }
        }
        this.userLoginList userLoginList;
    }


    public String getUrlws() {
        return urlws;
    }

    public void setUrlws(String urlws) {
        this.urlws = urlws;
    }



    public static Singleton getInstance(){
        if(mSingleton== null)
        {
            mSingletonnew Singleton();
        }
        return mSingleton;
    }
    public com.subhan.model.userLogin getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(com.subhan.model.userLogin userLogin) {
        this.userLogin = userLogin;
    }



    public JSONArray getJsonArray() {
        return jsonArray;
    }

    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
    }

    public JSONObject getJsonObject() {
        return jsonObject;
    }

    public void setJsonObject(JSONObject jsonObject) {
        this.jsonObject = jsonObject;
    }

    private Singleton(){
        mString "Hello";
    }



    public String getString(){
        return this.mString;
    }

    public void setString(String value){
        mString = value;
    }
}
  
At the start the singleton declaration is declared it self :

private static Singleton mSingletonnull;

And create static procedure for initiation of the singleton for every time we will use these Singleton class:



public static Singleton getInstance(){
        if(mSingleton== null)
        {
            mSingletonnew Singleton();
        }
        return mSingleton;
    }

At first the getInstance() have to check are the instance of singleton is available or not by

        if(mSingleton== null)
        {
            mSingletonnew Singleton();
        }

And if not the getInstance() will create a new singleton.
Then return the singleton instance for our application to be use.

This singleton class is can provide a lot of entities , remember the “TON” word even a object.

In these example we can see :

    private String mString;
    private JSONArray jsonArray;
    private JSONObject jsonObject;
    private userLogin userLogin;
    private List<userLogin> userLoginList;
    private String urlws"http://172.16.110.35:80/Login.svc";
And we just had to create a getter and setter for those entities to be use.


How to use Singleton Pattern


We can access this singleton by call the static function getInstance(), for example :

private static String url = Singleton.getInstance().getUrlws()+"/Section/120p";

See , I call   Singleton.getInstance().getUrlws()+"/Section/120p"; 

I use it to retrieve url value that I will use for my application

Okay my dear friend and foe or cyborg from the future that will kill John Connor Mother.. Thank’s for read these blog.. If there’s some thing wrong please let me know. ^__^ V






No comments:

Post a Comment