What is a useful abstraction/contract to assist Builder pattern to construct a MVC UI?

188 views Asked by At

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?

2

There are 2 answers

0
limc On BEST ANSWER

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();
0
trashgod On

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.