Java 中处理组件输入的最佳实践

Best practice of handling input of components in Java

考虑这 3 个处理输入的示例:

/*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());
        }
    }
}

我想我应该在我的项目中保留一种处理输入的方法。大多数时候,我创建一个 class 来处理输入,并在那里注入所有需要的对象,因此与用户输入相关的每个操作都在一个地方进行管理。当然,我的项目越大,输入处理程序 class 就越大,它开始看起来有点乱。也许我错过了更好的解决方案?请告诉我应该避免哪些示例?

这取决于项目的大小以及您需要的 similar/different 个处理程序。 对于一些非常简单的情况,我会选择选项 A,但如果您几乎没有类似的处理程序,则最好提取到新的 class.

例如,如果文本 "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();
    }
}