Command builders
1 min
The command pattern encapsulates interface actions into reusable objects. This separates execution logic from concrete widgets into a testable unit. Because commands have no reference to the UI or a visual representation, the builder pattern generates widgets from commands in a generic manner.
A command consumer (or other object responsible for displaying commands) instantiates a builder based on the desired widget type. For example, to display buttons, use a button builder.
The builders have two public methods:
- create – Takes a command and a container. Builds the widget from the command and uses command properties to update the UI when the command state changes.
- remove – Takes a command and a container. Removes the widget that represents the given command from the container.
Example implementation
The following example shows the button builder in use:
require([
"dijit/Toolbar",
"epi/shell/command/builder/ButtonBuilder",
"epi/shell/command/ToggleCommand"
],
function (Toolbar, ButtonBuilder, ToggleCommand) {
var toolbar = new Toolbar(),
builder = new ButtonBuilder(),
model = {
enabled: true
},
command = new ToggleCommand({
model: model,
property: "enabled"
});
builder.create(command, toolbar);
}
);