我的骰子模拟器根据用户的输入和模型而异。我一直在尝试实现构建器模式来处理变体和可选参数,同时允许使用Grouplayout。

目前我在控制器中拨打这样的电话:

if ((model.simRolls <> null) && (inputEvent.getSource == outputBtn) && (model.testType.equals("Success"))) {
    SimView outputScreen = new SimView.Builder(jframe, jpanel).testLabel("SUCCESS TEST OUTPUT", GroupAlignment.LEADING).outputLabel(model.simRolls, GroupAlignment.CENTER).actionButton("Next", GroupAlignment.TRAILING).build();
}
.

我讨厌if陈述,因为它们是无限的。任何人都可以帮助我了解我可以使用哪些抽象或合同或界面来清洁和准确地在MVC中建立所需的UI?

有帮助吗?

解决方案

Instead of trying to handle infinite combination in your if-statement, why don't break up the the clause into several more granular clauses? This way, if you add more new features, then you can easily add another small clause into your existing clauses without worrying about forgetting to handle yet-another-combination(s).

For example:-

SimViewBuilder  builder = new SimViewBuilder(jframe, jpanel);

if (model.simRolls != null) {
    builder = builder.actionButton("Next", GroupAlignment.TRAILING);
}

if (inputEvent.getSource == outputBtn) {
    builder = builder.outputLabel(model.simRolls, GroupAlignment.CENTER);
}

if (model.testType.equals("Success")) {
    builder = builder.testLabel("SUCCESS TEST OUTPUT", GroupAlignment.LEADING);
}

... // add more if statements for other features and configure the builder accordingly

// finally, build it
SimView outputScreen = builder.build();

其他提示

Typically, you arrange for a view to register itself as an observer of a model, as suggested in this example. The layout would be known only to a view that actually uses it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top