使用辛格尔顿共享可变 [英] Using Singleton to Share a Variable

查看:187
本文介绍了使用辛格尔顿共享可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有一个很难理解如何使用一个单共用一个变量。我试图做一个黑莓应用程序有哪些需要共享一个共同的变量,iconCount两个入口点。我已被告知有人在论坛上用单与RunTimeStore API。最终周围的Googling导致:

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:

<一个href=\"http://docs.blackberry.com/en/developers/deliverables/17952/CS_creating_a_singleton_by_using_rutnime_store_1554335_11.jsp\" rel=\"nofollow\">http://docs.blackberry.com/en/developers/deliverables/17952/CS_creating_a_singleton_by_using_rutnime_store_1554335_11.jsp

我一直深藏在谷歌几页,但我还是不能老是明白这样做,以及如何实现它。我现在的理解是,一个单将创建一个全局变量不知何故通过code:

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

和另外一个问题是如何将我创建这个单变量?我需要在一开始就声明变量iconCount = 0,然后才能使用它。将宣布它是这样

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(); 

?这是很新的给我,因为我刚开始的Java,所以如果任何人能解释这牢记你与一个新手交流我将非常感激。在此先感谢!

? 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!

推荐答案

他们的意思是,请确保用户初始化MySingleton类只是一次性的,所以你不会有问题的多个实例,在同一时间初始化两秒。我从下面的一样多实例意味着什么:

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();

由于双方initilaization可以有diffetent计数。你需要的东西是这样的:

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.

这篇关于使用辛格尔顿共享可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆