创建接口类的实例与将该接口添加到主类以进行回调相比的优势是什么?

huangapple go评论57阅读模式
英文:

What is the advantage of creating an instance of an Interface Class instead of just adding that Interface to the main class for Callbacks?

问题

I am working on an Android app and I came across the following code for a set of Callbacks from C++.
I am wondering why, using an instance of the interface class JNIListener instead of implementing the interface and adding the Callbacks directly, makes sense.

public interface JNIListener {
    void onProcessingStarted();
    void onResultAvailable();
} 

public class MainFragment extends Fragment {

    ......

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        .....  

        mListener = new JNIListener() {
            @Override
            public void onProcessingStarted() {
                NavDirections action = MainFragmentDirections.actionMainFragmentToResultFragment();
                NavHostFragment.findNavController(ThisFragment).navigate(action);
            }

            @Override
            public void onResultAvailable(){
                ....
            }
        }
        subscribeListener(mListener);
    }
}

instead of :

public class MainFragment extends Fragment implements JNIListener{

    ......

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        .....  

        subscribeListener(this);
    }

    @Override
    public void onProcessingStarted() {
        NavDirections action = MainFragmentDirections.actionMainFragmentToResultFragment();
        NavHostFragment.findNavController(thisFragment).navigate(action);
    }

    @Override
    public void onResultAvailable(){
        ....
    }
}

I don't get the advantages of the first approach.
The second makes more sense to me: The callbacks have complete access to the members of the MainFragment.

The first approach should have its pro, otherwise why would someone have done it that way.

The person who wrote that code is for sure more experienced than I am. If I am doing something weird by preferring the second approach, it would be nice to understand why it is weird, learn something and avoid the issue next time.

英文:

I am working on an Android app and I came across the following code for a set of Callbacks from C++.
I am wondering why, using an instance of the interface class JNIListener instead of implementing the interface and adding the Callbacks directly, makes sense.

public interface JNIListener {
    void onProcessingStarted();
    void onResultAvailable();
} 


public class MainFragment extends Fragment {

......

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        .....  

        mListener = new JNIListener() {
            @Override
            public void onProcessingStarted() {
                NavDirections action = MainFragmentDirections.actionMainFragmentToResultFragment();
                NavHostFragment.findNavController(ThisFragment).navigate(action);
            }

            @Override
            public void onResultAvailable(){
                ....
            }
        }
        subscribeListener(mListener);
    }
}

instead of :

public class MainFragment extends Fragment implements JNIListener{

......

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        .....  

        subscribeListener(this);
    }

    @Override
    public void onProcessingStarted() {
        NavDirections action = MainFragmentDirections.actionMainFragmentToResultFragment();
        NavHostFragment.findNavController(thisFragment).navigate(action);
    }

    @Override
    public void onResultAvailable(){
        ....
    }
}

I don't get the advantages of the first approach.
The second makes more sense to me: The callbacks have complete access to the members of the MainFragment.

The first approach should have its pro, otherwise why would someone have done it that way.

The person who wrote that code is for sure more experienced than I am. If I am doing something weird by preferring the second approach, it would be nice to understand why it is weird, learn something and avoid the issue next time.

答案1

得分: 2

第一种方法唯一的优点是,如果你的类需要两个或更多用于不同目的的接口。在其他所有情况下,我会使用第二种方法,因为它使代码看起来更干净。

英文:

The only advantage for the first approach is If you need two or more interfaces for separate things in your class. In every other case, I would use the second approach since it makes the code looks cleaner.

huangapple
  • 本文由 发表于 2020年8月7日 16:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63297912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定