-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
159 lines (133 loc) · 4.63 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import 'video_model.dart';
int lineNum = 0;
abstract class ThirdPartyYouTubeLib {
Map<String, Video> popularVideos();
Video getVideo(String videoId);
}
class ThirdPartyYouTubeClass implements ThirdPartyYouTubeLib {
@override
Map<String, Video> popularVideos() {
_connectToServer("http://www.youtube.com");
return _getPopularVideos();
}
@override
Video getVideo(String videoId) {
_connectToServer("http://www.youtube.com/$videoId");
return _getSomeVideo(videoId);
}
// -----------------------------------------------------------------------
// Fake methods to simulate network activity.
// They as slow as a real life.
void _experienceNetworkLatency() {
// int randomLatency = 5 + (Random().nextInt(10) * 6);
int randomLatency = 10;
for (int i = 0; i < randomLatency; i++) {
try {
Future.delayed(Duration(milliseconds: 100));
} catch (ex) {
print(ex);
}
}
}
void _connectToServer(String server) {
print("${++lineNum}: Connecting to $server ... ");
_experienceNetworkLatency();
print("${++lineNum}: Connected! ");
}
Map<String, Video> _getPopularVideos() {
print("${++lineNum}: Downloading popular Videos... ");
_experienceNetworkLatency();
_experienceNetworkLatency();
Map<String, Video> map = Map<String, Video>();
map["catzzzzzzzzz"] = Video(id: "sadgahasgdas", title: "Catzzzz.avi");
map["dancesvideoo"] = Video(id: "asdfas3ffasd", title: "Dancing video.mpq");
print("${++lineNum}: Done!");
return map;
}
Video _getSomeVideo(String videoId) {
print("${++lineNum}: Downloading video... ");
_experienceNetworkLatency();
Video video = Video(id: videoId, title: "Some video title");
print("${++lineNum}: Done!");
return video;
}
}
class YouTubeCacheProxy implements ThirdPartyYouTubeLib {
ThirdPartyYouTubeLib _youtubeService = ThirdPartyYouTubeClass();
Map<String, Video> _cachePopular = Map<String, Video>();
Map<String, Video> _cacheAll = Map<String, Video>();
YouTubeCacheProxy();
@override
Map<String, Video> popularVideos() {
if (_cachePopular.isEmpty) {
_cachePopular = _youtubeService.popularVideos();
} else {
print("${++lineNum}: Retrieved list from cache.");
}
return _cachePopular;
}
@override
Video getVideo(String videoId) {
Video? video = _cacheAll[videoId];
if (video == null) {
video = _youtubeService.getVideo(videoId);
_cacheAll[videoId] = video;
} else {
print("${++lineNum}: Retrieved video ' $videoId ' from cache.");
}
return video;
}
void reset() {
_cachePopular.clear();
_cacheAll.clear();
}
}
class YouTubeDownloader {
ThirdPartyYouTubeLib _api;
YouTubeDownloader(ThirdPartyYouTubeLib api) : _api = api;
void renderVideoPage(String videoId) {
Video video = _api.getVideo(videoId);
print("${++lineNum}: -------------------------------");
print("${++lineNum}: Video page (imagine fancy HTML)");
print(
"${++lineNum}: ID: ${video.id} && Title: ${video.title} && Video: ${video.data}");
print("${++lineNum}: -------------------------------");
}
void renderPopularVideos() {
Map<String, Video> map = _api.popularVideos();
print("${++lineNum}: -------------------------------");
print("${++lineNum}: Most popular videos on YouTube (imagine fancy HTML)");
for (Video video in map.values) {
print("${++lineNum}: ID: ${video.id} / Title: ${video.title}");
}
print("${++lineNum}: -------------------------------");
}
}
void main() {
// without proxy
YouTubeDownloader naiveDownloader =
YouTubeDownloader(ThirdPartyYouTubeClass());
// with proxy
YouTubeDownloader smartDownloader = YouTubeDownloader(YouTubeCacheProxy());
int naive = test(naiveDownloader);
print(
"${++lineNum}: -------------------------Proxy Added-------------------------");
int smart = test(smartDownloader);
print("${++lineNum}: Time elapsed without proxy = naive: $naive ms");
print("${++lineNum}: Time elapsed with proxy = smart: $smart ms");
print(
"${++lineNum}: Time saved by caching proxy: ($naive - $smart= ${naive - smart}) ms");
}
int test(YouTubeDownloader downloader) {
int startTime = DateTime.now().millisecond;
// User behavior in our app:
downloader.renderPopularVideos();
downloader.renderVideoPage("catzzzzzzzzz");
downloader.renderPopularVideos();
downloader.renderVideoPage("dancesvideoo");
// Users might visit the same page quite often.
downloader.renderVideoPage("catzzzzzzzzz");
downloader.renderVideoPage("someothervid");
int estimatedTime = DateTime.now().millisecond - startTime;
return estimatedTime;
}