zam_command_pattern 0.2.0 copy "zam_command_pattern: ^0.2.0" to clipboard
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.

Status #

Build

Contributors #

License #

BSD 3-Clause License

0
likes
150
pub points
3%
popularity

Publisher

verified publisherzamstation.com

Provides utilities to implement the command pattern (a behavioral design pattern).

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

zam_core, zam_factory_pattern

More

Packages that depend on zam_command_pattern