Skip to content

Commit ef3bd18

Browse files
committed
Create/Impl pattern.
1 parent d46cf2e commit ef3bd18

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
part of mediator;
2+
3+
class Component {
4+
get mediator => _mediator;
5+
6+
Mediator? _mediator;
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

0 commit comments

Comments
 (0)