static void Main(string[] args) { var MediaStorageInstance = new MediaStorage(); var AudioPlayerInstance = new AudioPlayer(); var AudioPlayerDelegateInstance = new MediaStorage.PlayMedia(AudioPlayerInstance.PlayAudioFile); MediaStorageInstance.ReportResult(AudioPlayerDelegateInstance); }
static void Main(string[] args) { var mediaStorage = new MediaStorage(); var audioPlayer = new AudioPlayer(); var videoPlayer = new VideoPlayer(); var audioDelegate = new MediaStorage.PlayMedia(audioPlayer.PlayAudio); var videoDelegate = new MediaStorage.PlayMedia(videoPlayer.PlayVideo); mediaStorage.ReportResult(new AudioPlayer().PlayAudio); mediaStorage.ReportResult(audioDelegate); mediaStorage.ReportResult(videoPlayer.PlayVideo); }
public void Run() { MediaStorage myMediaStorage = new MediaStorage(); AudioPlayer myAudioPlayer = new AudioPlayer(); VideoPlayer myVideoPlayer = new VideoPlayer(); MediaStorage.PlayMedia audioPlayerDelegate = new MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile); MediaStorage.PlayMedia videoPlayerDelegate = new MediaStorage.PlayMedia(myVideoPlayer.PlayVideoFile); myMediaStorage.ReportResult(audioPlayerDelegate); myMediaStorage.ReportResult(videoPlayerDelegate); }
public void Run() { MediaStorage myMediaStorage = new MediaStorage(); // instantiate the two media players AudioPlayer myAudioPlayer = new AudioPlayer(); // instantiate the delegates MediaStorage.PlayMedia audioPlayerDelegate = new MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile); // call the delegates myMediaStorage.ReportResult(audioPlayerDelegate); }
public void Run() { MediaStorage myMediaStorage = new MediaStorage(); // instantiate the two media players AudioPlayer myAudioPlayer = new AudioPlayer(); VideoPlayer myVideoPlayer = new VideoPlayer(); // instantiate the delegates MediaStorage.PlayMedia audioPlayerDelegate = new MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile); MediaStorage.PlayMedia videoPlayerDelegate = new MediaStorage.PlayMedia(myVideoPlayer.PlayVideoFile); // call the delegates myMediaStorage.ReportResult(audioPlayerDelegate); myMediaStorage.ReportResult(videoPlayerDelegate); }
//To test the delegate public static void run() { string check = "y"; while (!(check == "exit" || check == "n")) { MediaStorage.PlayMedia audioDelegate = new MediaStorage.PlayMedia(AudioFile.playAudio); MediaStorage.PlayMedia videoDelegate = new MediaStorage.PlayMedia(VideoFile.playVideo); MediaStorage store = new MediaStorage(); store.reportResult(audioDelegate); //non-static field needs object reference Console.WriteLine(); store.reportResult(videoDelegate); Console.Write("\r\nEnter any character to continue (Enter 'exit' or 'n' to quit): "); check = Console.ReadLine(); Console.WriteLine(); } }
// in Main we use the higher-order function public static void Main() { // instantiate the storage class var ms = new MediaStorage(); // instantiate the player classes var aPlayer = new AudioPlayer(); var vPlayer = new VideoPlayer(); // instantiate the delegate var aDelegate = new MediaStorage.PlayMedia(aPlayer.PlayAudioFile); var vDelegate = new MediaStorage.PlayMedia(vPlayer.PlayVideoFile); // provide instances to the method using the delegate ms.ReportResult(aDelegate); ms.ReportResult(vDelegate); }
static void Main(string[] args) { // Instatiate the MediaStorage class // and each of the media players MediaStorage myMediaStorage = new MediaStorage(); AudioPlayer myAudioPlayer = new AudioPlayer(); VideoPlayer myVideoPlayer = new VideoPlayer(); // Instantiate the delegates MediaStorage.PlayMedia audioPlayerDelegate = new MediaStorage.PlayMedia(myAudioPlayer.PlayAudioFile); MediaStorage.PlayMedia videoPlayerDelegate = new MediaStorage.PlayMedia(myVideoPlayer.PlayVideoFile); // If you uncomment the below line of code you will get a compiler error. // We are trying to encapsulate a method that does not match the signature of the delegate. //MediaStorage.PlayMedia alternativeDelegate = new MediaStorage.PlayMedia(myAudioPlayer.AlternativePlayer); // Delegates are used with the ReportResult() method to see if the media // files are valid. myMediaStorage.ReportResult(audioPlayerDelegate); myMediaStorage.ReportResult(videoPlayerDelegate); Console.ReadKey(); }