Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. public class Messenger
  2. {
  3. /// <summary>
  4. /// Gets the default messenger.
  5. /// </summary>
  6. /// <value> The default messenger. </value>
  7. public static Messenger Default
  8. {
  9. get
  10. {
  11. defaultMessenger = defaultMessenger ?? new Messenger();
  12.  
  13. return defaultMessenger;
  14. }
  15. }
  16.  
  17. /// <summary>
  18. /// The default messenger
  19. /// </summary>
  20. private static Messenger defaultMessenger;
  21.  
  22. /// <summary>
  23. /// The actions hooked up to messages
  24. /// </summary>
  25. private Dictionary<Type, Delegate> actions = new Dictionary<Type, Delegate>();
  26.  
  27. /// <summary>
  28. /// The functions hooked up to requests
  29. /// </summary>
  30. private Dictionary<Type, Delegate> functions = new Dictionary<Type, Delegate>();
  31.  
  32. /// <summary>
  33. /// Register a function for a request message.
  34. /// </summary>
  35. /// <typeparam name="T"> Type of message to receive. </typeparam>
  36. /// <typeparam name="R"> Type of the r. </typeparam>
  37. /// <param name="request"> The function that fills the request. </param>
  38. public void Register<T, R>(Func<T, R> request)
  39. {
  40. if (request == null)
  41. {
  42. throw new ArgumentNullException("request");
  43. }
  44.  
  45. if (functions.ContainsKey(typeof(T)))
  46. {
  47. functions[typeof(T)] = Delegate.Combine(functions[typeof(T)], request);
  48. }
  49. else
  50. {
  51. functions.Add(typeof(T), request);
  52. }
  53. }
  54.  
  55. /// <summary>
  56. /// Register an action for a message.
  57. /// </summary>
  58. /// <typeparam name="T"> Type of message to receive. </typeparam>
  59. /// <param name="action"> The action that happens when the message is received. </param>
  60. public void Register<T>(Action<T> action)
  61. {
  62. if (action == null)
  63. {
  64. throw new ArgumentNullException("action");
  65. }
  66.  
  67. if (actions.ContainsKey(typeof(T)))
  68. {
  69. actions[typeof(T)] = (Action<T>)Delegate.Combine(actions[typeof(T)], action);
  70. }
  71. else
  72. {
  73. actions.Add(typeof(T), action);
  74. }
  75. }
  76.  
  77. /// <summary>
  78. /// Send a request.
  79. /// </summary>
  80. /// <typeparam name="T"> The type of the parameter of the request. </typeparam>
  81. /// <typeparam name="R"> The return type of the request. </typeparam>
  82. /// <param name="parameter"> The parameter. </param>
  83. /// <returns> The result of the request. </returns>
  84. public R Request<T, R>(T parameter)
  85. {
  86. if (functions.ContainsKey(typeof(T)))
  87. {
  88. var function = functions[typeof(T)] as Func<T, R>;
  89. if (function != null)
  90. {
  91. return function(parameter);
  92. }
  93. }
  94. return default(R);
  95. }
  96.  
  97. /// <summary>
  98. /// Sends the specified message.
  99. /// </summary>
  100. /// <typeparam name="T"> The type of message. </typeparam>
  101. /// <param name="message"> The message. </param>
  102. public void Send<T>(T message)
  103. {
  104. if (actions.ContainsKey(typeof(T)))
  105. {
  106. ((Action<T>)actions[typeof(T)])(message);
  107. }
  108. }
  109.  
  110. /// <summary>
  111. /// Unregister a request.
  112. /// </summary>
  113. /// <typeparam name="T"> The type of request to unregister. </typeparam>
  114. /// <typeparam name="R"> The return type of the request. </typeparam>
  115. /// <param name="request"> The request to unregister. </param>
  116. public void Unregister<T, R>(Func<T, R> request)
  117. {
  118. if (functions.ContainsKey(typeof(T)))
  119. {
  120. functions[typeof(T)] = Delegate.Remove(functions[typeof(T)], request);
  121. }
  122. }
  123.  
  124. /// <summary>
  125. /// Unregister a message.
  126. /// </summary>
  127. /// <typeparam name="T"> The type of message. </typeparam>
  128. /// <param name="action"> The action to unregister. </param>
  129. public void Unregister<T>(Action<T> action)
  130. {
  131. if (actions.ContainsKey(typeof(T)))
  132. {
  133. actions[typeof(T)] = (Action<T>)Delegate.Remove(actions[typeof(T)], action);
  134. }
  135. }
  136. }
  137.  
  138. public class Receiver
  139. {
  140. public Receiver()
  141. {
  142. Messenger.Default.Register<string>(x =>
  143. {
  144. Console.WriteLine(x);
  145. });
  146. Messenger.Default.Register<string, string>(x =>
  147. {
  148. if (x == "hello")
  149. {
  150. return "world";
  151. }
  152. return "who are you?";
  153. });
  154. }
  155. }
  156.  
  157. public class Sender
  158. {
  159. public Sender()
  160. {
  161. Messenger.Default.Send<string>("Hello world!");
  162.  
  163. Console.WriteLine(Messenger.Default.Request<string, string>("hello"));
  164.  
  165. Console.WriteLine(Messenger.Default.Request<string, string>("hi"));
  166. }
  167. }
  168.  
  169. Hello world!
  170. world
  171. who are you?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement