Esempio n. 1
0
        public void DoStuff()
        {
            var c = new ThirdPartyYoutube();

            c.GetVideoInfo(100);
            c.GetVideoInfo(100);
            c.DownloadVideo(100);
            c.DownloadVideo(100);
            c.ListVideos();

            CollectionAssert.AreEqual(new[] {
                "Get video 100",
                "Get video 100",      // nackdel: inget cache'as
                "Download video 100",
                "Download video 100", // nackdel: inget cache'as
                "List all videos"
            }, c.Events.ToArray());

            // Fördel: ingen extra kod
        }
Esempio n. 2
0
        public DownloadedVideo DownloadVideo(int id)
        {
            if (_cached.ContainsKey(id) && _cached[id] is DownloadedVideo)
            {
                _events.Enqueue($"Download video {id} from cache");

                return((DownloadedVideo)_cached[id]);
            }

            var downloaded = _thirdPartyYoutube.DownloadVideo(id);

            if (_cached.ContainsKey(id))
            {
                _cached.Remove(id);
            }

            _cached.Add(id, downloaded);

            _events.Enqueue($"Download video {id}");
            return(downloaded);
        }