Skip to content

Instantly share code, notes, and snippets.

@breeze4
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breeze4/7361db0d92ecbccf7769 to your computer and use it in GitHub Desktop.
Save breeze4/7361db0d92ecbccf7769 to your computer and use it in GitHub Desktop.
CQRS/Event Sourcing design pattern question
public enum Mode {
GROUND, AIR, OCEAN;
}
// *** GENERIC STYLE ***
// 1 command to set the mode
public SetModeCommand {
private final Mode mode;
// constructor, getters, etc
}
// handler:
public handle(SetModeCommand command) {
eventBus.publish(new ModeChangedEvent(id, command.getMode()));
}
// *** MORE SPECIFIC STYLE ***
// alternative:
// 3 commands, each to set the mode with a hardcoded value
public SetGroundModeCommand extends AbstractSetModeCommand {
private final Mode mode = GROUND;
// constructor, getters, etc
}
// handler alt 1:
public handle(AbstractSetModeCommand command) {
switch(command.getMode()) {
case: GROUND {
eventBus.publish(new ChangedToGroundModeEvent(id));
}
}
}
// handler alt 2:
public handle(SetGroundModeCommand command) {
eventBus.publish(new ChangedToGroundModeEvent(id));
}
public handle(SetAirModeCommand command) {
eventBus.publish(new ChangedToAirModeEvent(id));
}
public handle(SetOceanModeCommand command) {
eventBus.publish(new ChangedToOceanModeEvent(id));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment