简体   繁体   English

Android:使用全局对象而不是创建新对象

[英]Android: use a global object instead of creating new ones

I have a class A in my Android app where I have some methods. 我的Android应用程序中有A类,这里有一些方法。 Those methods are public and used in other classes (B, C, D, E, F ...). 这些方法是公共的,并在其他类(B,C,D,E,F ...)中使用。

Is there a possibility to create only once the object from the class A and then use it in the other classes or i have to create new object in each classe. 是否有可能只创建一次来自A类的对象,然后在其他类中使用它,否则我必须在每个类中创建一个新对象。

Actually I have to do in each classe (B, C, D, E, F ...) 实际上,我必须在每个班级(B,C,D,E,F ...)中做

A a = new A();
a.xxxx;

It will be great if I can create only once the object a and then call it in my other classes. 如果我只能创建一次对象a,然后在其他类中调用它,那就太好了。

thank you. 谢谢。

Use a singleton pattern. 使用单例模式。 It allows you to use the same instance across classes: 它允许您在类之间使用相同的实例:

http://www.javabeginner.com/learn-java/java-singleton-design-pattern http://www.javabeginner.com/learn-java/java-singleton-design-pattern

class A{
    static A a;
    static{
          a = new A();
    }
}

In every other class use 在其他所有类中使用

A.a to get the object and call respective methods as
A.a.xxxx()

Use static methods's analogy to do this. 使用静态方法的类比来做到这一点。

For example: 例如:

public class Helper{
    public static void doSomething(){
        //do something here
    }
}

Now in your other classes, use the above method as below: 现在在您的其他类中,使用以下方法:

Helper.doSomething();

Or Singleton pattern would be an alternate too. 或者Singleton模式也将是备用模式。

Instead of that why don't you make those methods static or consider single instance pattern if there are no states involved.. 相反,为什么不将这些方法设为静态,或者如果不涉及任何状态,就考虑使用单实例模式。

how to use static methods and singleton pattern 如何使用静态方法单例模式

I see the following 3 possibilities: 我看到以下3种可能性:

1. If these are just "normal" helper methods you may also just do 1.如果这些只是“正常”的辅助方法,您也可以这样做

class B extends A B级延伸到A

and inherit the methods of A into B,C,D,E,.... 并将A的方法继承为B,C,D,E ...

2. However if you need internal memory of class A which is global to all other classes or their instances of B,C,D then may use the static pattern like 2.但是,如果您需要类A的内部存储器对于所有其他类或其B,C,D实例是全局的,则可以使用静态模式,例如

class A{

static int myGlobalIntVariable; //which is accessible from everywhere 
static void myHelperMethod1() {

}

or 3. Also you may use singleton as mentioned above which creates an instance that you use everywhere. 或3.您也可以使用上述的singleton来创建您在各处使用的实例。

Just as a remark, you may use singleton or static pattern depending what you prefer when accessing the methods. 仅作说明,您可以使用单例或静态模式,具体取决于访问方法时的偏好。

For static patter you have to call them like: 对于静态模式,您必须像这样调用它们:

A.myHelperMethod();

and for singleton you have to do: 对于单例,您必须执行以下操作:

A.getSingleton().myHelperMethod1();  or A.singleton.myHelperMethod1()

if you have defined a variable called singleton within Class A 如果您在A类中定义了一个名为singleton的变量

Which one to use really depends on your needs and taste :-) 真正使用哪个取决于您的需求和口味:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ArrayList重用单个对象,而不是创建新的对象 - ArrayList re-using single Object, instead of creating new ones 强制cxf-codegen-plugin使用现有生成的类,而不是创建新的类 - Force cxf-codegen-plugin to use existing generated classes instead of creating new ones 我应该在Android而不是普通Java上使用哪些 - Which ones should I use on Android instead of normal Java 我的录音机将旧录音替换为新录音,而不是创建新文件 - My audio recorder replaces old recordings with new ones instead of creating a new file 从Java内部创建新文件时,无法正确使用拉丁字符。 文件名使用奇怪的字符而不是正确的字符 - Can't use latin characters correctly when creating new files from within Java. Filenames get weird characters instead of the correct ones 重新保存对象,而不是在列表中创建一个新对象 - Resaving object instead of creating a new one in list 是否可以在appWidget中重用RemoteViews而不是每次都创建新的? 以及如何存储它们? - Is it possible to reuse RemoteViews in an appWidget instead of creating new ones every time? and how can I store them? 在流中创建新对象并使用setter - creating new object inside a stream and use the setters Android意图创建对象的新实例 - Android intent creating new instance of object 在创建对象时使用带有“new”的变量 - use variable with “new” when creating object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM