What is a useful abstraction/contract to assist Builder pattern to construct a 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 o开发者_JAVA百科r interface I can possibly use to cleanly and accurately build the required UI in the MVC?
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.
精彩评论