File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 252516 . 迭代器模式
262617 . 中介者模式
272718 . 备忘录模式
28- 19 . 观察者模式
28+ 19 . 观察者模式
29+ 20 . 状态模式
Original file line number Diff line number Diff line change @@ -58,6 +58,10 @@ class StringConst {
5858
5959 //备忘录模式
6060 static const String MEMORY_ = "备忘录模式" ;
61+
6162 //备忘录模式
6263 static const String OBSERVER_ = "观察者模式" ;
64+
65+ //备忘录模式
66+ static const String STATE_ = "状态模式" ;
6367}
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import 'package:flutter_design/page/memory/memory_page.dart';
1515import 'package:flutter_design/page/midd/mid_page.dart' ;
1616import 'package:flutter_design/page/observer/observer_page.dart' ;
1717import 'package:flutter_design/page/proxy/proxy_page.dart' ;
18+ import 'package:flutter_design/page/state/state_page.dart' ;
1819
1920import 'constant/page_const.dart' ;
2021import 'constant/string_const.dart' ;
@@ -53,6 +54,7 @@ class MyApp extends StatelessWidget {
5354 PageConst .MEDIATOR_PAGE : (context) => MidPage (),
5455 PageConst .MEMENTO_PAGE : (context) => MemoryPage (),
5556 PageConst .OBSERVER_PAGE : (context) => ObserverPage (),
57+ PageConst .STATE_PAGE : (context) => StatePage (),
5658 },
5759 );
5860 }
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ Map<String, String> map = {
3434 PageConst .MEDIATOR_PAGE : StringConst .MID_ ,
3535 PageConst .MEMENTO_PAGE : StringConst .MEMORY_ ,
3636 PageConst .OBSERVER_PAGE : StringConst .OBSERVER_ ,
37+ PageConst .STATE_PAGE : StringConst .STATE_ ,
3738};
3839
3940class _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-14
4+ /// Github: https://github.com/nb312
5+ /// Email: niebin312@gmail.com
6+ ///参考文章https://www.runoob.com/w3cnote/state-vs-strategy.html
7+ abstract class State {
8+ void eatWater ();
9+
10+ void watchSea ();
11+ }
12+
13+ class FlyState extends State {
14+ @override
15+ void eatWater () {
16+ print ("高空跳伞太危险不能喝水" );
17+ }
18+
19+ @override
20+ void watchSea () {
21+ print ("飞得高看得远,整个大海都尽收眼底" );
22+ }
23+ }
24+
25+ class SubWayState extends State {
26+ @override
27+ void eatWater () {
28+ print ("拿起你的矿泉水瓶开始喝水" );
29+ }
30+
31+ @override
32+ void watchSea () {
33+ print ("拿起手机开始看大海的视频" );
34+ }
35+ }
36+
37+ class HomeState extends State {
38+ @override
39+ void eatWater () {
40+ print ("用杯子倒一杯水喝了起来" );
41+ }
42+
43+ @override
44+ void watchSea () {
45+ print ("打卡电视机,调到能看大海的频道" );
46+ }
47+ }
48+
49+ //如果程序员要执行这些状态会怎么样呢
50+ class Programmer extends State {
51+ FlyState _flyState = FlyState ();
52+ SubWayState _subWayState = SubWayState ();
53+ HomeState _homeState = HomeState ();
54+
55+ State get fly => _flyState;
56+
57+ State get subway => _subWayState;
58+
59+ State get homeState => _homeState;
60+ State state;
61+
62+ Programmer () {
63+ state = homeState;
64+ }
65+
66+ @override
67+ void eatWater () {
68+ state? .eatWater ();
69+ }
70+
71+ @override
72+ void watchSea () {
73+ state.watchSea ();
74+ }
75+ }
Original file line number Diff line number Diff line change 1+ import 'package:flutter/material.dart' ;
2+ import 'package:flutter_design/page/state/state_mode.dart' as s;
3+
4+ /// Created by NieBin on 2020-04-14
5+ /// Github: https://github.com/nb312
6+ /// Email: niebin312@gmail.com
7+
8+ class StatePage extends StatefulWidget {
9+ @override
10+ _StatePageState createState () => _StatePageState ();
11+ }
12+
13+ class _StatePageState extends State <StatePage > {
14+ s.Programmer _programmer;
15+
16+ @override
17+ void initState () {
18+ _programmer = s.Programmer ();
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+ child: Column (
30+ children: < Widget > [
31+ FlatButton (
32+ color: Colors .red,
33+ child: Text ("跳伞飞起来" ),
34+ onPressed: () {
35+ _programmer.state = s.FlyState ();
36+ },
37+ ),
38+ FlatButton (
39+ color: Colors .yellow,
40+ child: Text ("坐地铁" ),
41+ onPressed: () {
42+ _programmer.state = s.SubWayState ();
43+ },
44+ ),
45+ FlatButton (
46+ color: Colors .orange,
47+ child: Text ("在家里" ),
48+ onPressed: () {
49+ _programmer.state = s.HomeState ();
50+ },
51+ ),
52+ FlatButton (
53+ color: Colors .green,
54+ child: Text ("喝水" ),
55+ onPressed: () {
56+ _programmer.eatWater ();
57+ },
58+ ),
59+ FlatButton (
60+ color: Colors .pink,
61+ child: Text ("看海" ),
62+ onPressed: () {
63+ _programmer.watchSea ();
64+ },
65+ )
66+ ],
67+ ),
68+ ),
69+ );
70+ }
71+ }
You can’t perform that action at this time.
0 commit comments