Skip to content

Commit 8d43b7d

Browse files
author
聂彬
committed
feat(): 享元模式的完成
1 parent 437ae15 commit 8d43b7d

File tree

6 files changed

+155
-1
lines changed

6 files changed

+155
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
7. 过滤器/标准模式
1515
8. 组合模式
1616
9. 装饰者模式
17-
10. 外观/门面模式
17+
10. 外观/门面模式
18+
11. 享元模式

lib/constant/string_const.dart

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

3535
//外观模式
3636
static const String FACADE_ = "外观模式";
37+
38+
//享元模式
39+
static const String FLYWEIGHT_ = "享元模式";
3740
}

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter_design/page/combination/combination_page.dart';
66
import 'package:flutter_design/page/decorator/decorator_page.dart';
77
import 'package:flutter_design/page/facade/facade_page.dart';
88
import 'package:flutter_design/page/filter/filter_page.dart';
9+
import 'package:flutter_design/page/flyweight/flyweight_page.dart';
910

1011
import 'constant/page_const.dart';
1112
import 'constant/string_const.dart';
@@ -35,6 +36,7 @@ class MyApp extends StatelessWidget {
3536
PageConst.COMPOSITE_PAGE: (context) => CombinationPage(),
3637
PageConst.DECORATOR_PAGE: (context) => DecoratorPage(),
3738
PageConst.FACADE_PAGE: (context) => FacadePage(),
39+
PageConst.FLYWEIGHT_PAGE: (context) => FlyweightPage(),
3840
},
3941
);
4042
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import 'dart:collection';
2+
3+
import 'package:flutter/material.dart';
4+
5+
/// Created by NieBin on 2020-03-26
6+
/// Github: https://github.com/nb312
7+
/// Email: niebin312@gmail.com
8+
abstract class Clock {
9+
Color color;
10+
11+
Widget display();
12+
}
13+
14+
class PinkClock implements Clock {
15+
@override
16+
Color color = Colors.pink;
17+
18+
@override
19+
Widget display() => Container(
20+
width: 100,
21+
height: 100,
22+
decoration: BoxDecoration(
23+
shape: BoxShape.rectangle,
24+
border: Border.all(color: Colors.green),
25+
color: color,
26+
),
27+
child: Center(child: Text(DateTime.now().toIso8601String())),
28+
);
29+
}
30+
31+
class GreenClock implements Clock {
32+
@override
33+
Color color = Colors.green;
34+
35+
@override
36+
Widget display() => Container(
37+
width: 200,
38+
height: 100,
39+
decoration: BoxDecoration(shape: BoxShape.circle, color: color),
40+
child: Center(child: Text(DateTime.now().toIso8601String())),
41+
);
42+
}
43+
44+
class BlueClock implements Clock {
45+
@override
46+
Color color = Colors.blue;
47+
48+
@override
49+
Widget display() => Container(
50+
width: 150,
51+
height: 150,
52+
decoration: BoxDecoration(shape: BoxShape.circle, color: color),
53+
child: Center(child: Text(DateTime.now().toIso8601String())),
54+
);
55+
}
56+
57+
class WhiteClock implements Clock {
58+
@override
59+
Color color = Colors.white;
60+
61+
@override
62+
Widget display() => Container(
63+
width: 100,
64+
height: 100,
65+
decoration: BoxDecoration(
66+
shape: BoxShape.rectangle,
67+
color: color,
68+
border: Border.all(color: Colors.orangeAccent)),
69+
child: Center(child: Text(DateTime.now().toIso8601String())),
70+
);
71+
}
72+
73+
class ClockManager {
74+
static Map<String, Clock> _clocks = HashMap();
75+
76+
static Clock getColorClock(String color) {
77+
if (_clocks.containsKey(color)) {
78+
return _clocks[color];
79+
} else {
80+
_clocks.putIfAbsent(color, () {
81+
if (color.toLowerCase() == "pink") {
82+
return PinkClock();
83+
} else if (color.toLowerCase() == "green") {
84+
return GreenClock();
85+
} else if (color.toLowerCase() == "blue") {
86+
return BlueClock();
87+
} else {
88+
return WhiteClock();
89+
}
90+
});
91+
return _clocks[color];
92+
}
93+
}
94+
95+
static Clock removeClock(String color) {
96+
if (_clocks.containsKey(color)) {
97+
return _clocks.remove(color);
98+
} else {
99+
return null;
100+
}
101+
}
102+
103+
static void clearAll() {
104+
_clocks.clear();
105+
}
106+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_design/constant/string_const.dart';
3+
import 'package:flutter_design/page/flyweight/flyweight_mode.dart';
4+
5+
/// Created by NieBin on 2020-03-26
6+
/// Github: https://github.com/nb312
7+
/// Email: niebin312@gmail.com
8+
9+
class FlyweightPage extends StatefulWidget {
10+
@override
11+
_FlyweightPageState createState() => _FlyweightPageState();
12+
}
13+
14+
class _FlyweightPageState extends State<FlyweightPage> {
15+
@override
16+
Widget build(BuildContext context) {
17+
return Scaffold(
18+
appBar: AppBar(
19+
title: Text(StringConst.FLYWEIGHT_),
20+
),
21+
body: Container(
22+
child: Center(
23+
child: Column(
24+
mainAxisAlignment: MainAxisAlignment.center,
25+
crossAxisAlignment: CrossAxisAlignment.center,
26+
children: <Widget>[
27+
ClockManager.getColorClock("pink").display(),
28+
SizedBox(height: 10,),
29+
ClockManager.getColorClock("green").display(),
30+
SizedBox(height: 10,),
31+
ClockManager.getColorClock("blue").display(),
32+
SizedBox(height: 10,),
33+
ClockManager.getColorClock("12423").display(),
34+
SizedBox(height: 10,)
35+
],
36+
),
37+
),
38+
),
39+
);
40+
}
41+
}

lib/page/home_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Map<String, String> map = {
2525
PageConst.COMPOSITE_PAGE: StringConst.COMBINATION_,
2626
PageConst.DECORATOR_PAGE: StringConst.DECORATOR_,
2727
PageConst.FACADE_PAGE: StringConst.FACADE_,
28+
PageConst.FLYWEIGHT_PAGE: StringConst.FLYWEIGHT_,
2829
};
2930

3031
class _HomePageState extends State<HomePage> {

0 commit comments

Comments
 (0)