Skip to content

Commit 48b2b48

Browse files
author
聂彬
committed
feat(): 策略模式完成
1 parent 506d5e6 commit 48b2b48

File tree

6 files changed

+156
-3
lines changed

6 files changed

+156
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
17. 中介者模式
2727
18. 备忘录模式
2828
19. 观察者模式
29-
20. 状态模式
29+
20. 状态模式
30+
21. 策略模式

lib/constant/string_const.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ class StringConst {
5959
//备忘录模式
6060
static const String MEMORY_ = "备忘录模式";
6161

62-
//备忘录模式
62+
//观察者模式
6363
static const String OBSERVER_ = "观察者模式";
6464

65-
//备忘录模式
65+
//状态模式
6666
static const String STATE_ = "状态模式";
67+
68+
//策略模式
69+
static const String STRATEGY_ = "策略模式";
6770
}

lib/main.dart

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:flutter_design/page/midd/mid_page.dart';
1616
import 'package:flutter_design/page/observer/observer_page.dart';
1717
import 'package:flutter_design/page/proxy/proxy_page.dart';
1818
import 'package:flutter_design/page/state/state_page.dart';
19+
import 'package:flutter_design/page/strategy/strategy_page.dart';
1920

2021
import 'constant/page_const.dart';
2122
import 'constant/string_const.dart';
@@ -55,6 +56,7 @@ class MyApp extends StatelessWidget {
5556
PageConst.MEMENTO_PAGE: (context) => MemoryPage(),
5657
PageConst.OBSERVER_PAGE: (context) => ObserverPage(),
5758
PageConst.STATE_PAGE: (context) => StatePage(),
59+
PageConst.STRATEGY_PAGE: (context) => StrategyPage(),
5860
},
5961
);
6062
}

lib/page/home_page.dart

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Map<String, String> map = {
3535
PageConst.MEMENTO_PAGE: StringConst.MEMORY_,
3636
PageConst.OBSERVER_PAGE: StringConst.OBSERVER_,
3737
PageConst.STATE_PAGE: StringConst.STATE_,
38+
PageConst.STRATEGY_PAGE: StringConst.STRATEGY_,
3839
};
3940

4041
class _HomePageState extends State<HomePage> {

lib/page/strategy/strategy_mode.dart

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

lib/page/strategy/strategy_page.dart

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

0 commit comments

Comments
 (0)