Advertisement
simonradev

EventsSample

Jan 7th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. namespace UnitTestingClasses
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class Startup
  8.     {
  9.         public static void Main()
  10.         {
  11.             Inbox personsInbox = new Inbox();
  12.             Person person = new Person("Ivan", personsInbox);
  13.  
  14.             Website website = new Website(@"https://www.softuni.bg");
  15.             website.SubscribeToNews(person);
  16.  
  17.             News firstNews = new News("first");
  18.             website.PostNews(firstNews);
  19.             News secondNews = new News("second");
  20.             website.PostNews(secondNews);
  21.  
  22.             List<News> notificationNews = person.GetNewsFromNotifications();
  23.             string titles = string.Join(Environment.NewLine, notificationNews.Select(n => n.Title));
  24.             Console.WriteLine(titles);
  25.         }
  26.     }
  27.  
  28.     public class Inbox
  29.     {
  30.         private List<News> news;
  31.  
  32.         public Inbox()
  33.         {
  34.             this.news = new List<News>();
  35.         }
  36.  
  37.         public List<News> News
  38.         {
  39.             get
  40.             {
  41.                 return this.news.ToList();
  42.             }
  43.         }
  44.  
  45.         public void AddNewsNotification(News news)
  46.         {
  47.             this.news.Add(news);
  48.         }
  49.     }
  50.  
  51.     public class Person
  52.     {
  53.         private string name;
  54.         private Inbox inbox;
  55.  
  56.         public Person(string name, Inbox inbox)
  57.         {
  58.             this.name = name;
  59.             this.inbox = inbox;
  60.         }
  61.  
  62.         public void NewsPostedEventHandler(object sender, WebsiteEventArgs websiteEventArgs)
  63.         {
  64.             News news = websiteEventArgs.News;
  65.             this.inbox.AddNewsNotification(news);
  66.         }
  67.  
  68.         public List<News> GetNewsFromNotifications()
  69.         {
  70.             return this.inbox.News;
  71.         }
  72.     }
  73.  
  74.     public delegate void NewsPostedEventHandler(object sender, WebsiteEventArgs websiteEventArgs);
  75.  
  76.     public class Website
  77.     {
  78.         private event NewsPostedEventHandler NewsPosted;
  79.  
  80.         public Website(string url)
  81.         {
  82.             this.Url = url;
  83.         }
  84.  
  85.         public string Url { get; set; }
  86.  
  87.         protected virtual void OnNewsPosted(WebsiteEventArgs websiteEventArgs)
  88.         {
  89.             this.NewsPosted?.Invoke(this, websiteEventArgs);
  90.         }
  91.  
  92.         public void PostNews(News news)
  93.         {
  94.             WebsiteEventArgs websiteEventArgs = new WebsiteEventArgs(news);
  95.             this.OnNewsPosted(websiteEventArgs);
  96.         }
  97.  
  98.         public void SubscribeToNews(Person person)
  99.         {
  100.             this.NewsPosted += person.NewsPostedEventHandler;
  101.         }
  102.     }
  103.  
  104.     public class WebsiteEventArgs : EventArgs
  105.     {
  106.         public WebsiteEventArgs(News news)
  107.         {
  108.             this.News = news;
  109.         }
  110.  
  111.         public News News { get; set; }
  112.     }
  113.  
  114.     public class News
  115.     {
  116.         private string title;
  117.  
  118.         public News(string title)
  119.         {
  120.             this.Title = title;
  121.         }
  122.  
  123.         public string Title
  124.         {
  125.             get
  126.             {
  127.                 return this.title;
  128.             }
  129.  
  130.             private set
  131.             {
  132.                 this.title = value;
  133.             }
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement