簡體   English   中英

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

我想我應該保留一種處理項目輸入的方法。 大多數時候,我創建一個類來處理輸入,並且將所有需要的對象注入那里,因此與用戶輸入相關的每個動作都在一個地方進行管理。 當然,我的項目越大,輸入處理程序類就越大,並且開始看起來有些混亂。 也許我缺少更好的解決方案? 請告訴我應該避免哪些示例?

這取決於項目的大小以及您需要多少個類似/不同的處理程序。 在某些非常簡單的情況下,我會選擇選項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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM