考虑这三个处理输入的例子:
/*EXAMPLE A*/
public class HandlingInputExampleA
{
private Label labelFromOtherClass; //injected by setter/constructor
private String myText = "hello ";
private int myInt = 1;
private void init()
{
Button button = new Button();
button.addClickListener(event -> labelFromOtherClass.setCaption(myText + myInt));
}
}
public class HandlingInputExampleB
{
private ClickListener inputHandler; //injected by setter/constructor
private String myText = "hello ";
private int myInt = 2;
private void init()
{
Button button = new Button();
button.addClickListener(inputHandler);
}
}
/*EXAMPLE B*/
public class HandlingInputExampleB
{
private ClickListener inputHandler; //injected by setter/constructor
private String myText = "hello ";
private int myInt = 2;
private void init()
{
Button button = new Button();
button.addClickListener(inputHandler);
}
}
public class InputHandlerB implements ClickListener
{
private HandlingInputExampleB exampleB; //injected by setter
private Label label; //injected by setter/constructor
@Override
public void buttonClick(ClickEvent event)
{
Button button = event.getButton();
if( button == exampleB.getButton() )
{
label.setCaption(exampleB.getMyText() + exampleB.getMyInt());
}
}
}
/*EXAMPLE C*/
public class HandlingInputExampleC
{
private ClickListener inputHandler; //injected by setter/constructor
private String myText = "hello ";
private int myInt = 2;
private void init()
{
Button button = new Button();
button.setData(this);
button.addClickListener(inputHandler);
}
}
public class InputHandlerC implements ClickListener
{
private Label label; //injected by setter/constructor
@Override
public void buttonClick(ClickEvent event)
{
Button button = event.getButton();
if( button.getData() instanceof HandlingInputExampleC )
{
HandlingInputExampleC exampleC = (HandlingInputExampleC)button.getData();
label.setCaption(exampleC.getMyText() + exampleC.getMyInt());
}
}
}
我想我应该在项目中保留一种处理输入的方法。大多数时候我正在创建一个类来处理输入,并且我在那里注入所有需要的对象,因此与用户输入相关的每个操作都在一个地方进行管理。当然,我的项目越大,输入处理程序类越大,它开始看起来有点凌乱。也许我错过了更好的解决方案?请告诉我应该避免哪些例子?
答案 0 :(得分:1)
这取决于项目的大小以及您需要多少相似/不同的处理程序。 对于一些非常简单的情况,我会选择A,但如果你有类似的处理程序,最好提取到一个新类。
例如,如果文本“exampleC.getMyText()+ exampleC.getMyInt()”在执行期间没有改变,我宁愿:
public class HandlingInputExample {
private ClickListener inputHandler; //injected by setter/constructor
private String myText = "hello ";
private int myInt = 2;
private void init() {
Button button = new Button();
button.setData(this);
button.addClickListener(new SetCaptionClickListener(example.getMyText() + example.getMyInt()));
}
}
public class SetCaptionClickListener implements ClickListener {
private Label label; //injected by setter/constructor
private String caption;
public SetCaptionClickListener(String caption) {
this.caption = caption;
}
@Override
public void buttonClick(ClickEvent event) {
label.setCaption(caption);
}
}
但是如果数据可能会发生变化,您可以添加另一个负责检索处理程序所需信息的图层,例如:
public class HandlingInputExample {
private ClickListener inputHandler; //injected by setter/constructor
private String myText = "hello ";
private int myInt = 2;
private void init() {
Button button = new Button();
button.setData(this);
button.addClickListener(new SetCaptionClickListener(new Context(this)));
}
}
public class SetCaptionClickListener implements ClickListener {
private Label label; //injected by setter/constructor
private Context context;
public SetCaptionClickListener(Context context) {
this.context = context;
}
@Override
public void buttonClick(ClickEvent event) {
label.setCaption(context.getCaption());
}
}
public class Context {
HandlingInputExample handlingInputExample;
public Context(handlingInputExample handlingInputExample) {
this.handlingInputExample = handlingInputExample;
}
public String getCaption() {
return handlingInputExample.getMyText() + handlingInputExample.getMyInt();
}
}