File tree Expand file tree Collapse file tree 10 files changed +129
-3
lines changed
patterns/mediator/conceptual Expand file tree Collapse file tree 10 files changed +129
-3
lines changed Original file line number Diff line number Diff line change 1+ ## 0.26.0
2+ - Add conceptual mediator pattern.
3+
14## 0.25.0
25- Add conceptual state pattern: Three State.
36
Original file line number Diff line number Diff line change @@ -14,10 +14,10 @@ It contains **Dart** examples for all classic **GoF** design patterns.
1414 - [x] ** Command** - [[ Text Editor] ( https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor )]
1515 - [ ] Interpreter
1616 - [ ] ** Iterator**
17- - [ ] ** Mediator**
17+ - [x ] ** Mediator** [[ Conceptual ] ( https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/mediator/conceptual )]
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** - [[ 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 )]
20+ - [x] ** State** - [[ Conceptual ] ( 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+ # Mediator Pattern
2+ Mediator is a behavioral design pattern that lets you reduce chaotic dependencies between objects.
3+ The pattern restricts direct communications between the objects and forces them to collaborate only
4+ via a mediator object.
5+
6+ Tutorial: [ here] ( https://refactoring.guru/design-patterns/mediator ) .
7+
8+ ## Conceptual diagram:
9+ ![ image] ( https://user-images.githubusercontent.com/8049534/173237874-971dd4e7-2e74-4cac-bcea-77b88255adad.png )
10+
11+ ### Client code:
12+ ``` dart
13+ void main() {
14+ final component1 = Component1();
15+ final component2 = Component2();
16+
17+ ConcreteMediator(component1, component2);
18+
19+ component1.doOne();
20+ print('');
21+ component2.doTwo();
22+ }
23+ ```
24+
25+ ### Output:
26+ ```
27+ call Component1.doOne()
28+ ConcreteMediator.notify(event: "doOne")
29+ ConcreteMediator.reactComponentOne()
30+ use component2.name = "Two"
31+
32+ call Component2.doTwo()
33+ ConcreteMediator.notify(event: "doTwo")
34+ ConcreteMediator.reactComponentTwo()
35+ use component1.sate = "Cmp1"
36+ ```
37+
Original file line number Diff line number Diff line change 1+ import '../pattern/mediator.dart' ;
2+
3+ class Component1 extends Component {
4+ final sate = 'Cmp1' ;
5+
6+ void doOne () {
7+ print ('call Component1.doOne()' );
8+ mediator? .notify (this , 'doOne' );
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ import '../pattern/mediator.dart' ;
2+
3+ class Component2 extends Component {
4+ final name = 'Two' ;
5+
6+ void doTwo () {
7+ print ('call Component2.doTwo()' );
8+ mediator? .notify (this , 'doTwo' );
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ import '../components/component1.dart' ;
2+ import '../components/component2.dart' ;
3+ import '../pattern/mediator.dart' ;
4+
5+ class ConcreteMediator extends Mediator {
6+ final Component1 component1;
7+ final Component2 component2;
8+
9+ ConcreteMediator (this .component1, this .component2) {
10+ applyThisMediator (component1);
11+ applyThisMediator (component2);
12+ }
13+
14+ @override
15+ void notify (Component component, String event) {
16+ print ('ConcreteMediator.notify(event: "$event ")' );
17+
18+ if (component == component1) {
19+ reactComponentOne ();
20+ } else if (component == component2) {
21+ reactComponentTwo ();
22+ }
23+ }
24+
25+ void reactComponentOne () {
26+ print ('ConcreteMediator.reactComponentOne()' );
27+ print ('use component2.name = "${component2 .name }"' );
28+ }
29+
30+ void reactComponentTwo () {
31+ print ('ConcreteMediator.reactComponentTwo()' );
32+ print ('use component1.sate = "${component1 .sate }"' );
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ import 'components/component1.dart' ;
2+ import 'components/component2.dart' ;
3+ import 'concrete_mediator/concrete_mediator.dart' ;
4+
5+ void main () {
6+ final component1 = Component1 ();
7+ final component2 = Component2 ();
8+
9+ ConcreteMediator (component1, component2);
10+
11+ component1.doOne ();
12+ print ('' );
13+ component2.doTwo ();
14+ }
Original file line number Diff line number Diff line change 1+ part of mediator;
2+
3+ class Component {
4+ get mediator => _mediator;
5+
6+ Mediator ? _mediator;
7+ }
Original file line number Diff line number Diff line change 1+ library mediator;
2+
3+ part 'component.dart' ;
4+
5+ abstract class Mediator {
6+ void notify (Component component, String event);
7+
8+ void applyThisMediator (Component component) {
9+ component._mediator = this ;
10+ }
11+ }
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.25 .0
3+ version : 0.26 .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