Skip to content

Add conceptual command pattern #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2022
Merged

Add conceptual command pattern #73

merged 3 commits into from
Aug 5, 2022

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