Skip to content

Commit 3e5bd42

Browse files
authored
Merge pull request #61 from ilopX/three_state
Add conceptual state pattern: Three state
2 parents 080323d + a3b573b commit 3e5bd42

File tree

10 files changed

+103
-2
lines changed

10 files changed

+103
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.25.0
2+
- Add conceptual state pattern: Three State.
3+
14
## 0.24.1
25
- Add flutter icon to project links.
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
1717
- [ ] **Mediator**
1818
- [x] **Memento** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/conceptual)] [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) Memento Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/memento_editor)]
1919
- [x] **Observer** - [[Open-Close Editor Events](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/open_close_editor_events)] [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/app_observer)] [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) Subscriber Flutter Widget](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/subscriber_flutter_widget)]
20-
- [x] **State** - [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) State Manipulator](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/state/manipulator_state)]
20+
- [x] **State** - [[Three State](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/state/three_state)] [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) State Manipulator](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/state/manipulator_state)]
2121
- [ ] **Template Method**
2222
- [X] **Visitor** [[Shape XML Exporter](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/visitor/shapes_exporter)]
2323
- [X] **Strategy** [[Reservation Cargo Spaces](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/strategy/reservation_cargo_spaces)]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# State Pattern
2+
State is a behavioral design pattern that lets an object alter its behavior when its internal state
3+
changes. It appears as if the object changed its class.
4+
5+
Tutorial: [here](https://refactoring.guru/design-patterns/state).
6+
7+
### Diagram:
8+
![image](https://user-images.githubusercontent.com/8049534/172000870-a96c6dd2-5b6a-4a64-b3a9-4edb75dd8076.png)
9+
10+
### Client code:
11+
```dart
12+
void main() {
13+
final switcher = Switcher(
14+
initState: One(),
15+
);
16+
17+
switcher.call(); // call(1): One
18+
switcher.call(); // call(2): Two
19+
switcher.call(); // call(3): Three
20+
switcher.call(); // call:(4) One
21+
}
22+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'pattern/switcher.dart';
2+
import 'states/one.dart';
3+
4+
void main() {
5+
final switcher = Switcher(
6+
initState: One(),
7+
);
8+
9+
switcher.call(); // call(1): One
10+
switcher.call(); // call(2): Two
11+
switcher.call(); // call(3): Three
12+
switcher.call(); // call:(4) One
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
part of switcher;
2+
3+
abstract class State {
4+
Switcher get context => _context;
5+
6+
void call();
7+
8+
late Switcher _context;
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library switcher;
2+
3+
part 'state.dart';
4+
5+
class Switcher {
6+
Switcher({required State initState}) {
7+
changeState(initState);
8+
}
9+
10+
int get calls => _calls;
11+
12+
void call() {
13+
_calls++;
14+
_state.call();
15+
}
16+
17+
void changeState(State newState) {
18+
_state = newState;
19+
_state._context = this;
20+
}
21+
22+
late State _state;
23+
int _calls = 0;
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import '../pattern/switcher.dart';
2+
import 'two.dart';
3+
4+
class One extends State {
5+
@override
6+
void call() {
7+
print('call(${context.calls}): One');
8+
context.changeState(Two());
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import '../pattern/switcher.dart';
2+
import 'one.dart';
3+
4+
class Three extends State {
5+
@override
6+
void call() {
7+
print('call(${context.calls}): Three');
8+
context.changeState(One());
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import '../pattern/switcher.dart';
2+
import 'three.dart';
3+
4+
class Two extends State {
5+
@override
6+
void call() {
7+
print('call(${context.calls}): Two');
8+
context.changeState(Three());
9+
}
10+
}

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.24.1
3+
version: 0.25.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)