private static void UseEvents() { var video = new Video() { Title = "video 1" }; var videoEncoder = new VideoEncoder(); // publisher var mailService = new MailService(); // subscriber var messageService = new MessageService(); // subscriber //registering a handler (OnVideoEncoded) for the event. 'mailService.OnVideoEncoded' is a reference or pointer to the method videoEncoder.VideoEncoded += mailService.OnVideoEncoded; videoEncoder.VideoEncoded += messageService.OnVideoEncoded; videoEncoder.Encode(video); //_____________NOTES_____________ //Events... //...a mechanism for communication between objects //...used in building Loosely Coupled Applications //...helps extending applications //Delegates... //...agreement/contract between Publisher and Subscriber //...determines the signature of the event handler method in Subscriber }
static void Main(string[] args) { var video = new Video { Title = "Titanic" }; var videoEncoder = new VideoEncoder(); //Publisher var mailService = new MailService(); //Subscriber var messageService = new TextMessageServices(); //Subscriber videoEncoder.VideoEncoded += mailService.OnVideoEncodedMail; videoEncoder.VideoEncoded += messageService.OnVideoEncodedTextMessage; videoEncoder.Encode(video); Console.ReadKey(); }