Atul gupta
Atul gupta

Reputation: 75

Using java singleton class in Android for common tasks

In my android app there are certain common tasks for example fetching the IP of server to make API calls - For this I have created a singleton class

public class MiscFunctions extends Activity {

private static MiscFunctions ourInstance = new MiscFunctions();

public static MiscFunctions getInstance() {
    return ourInstance;
}

private MiscFunctions() {
}

/*
method to return the ip of main server.
The ip is written in plain text in file server_url.txt in assets folder
*/

public  String getServerIP(Context c)  {
    try {
        AssetManager am = c.getAssets();
        InputStream is = am.open("server_url.txt");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        return new String(buffer);
    }
    catch (IOException e)   {
        System.out.println("IO Error while accesssing server_url.txt");
        e.printStackTrace();
        return "Failed to open server_url.txt";
    }
    catch (NullPointerException e)  {
        System.out.println("NullPointer exception error while accessrng server_url.txt");
        e.printStackTrace();
        return "Failed to open server_url.txt";
    }
    catch (Exception e)  {
        System.out.println("Unknown exception error while accessrng server_url.txt");
        e.printStackTrace();
        return "Failed to open server_url.txt";
    }
}

}

Then I will be using this class in various activities like this:

    Context c = this.getApplicationContext();
    server_ip = MiscFunctions.getInstance().getServerIP(c);

I am just wondering whether this is the correct approach? Or I should be using a regular java class and instantiate that class to call methods in activities.

Which approach would be more efficient and robust?

Any expert comments would be highly appreciated. Thanks in advance

Upvotes: 2

Views: 638

Answers (3)

Sourabh Bans
Sourabh Bans

Reputation: 3134

Singleton is a pattern you can't test whether it is really a singleton or not. As I see your singleton is not thread safe also. You can refer here.

If you want your IP address to be secure then don't put it in your assets directory. Anyone can see it by changing your .apk to .zip. I think you should make a class and make a static final String, like:

public class Constants {
    public static final String MY_IP = "your file content";
}

what I think you are just reading a text file, hardly one line of text.

And access the IP like.

String ip = Constants.MY_IP;

Upvotes: 1

Nguyen Quang Anh
Nguyen Quang Anh

Reputation: 315

I'm only used singleton in case that it needs to maintain some global states. For example a DAO that can write and get data from database need only one database connection object, although in this case I can call Database.getConnection() everytime I need, but by using a single object in the class that save the connection, it will speed up a little bit, and easier to debug too. Otherwise, static method is good.

Upvotes: 0

rainash
rainash

Reputation: 874

I suggest you to use static method like:

public static String getServerIp(Context ct) {
  ....
}

here is another question close to you Why use a singleton instead of static methods?

Upvotes: 0

Related Questions