我发现人们经常使用一个处理多个事件源(对于几个按钮的exeampl一个OnClickHandler(视图v))。一个OnClickHandler与开关/案件与匿名内部类
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mButton1 = (Button) v.findViewById(R.id.button1);
mButton2 = (Button) v.findViewById(R.id.button2);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
}
@Override
public boolean onClick(final View v) {
if (v == mButton1) {
title = getString(R.string.action1Title);
defaultText = getText1();
} else if (v == mUriLabel) {
title = getString(R.string.action2Title);
defaultText = getText2;
}
// Here some common code
}
在这里,我们有两个功能:然后处理程序取决于参数v
一个分支,比如选择。 onCreateView只是将所有事件导向单个处理程序。而onClick是由它自己处理的,它应该确定事件的来源并且转到一个分支或另一个分支。
另一方面,我们可以在实现onClick的anonymouse内部类中实例化。就像这样:
// In cases, when there is some common part,
// we need additional interface to separate common part and
// customizable action.
interface ICustomAction {
void doSomeAction();
}
class BaseHandler implements View.ClickListener {
ICustomAction mCustomAction;
// Constructor which receive specific action
CommonHandler(ICustomAction customAction) {
mCustomAction = customAction;
}
@Override
public boolean onClick(final View v) {
mCustomAction.doSomeAction();
// Here some common code
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mButton1 = (Button) v.findViewById(R.id.button1);
mButton1.setOnClickListener(new BaseHandler(new ICustomAction() {
@Override
void doSomeAction() {
title = getString(R.string.action1Title);
defaultText = getText1();
}
}));
mButton2 = (Button) v.findViewById(R.id.button2);
mButton2.setOnClickListener(new BaseHandler(new ICustomAction() {
@Override
void doSomeAction() {
title = getString(R.string.action2Title);
defaultText = getText2();
}
}));
在这里,我们有更复杂的通信类,但局部的附近注册处理的具体差异。我们用一个虚拟函数(定义在ICustomAction接口中)替换开关/外壳。
而且IDE可以简化表示这样的匿名类,并显示他们像拉姆达功能
mButton2.setOnClickListener(new BaseHandler(() ->{
title = getString(R.string.action2Title);
defaultText = getText2();
}));
所以,登记处理变得更加紧凑,但还是老样子包含有意义的差异。
问题是,在switch/case statment中使用一个处理程序以及使用anonymouse内部类的方法更可取的原因是什么?
如果您根据类似的实现进行比较,它会更有意义。例如。如果你已经使用了'mButton1.setOnClickListener(新的View.OnClickListener'而不是这个自制的'复杂'层次结构 –
我想展示一些常见代码的例子,它总是执行直接继承'View.OnClickListener'导致代码重复在这种情况下 – Piroxiljin