Skip to content

Conversation

@ilopX
Copy link
Collaborator

@ilopX ilopX commented Aug 5, 2022

Command Pattern

Command is a behavioral design pattern that turns a request into a stand-alone object that contains
all information about the request.

Tutorial: here.

Diagram:

image

Client dode:

void main() {
  final mutStr = MutStr();

  final input1 = AddTextCommand('One', mutStr);
  final input2 = AddTextCommand('Three', mutStr);
  final input3 = InsertTextCommand(' Two ', mutStr, pos: 2);

  input1.execute();
  print('text = $mutStr'); // mutStr = "One"

  input2.execute();
  print('text = $mutStr'); // mutStr = "OneThree"

  input3.execute();
  print('text = $mutStr'); // mutStr = "One Two Three"

  input3.undo();
  print('text = $mutStr'); // mutStr = "OneThree"

  input2.undo();
  print('text = $mutStr'); // mutStr = "One "

  input1.undo();
  print('text = $mutStr'); // mutStr = ""
}

Output:

text = One
text = OneThree
text = One Two Three
text = OneThree
text = One
text = 

@ilopX ilopX merged commit b46bdca into RefactoringGuru:main Aug 5, 2022
@ilopX ilopX deleted the add-conceptual-command-pattern branch August 5, 2022 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant