File tree 6 files changed +156
-3
lines changed
6 files changed +156
-3
lines changed Original file line number Diff line number Diff line change 26
26
17 . 中介者模式
27
27
18 . 备忘录模式
28
28
19 . 观察者模式
29
- 20 . 状态模式
29
+ 20 . 状态模式
30
+ 21 . 策略模式
Original file line number Diff line number Diff line change @@ -59,9 +59,12 @@ class StringConst {
59
59
//备忘录模式
60
60
static const String MEMORY_ = "备忘录模式" ;
61
61
62
- //备忘录模式
62
+ //观察者模式
63
63
static const String OBSERVER_ = "观察者模式" ;
64
64
65
- //备忘录模式
65
+ //状态模式
66
66
static const String STATE_ = "状态模式" ;
67
+
68
+ //策略模式
69
+ static const String STRATEGY_ = "策略模式" ;
67
70
}
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import 'package:flutter_design/page/midd/mid_page.dart';
16
16
import 'package:flutter_design/page/observer/observer_page.dart' ;
17
17
import 'package:flutter_design/page/proxy/proxy_page.dart' ;
18
18
import 'package:flutter_design/page/state/state_page.dart' ;
19
+ import 'package:flutter_design/page/strategy/strategy_page.dart' ;
19
20
20
21
import 'constant/page_const.dart' ;
21
22
import 'constant/string_const.dart' ;
@@ -55,6 +56,7 @@ class MyApp extends StatelessWidget {
55
56
PageConst .MEMENTO_PAGE : (context) => MemoryPage (),
56
57
PageConst .OBSERVER_PAGE : (context) => ObserverPage (),
57
58
PageConst .STATE_PAGE : (context) => StatePage (),
59
+ PageConst .STRATEGY_PAGE : (context) => StrategyPage (),
58
60
},
59
61
);
60
62
}
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ Map<String, String> map = {
35
35
PageConst .MEMENTO_PAGE : StringConst .MEMORY_ ,
36
36
PageConst .OBSERVER_PAGE : StringConst .OBSERVER_ ,
37
37
PageConst .STATE_PAGE : StringConst .STATE_ ,
38
+ PageConst .STRATEGY_PAGE : StringConst .STRATEGY_ ,
38
39
};
39
40
40
41
class _HomePageState extends State <HomePage > {
Original file line number Diff line number Diff line change
1
+ import 'package:flutter/cupertino.dart' ;
2
+
3
+ /// Created by NieBin on 2020-04-15
4
+ /// Github: https://github.com/nb312
5
+
6
+ abstract class IMove {
7
+ void run ();
8
+ }
9
+
10
+ class Fly extends IMove {
11
+ @override
12
+ void run () {
13
+ debugPrint ("我用飞的不香吗" );
14
+ }
15
+ }
16
+
17
+ //
18
+ class Run extends IMove {
19
+ @override
20
+ void run () {
21
+ debugPrint ("跑步使我快乐" );
22
+ }
23
+ }
24
+
25
+ class Walk extends IMove {
26
+ @override
27
+ void run () {
28
+ debugPrint ("散散步也不无不行" );
29
+ }
30
+ }
31
+
32
+ class ByBike extends IMove {
33
+ @override
34
+ void run () {
35
+ debugPrint ("骑行也是很环保的" );
36
+ }
37
+ }
38
+
39
+ class ByBus extends IMove {
40
+ @override
41
+ void run () {
42
+ debugPrint ("还是老司机开车快点" );
43
+ }
44
+ }
45
+
46
+ class Context implements IMove {
47
+ IMove move;
48
+
49
+ @override
50
+ void run () {
51
+ move? .run ();
52
+ }
53
+ }
Original file line number Diff line number Diff line change
1
+ import 'package:flutter/material.dart' ;
2
+ import 'package:flutter_design/page/strategy/strategy_mode.dart' ;
3
+
4
+ /// Created by NieBin on 2020-04-15
5
+ /// Github: https://github.com/nb312
6
+
7
+
8
+ class StrategyPage extends StatefulWidget {
9
+ @override
10
+ _StrategyPageState createState () => _StrategyPageState ();
11
+ }
12
+
13
+ class _StrategyPageState extends State <StrategyPage > {
14
+ Context _context;
15
+
16
+ @override
17
+ void initState () {
18
+ _context = Context ();
19
+ super .initState ();
20
+ }
21
+
22
+ @override
23
+ Widget build (BuildContext context) {
24
+ return Scaffold (
25
+ appBar: AppBar (
26
+ title: Text ("策略模式" ),
27
+ ),
28
+ body: Container (
29
+ constraints: BoxConstraints .expand (),
30
+ child: Column (
31
+ crossAxisAlignment: CrossAxisAlignment .center,
32
+ children: < Widget > [
33
+ FlatButton (
34
+ color: Colors .red,
35
+ child: Padding (
36
+ padding: const EdgeInsets .all (8.0 ),
37
+ child: Text ("开始飞" ),
38
+ ),
39
+ onPressed: () {
40
+ _context.move = Fly ();
41
+ _context.run ();
42
+ },
43
+ ),
44
+ FlatButton (
45
+ color: Colors .green,
46
+ child: Padding (
47
+ padding: const EdgeInsets .all (8.0 ),
48
+ child: Text ("开始跑" ),
49
+ ),
50
+ onPressed: () {
51
+ _context.move = Run ();
52
+ _context.run ();
53
+ },
54
+ ),
55
+ FlatButton (
56
+ color: Colors .yellow,
57
+ child: Padding (
58
+ padding: const EdgeInsets .all (8.0 ),
59
+ child: Text ("开始散步" ),
60
+ ),
61
+ onPressed: () {
62
+ _context.move = Walk ();
63
+ _context.run ();
64
+ },
65
+ ),
66
+ FlatButton (
67
+ color: Colors .blue,
68
+ child: Padding (
69
+ padding: const EdgeInsets .all (8.0 ),
70
+ child: Text ("开始骑行" ),
71
+ ),
72
+ onPressed: () {
73
+ _context.move = ByBike ();
74
+ _context.run ();
75
+ },
76
+ ),
77
+ FlatButton (
78
+ color: Colors .orange,
79
+ child: Padding (
80
+ padding: const EdgeInsets .all (8.0 ),
81
+ child: Text ("开始开车" ),
82
+ ),
83
+ onPressed: () {
84
+ _context.move = ByBus ();
85
+ _context.run ();
86
+ },
87
+ ),
88
+ ],
89
+ ),
90
+ ),
91
+ );
92
+ }
93
+ }
You can’t perform that action at this time.
0 commit comments