Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Mediator {
  5. /// <summary>
  6. /// The 'Mediator' abstract class
  7. /// </summary>
  8. public abstract class AbstractChatroom {
  9. public abstract void Register (User user);
  10. public abstract void Send (string from, string to, string message);
  11.  
  12. }
  13. }
  14. namespace Mediator {
  15. /// <summary>
  16. /// The 'ConcreteMediator' class
  17. /// </summary>
  18. public class Chatroom : AbstractChatroom {
  19. private Dictionary<string, User> _users = new Dictionary<string, User> ();
  20. public override void Register (User user) {
  21. if (!_users.ContainsValue (user)) {
  22. _users[user.Name] = user;
  23. }
  24. user.Chatroom = this;
  25. }
  26.  
  27. public override void Send (string from, string to, string message) {
  28. User participant = _users[to];
  29.  
  30. if (participant != null) {
  31. participant.Receive (from, message);
  32. } else {
  33. throw new KeyNotFoundException ("User not found");
  34. }
  35. }
  36. }
  37. }
  38.  
  39. namespace Mediator {
  40. public class User {
  41. public User (string name) {
  42. Name = name;
  43. }
  44. public Chatroom Chatroom { get; set; }
  45. public string Name { get; }
  46.  
  47. public Stack < (string source, string message) > MessageCache { get; private set; } = new Stack < (string source, string message) > ();
  48.  
  49. // it is also possible to create interface for recipient aka 'AbstractColleague'
  50. public void Receive (string from, string message) {
  51. MessageCache.Push ((source: from, message: message));
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement