英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论