File tree Expand file tree Collapse file tree 10 files changed +103
-2
lines changed
patterns/state/three_state Expand file tree Collapse file tree 10 files changed +103
-2
lines changed Original file line number Diff line number Diff line change 1+ ## 0.25.0
2+ - Add conceptual state pattern: Three State.
3+
14## 0.24.1
25- Add flutter icon to project links.
36
Original file line number Diff line number Diff 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 )]
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ part of switcher;
2+
3+ abstract class State {
4+ Switcher get context => _context;
5+
6+ void call ();
7+
8+ late Switcher _context;
9+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11name : design_patterns_dart
22description : Dart examples for all classic GoF design patterns.
3- version : 0.24.1
3+ version : 0.25.0
44homepage : https://refactoring.guru/design-patterns
55repository : https://github.com/RefactoringGuru/design-patterns-dart
66issue_tracker : https://github.com/RefactoringGuru/design-patterns-dart/issue
You can’t perform that action at this time.
0 commit comments