static void Main(string[] args) { Console.WriteLine("**** Fun with lazy inastantiation ****"); MediaPlayer mp = new MediaPlayer(); mp.Play(); MediaPlayer mp2 = new MediaPlayer(); AllTracks tracks = mp2.GetAllTracks(); Console.ReadLine(); }
static void Main(string[] args) { // 这里没有分配AkkTracks对象 MediaPlayer myPlayer = new MediaPlayer(); myPlayer.Play(); // 在调用GetAllTracks()时分配AllTracks MediaPlayer yourPlayer = new MediaPlayer(); AllTracks yourMusic = yourPlayer.GetAllTracks(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Lazy Instantiation *****\n"); // This caller does not care about getting all songs, // but indirectly created 10,000 objects! MediaPlayer myPlayer = new MediaPlayer(); myPlayer.Play(); MediaPlayer yourPlayer = new MediaPlayer(); AllTracks yourMusic = yourPlayer.GetAllTracks(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Lazy Instantiation *****\n"); // No allocation of AllTracks object here! MediaPlayer myPlayer = new MediaPlayer(); myPlayer.Play(); // Allocation of AllTracks happens when you call GetAllTracks(). MediaPlayer yourPlayer = new MediaPlayer(); AllTracks yourMusic = yourPlayer.GetAllTracks(); Console.ReadLine(); }
static void Main(string[] args) { // Никакого размещения объекта AllTracks здесь не происходит // из-за ленивого создания объектов MediaPlayer myPlayer = new MediaPlayer(); myPlayer.Play(); // Размещение объекта AllTracks происходит // только в случае вызова метода GetAllTracks() MediaPlayer yourPlayer = new MediaPlayer(); AllTracks yourMusic = yourPlayer.GetAllTracks(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Lazy Instantiation *****\n"); // This caller does not care about getting all songs, // but indirectly created 10,000 objects! // With Lazy<AllTracks>, no allocation of AllTracks object here! MediaPlayer myPlayer = new MediaPlayer(); myPlayer.Play(); // Alloctation of AllTracks happens when you call GetAllTracks(). MediaPlayer yourPlayer = new MediaPlayer(); AllTracks yourMusic = yourPlayer.GetAllTracks(); Console.ReadLine(); }
static void UseLazyObject() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("=> Playback a song"); MediaPlayer player = new MediaPlayer(); player.Play(); MediaPlayer yourPlayer = new MediaPlayer(); AllTracks yourMusic = yourPlayer.GetAllTracks(); if (yourMusic == null) { Console.WriteLine("GetAllTracks() return null"); } else { Console.WriteLine($"Songs size <{yourMusic.Size()}>"); yourMusic.Choose(0); yourMusic.Choose(1); } }