Using Singleton to Share a Variable

2019-06-10 00:29发布

I have been having a hard time understanding how to use a singleton to share a common variable. I am trying to make a blackberry app which has two entry points which need to share a common variable, iconCount. I have been advised to use a singleton with the RunTimeStore API by someone on a forum. Googling around eventually leads to:

http://docs.blackberry.com/en/developers/deliverables/17952/CS_creating_a_singleton_by_using_rutnime_store_1554335_11.jsp

I have been a few pages deep in Google but I still can`t understand what this does and how to implement it. My current understanding is that a singleton will create a "global variable" somehow through the code:

class MySingleton {
   private static MySingleton _instance;
   private static final long GUID = 0xab4dd61c5d004c18L;

   // constructor
   MySingleton() {}

   public static MySingleton getInstance() {
      if (_instance == null) {
         _instance = (MySingleton)RuntimeStore.getRuntimeStore().get(GUID);
      if (_instance == null) {
         MySingleton singleton = new MySingleton();

         RuntimeStore.getRuntimeStore().put(GUID, singleton);
         _instance = singleton;
         }
      }
      return _instance;
   }
} 

And another question would be how would I create a variable from this singleton? I need to declare variable iconCount = 0 at the beginning and then be able to use it. Would declaring it be something like

Integer iconCount = (Integer) RuntimeStore.getInstance(); 

? This is very new to me as I have just started Java so if anyone could explain this keeping in mind you're communicating with a novice I would be very grateful. Thanks in advance!

3条回答
做自己的国王
2楼-- · 2019-06-10 01:11

You can't cast your MySingleton class to Integer.

And in your example you don't use your singleton but RuntimeStore !

You can use an integer field of your class Singleton, initalized to 0 in the constructor of your singleton (private constructor) and get it by doing :

MySingleton.getInstance().getIntegerField()

here is a detailled description of the singleton pattern : http://en.wikipedia.org/wiki/Singleton_pattern

I think you misunderstand the use of the singleton. the singleton is not injected in your RuntimeStore, it is a classic java object. The only subtile think to know about a singleton is that its constructor is private and the class MySingleton can have only one instance which is always returned when your singleton.getInstance() is called

查看更多
我想做一个坏孩纸
3楼-- · 2019-06-10 01:16

You would call

MySingleton.getInstance()

to get the instance in your app. The point is that getInstance is controlling access to the underlying object.

Also, you should make your constructor private, so it's only accessible in that file.

To define a property on you singleton class, just declare a non-static property. Each instance of the class will have its own copy, but you are controlling the creation of the objects, so their should only ever be 1 (per JVM). So

class MySingleton {
   private static MySingleton _instance;
   private static final long GUID = 0xab4dd61c5d004c18L;

   private Integer iconCount; // non-static method, add a public getIconCount below
  ...
}

and then you can access it via

MySingleton.getInstance().getIconCount();

查看更多
我只想做你的唯一
4楼-- · 2019-06-10 01:17

They mean please make sure that user initializing MySingleton class just onetime so you will not have problem with multiple instances and initialize two count in the same time. I mean from multiple instance something like below:

Mysingleton single = new Mysingleton();
Mysingleton single2 = new Mysingleton();

Because both initilaization can have diffetent count. You need something like this:

public class IconManager {
    private static iconManager _instance;
    private static final long GUID = 0xab4dd61c5d004c18L;
    private static int count = 0;

    // constructor
    IconManager() {
    }

    public static IconManager getInstance() {
        if (_instance == null) {
            _instance = (IconManager) RuntimeStore.getRuntimeStore().get(GUID);
            if (_instance == null) {
                IconManager singleton = new IconManager();

                RuntimeStore.getRuntimeStore().put(GUID, singleton);
                _instance = singleton;
            }
        }
        return _instance;
    }

    public static int getCount() {
        return count;
    }

    public static void setCount(int count) {
        this.count = count;
    }

}

and after you can create an instance for the class:

public static void main(String[] args){

    IconManager iconManager = IconManager.getInstance();
    iconManager.setCount(iconmanager.getCount() + 1);

}

So application will do first validation, if there is already an instance it will update existing one, if not than it will create new one.

查看更多
登录 后发表回答