什么是有用的抽象/契约来协助 Builder 模式构建 MVC UI?

发布于 2024-10-18 00:52:05 字数 567 浏览 3 评论 0原文

我的骰子模拟器会根据用户的输入和模型改变其 UI。我一直在尝试实现 Builder 模式来处理变化和可选参数,同时允许使用 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?

My dice simulator varies its UI depending on the user's input and the model. I have been trying to implement the Builder pattern to handle the variation and optional parameters, while allowing for the use of GroupLayout.

At the moment I am making a call like this in the 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();
}

I hate the if statements because they are infinite in variety. Can anyone please help me understand what abstraction or contract or interface I can possibly use to cleanly and accurately build the required UI in the MVC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你的占有欲 2024-10-25 00:52:05

与其尝试在 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();

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();
初心 2024-10-25 00:52:05

通常,您会安排视图将自身注册为模型的观察者,如本示例中所建议的。该布局只有实际使用它的视图才知道。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文