File tree Expand file tree Collapse file tree 6 files changed +153
-1
lines changed Expand file tree Collapse file tree 6 files changed +153
-1
lines changed Original file line number Diff line number Diff line change 24
24
15 . 解释器模式
25
25
16 . 迭代器模式
26
26
17 . 中介者模式
27
- 18 . 备忘录模式
27
+ 18 . 备忘录模式
28
+ 19 . 观察者模式
Original file line number Diff line number Diff line change @@ -58,4 +58,6 @@ class StringConst {
58
58
59
59
//备忘录模式
60
60
static const String MEMORY_ = "备忘录模式" ;
61
+ //备忘录模式
62
+ static const String OBSERVER_ = "观察者模式" ;
61
63
}
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import 'package:flutter_design/page/interpreter/intepreter_page.dart';
13
13
import 'package:flutter_design/page/iterator/Iterator_page.dart' ;
14
14
import 'package:flutter_design/page/memory/memory_page.dart' ;
15
15
import 'package:flutter_design/page/midd/mid_page.dart' ;
16
+ import 'package:flutter_design/page/observer/observer_page.dart' ;
16
17
import 'package:flutter_design/page/proxy/proxy_page.dart' ;
17
18
18
19
import 'constant/page_const.dart' ;
@@ -51,6 +52,7 @@ class MyApp extends StatelessWidget {
51
52
PageConst .ITERATOR_PAGE : (context) => IteratorPage (),
52
53
PageConst .MEDIATOR_PAGE : (context) => MidPage (),
53
54
PageConst .MEMENTO_PAGE : (context) => MemoryPage (),
55
+ PageConst .OBSERVER_PAGE : (context) => ObserverPage (),
54
56
},
55
57
);
56
58
}
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ Map<String, String> map = {
33
33
PageConst .ITERATOR_PAGE : StringConst .ITERATOR_ ,
34
34
PageConst .MEDIATOR_PAGE : StringConst .MID_ ,
35
35
PageConst .MEMENTO_PAGE : StringConst .MEMORY_ ,
36
+ PageConst .OBSERVER_PAGE : StringConst .OBSERVER_ ,
36
37
};
37
38
38
39
class _HomePageState extends State <HomePage > {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments