Skip to content

Commit 50cb34c

Browse files
author
聂彬
committed
feat(): 观察者模式完成
1 parent 4e35e7f commit 50cb34c

File tree

6 files changed

+153
-1
lines changed

6 files changed

+153
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
15. 解释器模式
2525
16. 迭代器模式
2626
17. 中介者模式
27-
18. 备忘录模式
27+
18. 备忘录模式
28+
19. 观察者模式

lib/constant/string_const.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ class StringConst {
5858

5959
//备忘录模式
6060
static const String MEMORY_ = "备忘录模式";
61+
//备忘录模式
62+
static const String OBSERVER_ = "观察者模式";
6163
}

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:flutter_design/page/interpreter/intepreter_page.dart';
1313
import 'package:flutter_design/page/iterator/Iterator_page.dart';
1414
import 'package:flutter_design/page/memory/memory_page.dart';
1515
import 'package:flutter_design/page/midd/mid_page.dart';
16+
import 'package:flutter_design/page/observer/observer_page.dart';
1617
import 'package:flutter_design/page/proxy/proxy_page.dart';
1718

1819
import 'constant/page_const.dart';
@@ -51,6 +52,7 @@ class MyApp extends StatelessWidget {
5152
PageConst.ITERATOR_PAGE: (context) => IteratorPage(),
5253
PageConst.MEDIATOR_PAGE: (context) => MidPage(),
5354
PageConst.MEMENTO_PAGE: (context) => MemoryPage(),
55+
PageConst.OBSERVER_PAGE: (context) => ObserverPage(),
5456
},
5557
);
5658
}

lib/page/home_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Map<String, String> map = {
3333
PageConst.ITERATOR_PAGE: StringConst.ITERATOR_,
3434
PageConst.MEDIATOR_PAGE: StringConst.MID_,
3535
PageConst.MEMENTO_PAGE: StringConst.MEMORY_,
36+
PageConst.OBSERVER_PAGE: StringConst.OBSERVER_,
3637
};
3738

3839
class _HomePageState extends State<HomePage> {

lib/page/observer/observer_mode.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/// Created by NieBin on 2020-04-13
2+
/// Github: https://github.com/nb312
3+
/// Email: niebin312@gmail.com
4+
abstract class ISubject {
5+
void attach(IObserver observer);
6+
7+
void detach(IObserver observer);
8+
9+
void detachAll();
10+
11+
void notify();
12+
}
13+
14+
class RealSubject extends ISubject {
15+
List<IObserver> _list;
16+
String _name;
17+
18+
String get name => _name;
19+
20+
set name(String n) {
21+
_name = n;
22+
notify();
23+
}
24+
25+
RealSubject() {
26+
_list = List();
27+
}
28+
29+
@override
30+
void attach(IObserver observer) {
31+
if (observer == null) return;
32+
if (!_list.contains(observer)) {
33+
_list.add(observer);
34+
}
35+
}
36+
37+
@override
38+
void detach(IObserver observer) {
39+
if (_list.contains(observer)) {
40+
_list.remove(observer);
41+
}
42+
}
43+
44+
@override
45+
void detachAll() {
46+
_list.clear();
47+
}
48+
49+
@override
50+
void notify() {
51+
for (IObserver observer in _list) {
52+
observer.update(name);
53+
}
54+
}
55+
}
56+
57+
abstract class IObserver {
58+
void update(String name);
59+
}
60+
61+
class AObserver extends IObserver {
62+
String content;
63+
64+
@override
65+
void update(String name) {
66+
print("我是A,收到外星人: $name");
67+
}
68+
}
69+
70+
class BObserver extends IObserver {
71+
@override
72+
void update(String name) {
73+
print("我是B,收到一个东西: $name");
74+
}
75+
}

lib/page/observer/observer_page.dart

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_design/page/observer/observer_mode.dart';
3+
4+
/// Created by NieBin on 2020-04-13
5+
/// Github: https://github.com/nb312
6+
/// Email: niebin312@gmail.com
7+
8+
class ObserverPage extends StatefulWidget {
9+
@override
10+
_ObserverPageState createState() => _ObserverPageState();
11+
}
12+
13+
class _ObserverPageState extends State<ObserverPage> {
14+
List<IObserver> _observers;
15+
RealSubject _subject;
16+
17+
@override
18+
void initState() {
19+
_observers = List();
20+
_subject = RealSubject();
21+
super.initState();
22+
}
23+
24+
@override
25+
Widget build(BuildContext context) {
26+
return Scaffold(
27+
appBar: AppBar(
28+
title: Text("观察者模式"),
29+
),
30+
body: Container(
31+
child: Column(
32+
children: <Widget>[
33+
FlatButton(
34+
color: Colors.orange,
35+
child: Padding(
36+
padding: const EdgeInsets.all(8.0),
37+
child: Text("添加A型观察者"),
38+
),
39+
onPressed: () {
40+
_subject.attach(AObserver());
41+
},
42+
),
43+
SizedBox(height: 10),
44+
FlatButton(
45+
color: Colors.orange,
46+
child: Padding(
47+
padding: const EdgeInsets.all(8.0),
48+
child: Text("添加B型观察者"),
49+
),
50+
onPressed: () {
51+
_subject.attach(BObserver());
52+
},
53+
),
54+
SizedBox(height: 10),
55+
FlatButton(
56+
color: Colors.orange,
57+
child: Padding(
58+
padding: const EdgeInsets.all(8.0),
59+
child: Text("发个消息通知大家"),
60+
),
61+
onPressed: () {
62+
_subject.name = "诸国缸体";
63+
_subject.notify();
64+
},
65+
),
66+
],
67+
),
68+
),
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)