zam_command_pattern 0.2.0 zam_command_pattern: ^0.2.0 copied to clipboard
Provides utilities to implement the command pattern (a behavioral design pattern).
Command Pattern #
Provides utilities to implement the command pattern.
What's inside the package #
Check out all the components in detail here
How to use #
// Create the base command.
abstract class CarCommand implements Command<void> {}
// Create child command StartEngineCommand.
class StartEngineCommand implements CarCommand {
@override
void execute() {
print('Engine Started.');
}
}
// Create child command ApplyBrakeCommand.
class ApplyBrakeCommand implements CarCommand {
@override
void execute() {
print('Brake Applied.');
}
}
// Now let's see the library in action.
void main() {
// Executing a command is simple.
final command = StartEngineCommand();
command.execute(); // prints 'Engine Started'.
// For complex systems you can use the executor.
// Build the command executor.
// Register StartEngineCommand.
final executor = CommandExecutor.fromBuilders({
StartEngineCommand: () => StartEngineCommand(),
});
// Registered command runs.
executor.execute(StartEngineCommand); // prints 'Engine Started'.
// Unregistered command throws error.
try {
executor.execute(ApplyBrakeCommand); // throws CommandNotDefinedException.
} catch (e) {
// CommandNotDefinedException has occured.
// > Problem: ApplyBrakeCommand is not registered in the factory.
// > Solution: Please add ApplyBrakeCommand to the factory.
print(e);
}
}
To learn more, move on to the example section or check out these dedicated examples in github.