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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.35.0
- Add conceptual command pattern.

## 0.34.0
- Add conceptual single pattern.
- Add conceptual singleton pattern.

## 0.33.0
- Add conceptual builder pattern.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
- [x] **Singleton** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/singleton/conceptual)]
- [ ] **Behavioral**
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware)]
- [x] **Command** - [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor)]
- [x] **Command** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/conceptual)] [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor)]
- [x] **Interpreter** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/interpreter/conceptual)]
- [ ] **Iterator**
- [x] **Mediator** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/mediator/conceptual)]
Expand Down
47 changes: 47 additions & 0 deletions patterns/command/conceptul/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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](https://refactoring.guru/design-patterns/command).

## Diagram:
![image](https://user-images.githubusercontent.com/8049534/183062798-c2e9207d-850c-47b6-bbba-3d669299d69f.png)

## Client dode:
```dart
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 =
```
26 changes: 26 additions & 0 deletions patterns/command/conceptul/command/add_text_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import '../pattern/command.dart';
import '../mut_str/mut_str.dart';

class AddTextCommand implements Command {
final String addedText;
final MutStr mutStr;

AddTextCommand(this.addedText, this.mutStr);

@override
void execute() {
additionPosition = mutStr.len;
mutStr.push(addedText);
}

@override
void undo() {
if (additionPosition == null) {
return;
}

mutStr.delete(additionPosition!, additionPosition! + addedText.length);
}

int? additionPosition;
}
27 changes: 27 additions & 0 deletions patterns/command/conceptul/command/insert_text_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import '../pattern/command.dart';
import '../mut_str/mut_str.dart';

class InsertTextCommand extends Command {
final int pos;
final String insertText;
final MutStr mutStr;

InsertTextCommand(this.insertText, this.mutStr, {required this.pos});

@override
void execute() {
_isNotExecute = false;
mutStr.insert(pos + 1, insertText);
}

@override
void undo() {
if (_isNotExecute) {
return;
}

mutStr.delete(pos + 1, insertText.length - 1);
}

bool _isNotExecute = true;
}
29 changes: 29 additions & 0 deletions patterns/command/conceptul/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'mut_str/mut_str.dart';
import 'command/add_text_command.dart';
import 'command/insert_text_command.dart';

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 = ""
}
22 changes: 22 additions & 0 deletions patterns/command/conceptul/mut_str/mut_str.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class MutStr {
void push(String str) {
_buff.addAll(str.split(''));
}

void insert(int pos, String str) {
_buff.insert(pos, str);
}

void delete(int startPos, int len) {
_buff.removeRange(startPos, len);
}

int get len => _buff.length;

@override
String toString() {
return _buff.join('');
}

final _buff = <String>[];
}
4 changes: 4 additions & 0 deletions patterns/command/conceptul/pattern/command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
abstract class Command {
void execute();
void undo();
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: design_patterns_dart
description: Dart examples for all classic GoF design patterns.
version: 0.34.0
version: 0.35.0
homepage: https://refactoring.guru/design-patterns
repository: https://github.com/RefactoringGuru/design-patterns-dart
issue_tracker: https://github.com/RefactoringGuru/design-patterns-dart/issue
Expand Down