我的骰子模拟器会根据用户的输入和模型改变其 UI。我一直在尝试实现 Builder 模式来处理变化和可选参数,同时允许使用 GroupLayout。
目前我正在 Controller 中进行如下调用:
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 语句,因为它们的种类是无限的。谁能帮我理解我可以使用什么抽象、契约或接口(interface)来干净、准确地在 MVC 中构建所需的 UI?
最佳答案
与其尝试在 if 语句
中处理无限组合,为什么不将 the 子句分解为几个更细粒度的子句呢?这样,如果您添加更多新功能,那么您可以轻松地将另一个小子句添加到现有子句中,而不必担心忘记处理另一个组合。
例如:-
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();
关于java - 什么是有用的抽象/契约来协助 Builder 模式构建 MVC UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5055121/