Skip to content

Commit b46bdca

Browse files
authored
Merge pull request #73 from ilopX/add-conceptual-command-pattern
Add conceptual command pattern
2 parents 4fcfede + 939e881 commit b46bdca

File tree

9 files changed

+161
-3
lines changed

9 files changed

+161
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
## 0.35.0
2+
- Add conceptual command pattern.
3+
14
## 0.34.0
2-
- Add conceptual single pattern.
5+
- Add conceptual singleton pattern.
36

47
## 0.33.0
58
- Add conceptual builder pattern.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
1111
- [x] **Singleton** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/singleton/conceptual)]
1212
- [ ] **Behavioral**
1313
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware)]
14-
- [x] **Command** - [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor)]
14+
- [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)]
1515
- [x] **Interpreter** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/interpreter/conceptual)]
1616
- [ ] **Iterator**
1717
- [x] **Mediator** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/mediator/conceptual)]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Command Pattern
2+
Command is a behavioral design pattern that turns a request into a stand-alone object that contains
3+
all information about the request.
4+
5+
Tutorial: [here](https://refactoring.guru/design-patterns/command).
6+
7+
## Diagram:
8+
![image](https://user-images.githubusercontent.com/8049534/183062798-c2e9207d-850c-47b6-bbba-3d669299d69f.png)
9+
10+
## Client dode:
11+
```dart
12+
void main() {
13+
final mutStr = MutStr();
14+
15+
final input1 = AddTextCommand('One', mutStr);
16+
final input2 = AddTextCommand('Three', mutStr);
17+
final input3 = InsertTextCommand(' Two ', mutStr, pos: 2);
18+
19+
input1.execute();
20+
print('text = $mutStr'); // mutStr = "One"
21+
22+
input2.execute();
23+
print('text = $mutStr'); // mutStr = "OneThree"
24+
25+
input3.execute();
26+
print('text = $mutStr'); // mutStr = "One Two Three"
27+
28+
input3.undo();
29+
print('text = $mutStr'); // mutStr = "OneThree"
30+
31+
input2.undo();
32+
print('text = $mutStr'); // mutStr = "One "
33+
34+
input1.undo();
35+
print('text = $mutStr'); // mutStr = ""
36+
}
37+
```
38+
39+
### Output:
40+
```
41+
text = One
42+
text = OneThree
43+
text = One Two Three
44+
text = OneThree
45+
text = One
46+
text =
47+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import '../pattern/command.dart';
2+
import '../mut_str/mut_str.dart';
3+
4+
class AddTextCommand implements Command {
5+
final String addedText;
6+
final MutStr mutStr;
7+
8+
AddTextCommand(this.addedText, this.mutStr);
9+
10+
@override
11+
void execute() {
12+
additionPosition = mutStr.len;
13+
mutStr.push(addedText);
14+
}
15+
16+
@override
17+
void undo() {
18+
if (additionPosition == null) {
19+
return;
20+
}
21+
22+
mutStr.delete(additionPosition!, additionPosition! + addedText.length);
23+
}
24+
25+
int? additionPosition;
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import '../pattern/command.dart';
2+
import '../mut_str/mut_str.dart';
3+
4+
class InsertTextCommand extends Command {
5+
final int pos;
6+
final String insertText;
7+
final MutStr mutStr;
8+
9+
InsertTextCommand(this.insertText, this.mutStr, {required this.pos});
10+
11+
@override
12+
void execute() {
13+
_isNotExecute = false;
14+
mutStr.insert(pos + 1, insertText);
15+
}
16+
17+
@override
18+
void undo() {
19+
if (_isNotExecute) {
20+
return;
21+
}
22+
23+
mutStr.delete(pos + 1, insertText.length - 1);
24+
}
25+
26+
bool _isNotExecute = true;
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'mut_str/mut_str.dart';
2+
import 'command/add_text_command.dart';
3+
import 'command/insert_text_command.dart';
4+
5+
void main() {
6+
final mutStr = MutStr();
7+
8+
final input1 = AddTextCommand('One', mutStr);
9+
final input2 = AddTextCommand('Three', mutStr);
10+
final input3 = InsertTextCommand(' Two ', mutStr, pos: 2);
11+
12+
input1.execute();
13+
print('text = $mutStr'); // mutStr = "One"
14+
15+
input2.execute();
16+
print('text = $mutStr'); // mutStr = "OneThree"
17+
18+
input3.execute();
19+
print('text = $mutStr'); // mutStr = "One Two Three"
20+
21+
input3.undo();
22+
print('text = $mutStr'); // mutStr = "OneThree"
23+
24+
input2.undo();
25+
print('text = $mutStr'); // mutStr = "One "
26+
27+
input1.undo();
28+
print('text = $mutStr'); // mutStr = ""
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class MutStr {
2+
void push(String str) {
3+
_buff.addAll(str.split(''));
4+
}
5+
6+
void insert(int pos, String str) {
7+
_buff.insert(pos, str);
8+
}
9+
10+
void delete(int startPos, int len) {
11+
_buff.removeRange(startPos, len);
12+
}
13+
14+
int get len => _buff.length;
15+
16+
@override
17+
String toString() {
18+
return _buff.join('');
19+
}
20+
21+
final _buff = <String>[];
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
abstract class Command {
2+
void execute();
3+
void undo();
4+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: design_patterns_dart
22
description: Dart examples for all classic GoF design patterns.
3-
version: 0.34.0
3+
version: 0.35.0
44
homepage: https://refactoring.guru/design-patterns
55
repository: https://github.com/RefactoringGuru/design-patterns-dart
66
issue_tracker: https://github.com/RefactoringGuru/design-patterns-dart/issue

0 commit comments

Comments
 (0)