|
| 1 | +# 3rd party youtube example |
| 2 | + |
| 3 | + |
| 4 | +## source: |
| 5 | +> https://refactoring.guru/design-patterns/proxy/java/example |
| 6 | + |
| 7 | + |
| 8 | +## What is Proxy? |
| 9 | + > - Proxy is a structural design pattern |
| 10 | + > - that provides an object |
| 11 | + > - that acts as a substitute for a real service object used by a client. |
| 12 | + > - A proxy receives client requests, |
| 13 | + > - does some work (access control, caching, etc.) |
| 14 | + > - and then passes the request to a service object. |
| 15 | +
|
| 16 | +## The proxy object |
| 17 | +- has (the same interface as a service), |
| 18 | +- which makes it interchangeable with a real object when passed to a client. |
| 19 | + |
| 20 | +## Usage examples: |
| 21 | +- It’s irreplaceable |
| 22 | +- when you want to add some `additional behaviors` to an object of some existing class |
| 23 | +- without changing the client code. |
| 24 | + |
| 25 | +## Identification: |
| 26 | +- Proxies delegate all of the real work to some other object. |
| 27 | +- Each proxy method should, in the end, refer to a service object |
| 28 | +- unless the proxy is a subclass of a service. |
| 29 | + |
| 30 | +## Caching proxy |
| 31 | +In this example, |
| 32 | +1. the Proxy pattern helps to implement the lazy initialization |
| 33 | +2. and caching to an inefficient 3rd-party YouTube integration library. |
| 34 | + |
| 35 | +## Proxy is invaluable |
| 36 | +> when you have to add some additional behaviors to a class which code you can’t change. |
| 37 | +
|
| 38 | +## UML |
| 39 | +<p align="center"> |
| 40 | + <img style="background-color:#5547" src = "./3rd_party_youtube_example.png" > |
| 41 | +</p> |
| 42 | + |
| 43 | +## Code |
| 44 | + |
| 45 | +### Service Interface || Subject |
| 46 | +```dart |
| 47 | +int lineNum = 0; |
| 48 | +
|
| 49 | +abstract class ThirdPartyYouTubeLib { |
| 50 | + Map<String, Video> popularVideos(); |
| 51 | + Video getVideo(String videoId); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### RealSubject == Service == ThirdPartyYouTubeClass |
| 56 | + |
| 57 | +```dart |
| 58 | +
|
| 59 | +class ThirdPartyYouTubeClass implements ThirdPartyYouTubeLib { |
| 60 | + @override |
| 61 | + Map<String, Video> popularVideos() { |
| 62 | + _connectToServer("http://www.youtube.com"); |
| 63 | + return _getPopularVideos(); |
| 64 | + } |
| 65 | +
|
| 66 | + @override |
| 67 | + Video getVideo(String videoId) { |
| 68 | + _connectToServer("http://www.youtube.com/$videoId"); |
| 69 | + return _getSomeVideo(videoId); |
| 70 | + } |
| 71 | +
|
| 72 | + // -------------------------------------- |
| 73 | + // Fake methods to simulate network activity. |
| 74 | + // They as slow as a real life. |
| 75 | +
|
| 76 | + void _experienceNetworkLatency() { |
| 77 | + // int randomLatency = 5 + (Random().nextInt(10) * 6); |
| 78 | + int randomLatency = 10; |
| 79 | + for (int i = 0; i < randomLatency; i++) { |
| 80 | + try { |
| 81 | + Future.delayed(Duration(milliseconds: 100)); |
| 82 | + } catch (ex) { |
| 83 | + print(ex); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +
|
| 88 | + void _connectToServer(String server) { |
| 89 | + print("${++lineNum}: Connecting to $server ... "); |
| 90 | + _experienceNetworkLatency(); |
| 91 | + print("${++lineNum}: Connected! "); |
| 92 | + } |
| 93 | +
|
| 94 | + Map<String, Video> _getPopularVideos() { |
| 95 | + print("${++lineNum}: Downloading popular Videos... "); |
| 96 | + _experienceNetworkLatency(); |
| 97 | + _experienceNetworkLatency(); |
| 98 | + Map<String, Video> map = Map<String, Video>(); |
| 99 | + map["catzzzzzzzzz"] = Video(id: "sadgahasgdas", title: "Catzzzz.avi"); |
| 100 | + map["dancesvideoo"] = Video(id: "asdfas3ffasd", title: "Dancing video.mpq"); |
| 101 | + print("${++lineNum}: Done!"); |
| 102 | + return map; |
| 103 | + } |
| 104 | +
|
| 105 | + Video _getSomeVideo(String videoId) { |
| 106 | + print("${++lineNum}: Downloading video... "); |
| 107 | +
|
| 108 | + _experienceNetworkLatency(); |
| 109 | + Video video = Video(id: videoId, title: "Some video title"); |
| 110 | +
|
| 111 | + print("${++lineNum}: Done!"); |
| 112 | + return video; |
| 113 | + } |
| 114 | +} |
| 115 | +
|
| 116 | +class Video { |
| 117 | + String id; |
| 118 | + String title; |
| 119 | + String data; |
| 120 | +
|
| 121 | + Video({required this.id, required this.title, this.data = "Random video."}); |
| 122 | +} |
| 123 | +``` |
| 124 | +### Proxy Class |
| 125 | + |
| 126 | +```dart |
| 127 | +class YouTubeCacheProxy implements ThirdPartyYouTubeLib { |
| 128 | + ThirdPartyYouTubeLib _youtubeService; |
| 129 | + Map<String, Video> _cachePopular = Map<String, Video>(); |
| 130 | + Map<String, Video> _cacheAll = Map<String, Video>(); |
| 131 | +
|
| 132 | + YouTubeCacheProxy() : _youtubeService = ThirdPartyYouTubeClass(); |
| 133 | +
|
| 134 | + @override |
| 135 | + Map<String, Video> popularVideos() { |
| 136 | + if (_cachePopular.isEmpty) { |
| 137 | + _cachePopular = _youtubeService.popularVideos(); |
| 138 | + } else { |
| 139 | + print("${++lineNum}: Retrieved list from cache."); |
| 140 | + } |
| 141 | + return _cachePopular; |
| 142 | + } |
| 143 | +
|
| 144 | + @override |
| 145 | + Video getVideo(String videoId) { |
| 146 | + Video? video = _cacheAll[videoId]; |
| 147 | + if (video == null) { |
| 148 | + video = _youtubeService.getVideo(videoId); |
| 149 | + _cacheAll[videoId] = video; |
| 150 | + } else { |
| 151 | + print("${++lineNum}: Retrieved video ' $videoId ' from cache."); |
| 152 | + } |
| 153 | + return video; |
| 154 | + } |
| 155 | +
|
| 156 | + void reset() { |
| 157 | + _cachePopular.clear(); |
| 158 | + _cacheAll.clear(); |
| 159 | + } |
| 160 | +} |
| 161 | +
|
| 162 | +class YouTubeDownloader { |
| 163 | + ThirdPartyYouTubeLib _api; |
| 164 | +
|
| 165 | + YouTubeDownloader(ThirdPartyYouTubeLib api) : _api = api; |
| 166 | +
|
| 167 | + void renderVideoPage(String videoId) { |
| 168 | + Video video = _api.getVideo(videoId); |
| 169 | + print("${++lineNum}: -------------------------------"); |
| 170 | + print("${++lineNum}: Video page (imagine fancy HTML)"); |
| 171 | + print( |
| 172 | + "${++lineNum}: ID: ${video.id} && Title: ${video.title} && Video: ${video.data}"); |
| 173 | + print("${++lineNum}: -------------------------------"); |
| 174 | + } |
| 175 | +
|
| 176 | + void renderPopularVideos() { |
| 177 | + Map<String, Video> map = _api.popularVideos(); |
| 178 | + print("${++lineNum}: -------------------------------"); |
| 179 | + print("${++lineNum}: Most popular videos on YouTube (imagine fancy HTML)"); |
| 180 | +
|
| 181 | + for (Video video in map.values) { |
| 182 | + print("${++lineNum}: ID: ${video.id} / Title: ${video.title}"); |
| 183 | + } |
| 184 | + print("${++lineNum}: -------------------------------"); |
| 185 | + } |
| 186 | +} |
| 187 | +
|
| 188 | +
|
| 189 | +``` |
| 190 | +### Testing proxy main() |
| 191 | +```dart |
| 192 | +
|
| 193 | +void main() { |
| 194 | + // without proxy |
| 195 | + YouTubeDownloader naiveDownloader = |
| 196 | + YouTubeDownloader(ThirdPartyYouTubeClass()); |
| 197 | + // with proxy |
| 198 | + YouTubeDownloader smartDownloader = YouTubeDownloader(YouTubeCacheProxy()); |
| 199 | +
|
| 200 | + int naive = test(naiveDownloader); |
| 201 | + print( |
| 202 | + "${++lineNum}: -------------------------Proxy Added-------------------------"); |
| 203 | + int smart = test(smartDownloader); |
| 204 | + print("${++lineNum}: Time elapsed without proxy = naive: $naive ms"); |
| 205 | + print("${++lineNum}: Time elapsed with proxy = smart: $smart ms"); |
| 206 | + print( |
| 207 | + "${++lineNum}: Time saved by caching proxy: ($naive - $smart= ${naive - smart}) ms"); |
| 208 | +} |
| 209 | +
|
| 210 | +int test(YouTubeDownloader downloader) { |
| 211 | + int startTime = DateTime.now().millisecond; |
| 212 | +
|
| 213 | + // User behavior in our app: |
| 214 | + downloader.renderPopularVideos(); |
| 215 | + downloader.renderVideoPage("catzzzzzzzzz"); |
| 216 | + downloader.renderPopularVideos(); |
| 217 | + downloader.renderVideoPage("dancesvideoo"); |
| 218 | + // Users might visit the same page quite often. |
| 219 | + downloader.renderVideoPage("catzzzzzzzzz"); |
| 220 | + downloader.renderVideoPage("someothervid"); |
| 221 | +
|
| 222 | + int estimatedTime = DateTime.now().millisecond - startTime; |
| 223 | + return estimatedTime; |
| 224 | +} |
| 225 | +
|
| 226 | +``` |
0 commit comments