Guest User

Untitled

a guest
Feb 3rd, 2019
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using static System.Console;
  3.  
  4. namespace NotificationPattern
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. var userWithoutEmailAndPassword = new User(null, null);
  11. var userWithoutPassword = new User("user@gmail.com", null);
  12. var userWithoutEmail = new User(null, "duvidei_descobrir_minha_senha_HAHAHAHAHA");
  13. var user = new User("user@gmail.com", "duvidei_descobrir_minha_senha_HAHAHAHAHA");
  14.  
  15. if (!userWithoutEmailAndPassword.IsValid())
  16. {
  17. WriteLine($"userWithoutEmailAndPassword = {string.Join("; ", userWithoutEmailAndPassword.Notifications)}");
  18. }
  19.  
  20. if (!userWithoutPassword.IsValid())
  21. {
  22. WriteLine($"userWithoutPassword = {string.Join("; ", userWithoutPassword.Notifications)}");
  23. }
  24.  
  25. if (!userWithoutEmail.IsValid())
  26. {
  27. WriteLine($"userWithoutEmail = {string.Join("; ", userWithoutEmail.Notifications)}");
  28. }
  29.  
  30. if (user.IsValid())
  31. {
  32. WriteLine($"User is valid!");
  33. }
  34.  
  35. ReadKey();
  36. }
  37. }
  38.  
  39. public class Notification
  40. {
  41. public Notification(string code, string message)
  42. {
  43. Code = code;
  44. Message = message;
  45. }
  46.  
  47. public string Code { get; }
  48.  
  49. public string Message { get; }
  50.  
  51. public override string ToString() => Message;
  52. }
  53.  
  54. public abstract class Notifiable
  55. {
  56. private List<Notification> _notifications;
  57.  
  58. public IReadOnlyCollection<Notification> Notifications => InternalNotifications;
  59.  
  60. private List<Notification> InternalNotifications
  61. {
  62. get
  63. {
  64. if (_notifications == null)
  65. {
  66. _notifications = new List<Notification>();
  67. }
  68.  
  69. return _notifications;
  70. }
  71. }
  72.  
  73. public void AddNotification(Notification notification)
  74. {
  75. if (notification != null)
  76. InternalNotifications.Add(notification);
  77. }
  78.  
  79. public void ClearNotifications() => _notifications = null;
  80.  
  81. public bool IsValid() => InternalNotifications.Count == 0;
  82. }
  83.  
  84. public class User : Notifiable
  85. {
  86. public User(string email, string password)
  87. {
  88. Email = email;
  89. Password = password;
  90.  
  91. if (string.IsNullOrEmpty(Email))
  92. {
  93. AddNotification(new Notification("USU-01", "Email must be informed."));
  94. }
  95.  
  96. if (string.IsNullOrEmpty(Password))
  97. {
  98. AddNotification(new Notification("USU-02", "Password must be informed."));
  99. }
  100. }
  101.  
  102. public string Email { get; private set; }
  103.  
  104. public string Password { get; private set; }
  105. }
  106. }
Add Comment
Please, Sign In to add comment