Skip to content

Commit b347089

Browse files
authored
Merge pull request #17 from FlutterOpen/dev
feat(): 完成责任链模式
2 parents f99a156 + c9e2c22 commit b347089

7 files changed

Lines changed: 155 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
12. 代理模式
2020

2121
### 行为型模式
22-
待添加
22+
13. 责任链模式

lib/constant/page_const.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class PageConst {
1818
static const String CHAIN_PAGE = "/chain"; //享元模式
1919
static const String COMMAND_PAGE = "/command"; //命令模式
2020
static const String INTERPRETER_PAGE = "/interpreter"; //解释器模式
21-
static const String ITERATOR_PAGE = "/interpreter"; //解释器模式
21+
static const String DUTY_PAGE = "/duty"; //责任链模式
2222
static const String MEDIATOR_PAGE = "/mediator"; //中介者模式
2323
static const String MEMENTO_PAGE = "/memento"; //备忘录模式
2424
static const String OBSERVER_PAGE = "/observer"; //观察者模式

lib/constant/string_const.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ class StringConst {
4040

4141
//享元模式
4242
static const String PROXY_ = "代理模式";
43+
44+
//享元模式
45+
static const String DUTY_ = "责任链模式";
4346
}

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter_design/page/adapter/adapter_page.dart';
44
import 'package:flutter_design/page/bridge/bridge_page.dart';
55
import 'package:flutter_design/page/combination/combination_page.dart';
66
import 'package:flutter_design/page/decorator/decorator_page.dart';
7+
import 'package:flutter_design/page/duty/duty_page.dart';
78
import 'package:flutter_design/page/facade/facade_page.dart';
89
import 'package:flutter_design/page/filter/filter_page.dart';
910
import 'package:flutter_design/page/flyweight/flyweight_page.dart';
@@ -39,6 +40,7 @@ class MyApp extends StatelessWidget {
3940
PageConst.FACADE_PAGE: (context) => FacadePage(),
4041
PageConst.FLYWEIGHT_PAGE: (context) => FlyweightPage(),
4142
PageConst.PROXY_PAGE: (context) => ProxyPage(),
43+
PageConst.DUTY_PAGE: (context) => DutyPage(),
4244
},
4345
);
4446
}

lib/page/duty/duty_mode.dart

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/// Created by NieBin on 2020-03-30
2+
/// Github: https://github.com/nb312
3+
/// Email: niebin312@gmail.com
4+
class MessageModel {
5+
final String message;
6+
final int level;
7+
8+
MessageModel(this.message, this.level);
9+
}
10+
11+
abstract class IHandler {
12+
static const int LEVEL_ONE = 1;
13+
static const int LEVEL_TWO = 2;
14+
static const int LEVEL_THREE = 3;
15+
int level = LEVEL_ONE;
16+
IHandler _nextHandler;
17+
18+
IHandler(this.level);
19+
20+
set nextHandler(IHandler handler) => _nextHandler = handler;
21+
22+
void sendMessage(MessageModel model);
23+
24+
void hand(MessageModel model) {
25+
if (model.level == level) {
26+
this.sendMessage(model);
27+
} else {
28+
if (_nextHandler != null) {
29+
print("正在发送到下一级");
30+
_nextHandler.hand(model);
31+
} else {
32+
//实在找不到人,先改下
33+
print("已经到达顶级了");
34+
this.sendMessage(model);
35+
}
36+
}
37+
}
38+
}
39+
40+
class EarthHandler extends IHandler {
41+
EarthHandler() : super(IHandler.LEVEL_ONE);
42+
43+
@override
44+
void sendMessage(MessageModel model) {
45+
print("我是地球,我已经收到: ${model.message}");
46+
}
47+
}
48+
49+
class MoonHandler extends IHandler {
50+
MoonHandler() : super(IHandler.LEVEL_TWO);
51+
52+
@override
53+
void sendMessage(MessageModel model) {
54+
print("我是月亮,我已经收到: ${model.message}");
55+
}
56+
}
57+
58+
class SunHandler extends IHandler {
59+
SunHandler() : super(IHandler.LEVEL_THREE);
60+
61+
@override
62+
void sendMessage(MessageModel model) {
63+
print("我是太阳,我已经收到: ${model.message}");
64+
}
65+
}

lib/page/duty/duty_page.dart

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_design/constant/string_const.dart';
4+
import 'package:flutter_design/page/duty/duty_mode.dart';
5+
6+
/// Created by NieBin on 2020-03-30
7+
/// Github: https://github.com/nb312
8+
/// Email: niebin312@gmail.com
9+
10+
class DutyPage extends StatefulWidget {
11+
@override
12+
_DutyPageState createState() => _DutyPageState();
13+
}
14+
15+
class _DutyPageState extends State<DutyPage> {
16+
IHandler _handler;
17+
18+
@override
19+
Widget build(BuildContext context) {
20+
return Scaffold(
21+
appBar: AppBar(
22+
title: Text(StringConst.DUTY_),
23+
),
24+
body: Container(
25+
constraints: BoxConstraints.expand(),
26+
child: Column(
27+
crossAxisAlignment: CrossAxisAlignment.center,
28+
mainAxisAlignment: MainAxisAlignment.center,
29+
children: <Widget>[
30+
FlatButton(
31+
onPressed: () {
32+
if (_handler == null) {
33+
_handler = EarthHandler();
34+
IHandler moon = MoonHandler();
35+
36+
IHandler sun = SunHandler();
37+
moon.nextHandler = sun;
38+
_handler.nextHandler = moon;
39+
print("初始化成功");
40+
} else {
41+
print("你已经初始化");
42+
}
43+
},
44+
color: Colors.yellow,
45+
child: Padding(
46+
padding: const EdgeInsets.all(10.0),
47+
child: Text("初始化"),
48+
),
49+
),
50+
SizedBox(height: 10),
51+
FlatButton(
52+
onPressed: () {
53+
MessageModel model = MessageModel("我爱地球", IHandler.LEVEL_ONE);
54+
_handler.hand(model);
55+
},
56+
color: Colors.green,
57+
child: Text("发送信息给地球"),
58+
),
59+
SizedBox(height: 10),
60+
FlatButton(
61+
onPressed: () {
62+
MessageModel model = MessageModel("我爱月球", IHandler.LEVEL_TWO);
63+
_handler.hand(model);
64+
},
65+
color: Colors.blueGrey,
66+
child: Text("发送信息给月亮"),
67+
),
68+
SizedBox(height: 10),
69+
FlatButton(
70+
onPressed: () {
71+
MessageModel model =
72+
MessageModel("我爱太阳", IHandler.LEVEL_THREE);
73+
_handler.hand(model);
74+
},
75+
color: Colors.red,
76+
child: Text("发送信息给太阳"),
77+
),
78+
],
79+
)),
80+
);
81+
}
82+
}

lib/page/home_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Map<String, String> map = {
2727
PageConst.FACADE_PAGE: StringConst.FACADE_,
2828
PageConst.FLYWEIGHT_PAGE: StringConst.FLYWEIGHT_,
2929
PageConst.PROXY_PAGE: StringConst.PROXY_,
30+
PageConst.DUTY_PAGE: StringConst.DUTY_,
3031
};
3132

3233
class _HomePageState extends State<HomePage> {

0 commit comments

Comments
 (0)