简体   繁体   English

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

[英]Best practice of handling input of components in Java

Consider this 3 examples of handling input: 考虑以下处理输入的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());
        }
    }
}

I suppose I should keep one way of handling input in my project. 我想我应该保留一种处理项目输入的方法。 Most of the time I'm creating one class to deal with input and I'm injecting all needed objects there so every action associated with user input is managed in one place. 大多数时候,我创建一个类来处理输入,并且将所有需要的对象注入那里,因此与用户输入相关的每个动作都在一个地方进行管理。 Of course the bigger my project become, the bigger input handler class is, and its starting to look a bit messy. 当然,我的项目越大,输入处理程序类就越大,并且开始看起来有些混乱。 Maybe I'm missing much better solution? 也许我缺少更好的解决方案? Please tell me which of those examples should I avoid? 请告诉我应该避免哪些示例?

It depends on the size of the project and how many similar/different handlers you'll need. 这取决于项目的大小以及您需要多少个类似/不同的处理程序。 For some very simple situations I would go for option A but if you have few similar handlers it's better to extract to a new class. 在某些非常简单的情况下,我会选择选项A,但如果您没有几个类似的处理程序,则最好提取到新类。

For example if the text "exampleC.getMyText() + exampleC.getMyInt()" is not changing during the execution I would prefer : 例如,如果文本“ 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);
    }
}

But if the data may change you can add another layer that have the responsability to retrieve the information needed by the handler, something like: 但是,如果数据可能更改,则可以添加另一层来负责检索处理程序所需的信息,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM