简体   繁体   English

Java:最佳做法-回调与成员类

[英]Java: Best practice - Callback vs member class

In Java, what code to call a method in a singleton would be better practice, and why? 在Java中,最好在单例中使用什么代码来调用方法,为什么?

Please note that the following code is psudocode, not necessarily compilable code. 请注意,以下代码是伪代码,不一定是可编译代码。 I ask this, as Method 2 (calling a method directly) is easier to implement in code, but not as often seen from my experience. 我问这个问题,因为方法2(直接调用方法)更容易在代码中实现,但从我的经验来看并不常见。

I'm developing for Android, but I suppose this question could apply to any Java program. 我正在为Android开发,但是我想这个问题可能适用于任何Java程序。

Something happens in class B. A method in Class A must be called. B类中发生了某些事情。必须调用A类中的方法。

Method 1: Interface registered in ClassA is called from ClassB 方法1:从ClassB调用在ClassA中注册的接口

public class ClassA
{
    // Member class B object
    ClassB mClassBObject = new ClassB();

    // Singleton has a private constructor
    private ClassA(){}

    public void onCreate() // Or main, or whatever method...
    {
        // Set callback for ClassB
        mClassBObject.setOnSomethingHappened
        (
            new OnSomethingHappened()
            {
                public void callback()
                {
                    // Do something in Class A called in Class B
                }
            }
        );
    }

}

public class ClassB
{
    // Registered member callback
    OnSomethingHappened mCallback;

    // Interface for callback
    public interface OnSomethingHappened()
    {
        public void callback();
    }

    // Method to set callback for this object
    public void setOnSomethingHappened(OnSomethingHappened callback)
    {
        mCallback = callback;
    }     

    // A method that invokes the callback in Class A
    private void someMethod()
    {
        if (mCallback != null)
        {
            mCallback.callback();
        }        
    }

}

Method 2: Calling a method directly from ClassA in ClassB 方法2:直接从ClassB中的ClassA调用方法

// We could also call a static method, but in this example, we are assuming a Singleton.
public class ClassA
{
    // Reference to self
    private static mSelf;

    // Singleton has a private constructor
    private ClassA(){}

    public void onCreate() // Or main, etc
    {
        mSelf = this; // Store a reference to this Singleton class
    }

    public void someMethod()
    {
        // Do something in ClassA
    }

    public static getSelf()
    {
        return mSelf;
    }
}

public class ClassB
{

    // Code...
    private void someMethodInClassB()
    {
        // Get ClassA to call
        ClassA classAObject = ClassA.getSelf();

        if (classAObject != null)
        {
            // Call method in ClassA
            classAObject.someMethod();
        }
    }


}

It really depends on which way you want the dependency chain to go. 这实际上取决于您希望依赖链走的方式。 I'm going to generalize the pattern I think you're trying to describe. 我将归纳一下我认为您要描述的模式。

I'm the second example, B depends on A. As you mentioned, this is simpler, since what we're trying to setup is exactly that relationship. 我是第二个示例,B依赖于A。正如您提到的,这很简单,因为我们要设置的正是这种关系。

In the first example, A depends on B. Technically this could be written so that A and B both depend on C (the callback mechanism). 在第一个示例中,A依赖于B。从技术上讲,可以这样编写,以便A和B都依赖于C(回调机制)。 This kind of setup is popular as it reduces coupling between the components (A and B), allowing more flexibility for future changes. 这种设置很流行,因为它减少了组件(A和B)之间的耦合,为将来的更改提供了更大的灵活性。

The way you have written it doesn't quite capture this reduction of coupling in a "best practice" sort of way though... You'll find good examples where B represents a library or third party component, and A is your code. 但是,您编写的方式并没有完全以“最佳实践”的方式捕获这种耦合的减少。...您会找到很好的示例,其中B代表库或第三方组件,而A是您的代码。 Clearly the library can't depend directly on your code, so this style of dependency inversion is used. 显然,该库不能直接依赖您的代码,因此使用了这种依赖关系反转样式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM