diff --git a/src/MediatorPattern/MediatorPatternExamples.cs b/src/MediatorPattern/MediatorPatternExamples.cs index 99f28dc..84a11f5 100644 --- a/src/MediatorPattern/MediatorPatternExamples.cs +++ b/src/MediatorPattern/MediatorPatternExamples.cs @@ -13,11 +13,11 @@ namespace MediatorPattern { Console.WriteLine(GetPatternDescription()); Console.WriteLine(GetActors()); + Console.WriteLine(WhenToUseIt()); + GoToNextStep(); - //GoToNextStep(); - - //StockExchangeExample stockExample = new StockExchangeExample(); - //stockExample.Run(); + StockExchangeExample stockExample = new StockExchangeExample(); + stockExample.Run(); GoToNextStep(); AirTrafficControlExample airTraficExample = new AirTrafficControlExample(); @@ -41,6 +41,16 @@ Colleague: objects that communicate through the mediator "; } + static string WhenToUseIt() + { + return @"When to use it: +Identify a collection of interacting objects whose interaction needs simplification +Get a new abstract class that encapsulates that interaction +Create a instance of that class and redo the interaction with that class alone +But, don’t play God! +"; + } + private static void GoToNextStep() { Console.ReadKey(); diff --git a/src/MediatorPattern/UserToGroup/Group.cs b/src/MediatorPattern/UserToGroup/Group.cs new file mode 100644 index 0000000..1ca187d --- /dev/null +++ b/src/MediatorPattern/UserToGroup/Group.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MediatorPattern.UserToGroup +{ + /// + /// Colleague + /// + public class Group + { + public int ID { get; set; } + } +} diff --git a/src/MediatorPattern/UserToGroup/User.cs b/src/MediatorPattern/UserToGroup/User.cs new file mode 100644 index 0000000..dbd2e59 --- /dev/null +++ b/src/MediatorPattern/UserToGroup/User.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MediatorPattern.UserToGroup +{ + /// + /// Colleague + /// + public class User + { + UsersToGroups usersToGroups; + public User(UsersToGroups usersToGroups) + { + this.usersToGroups = usersToGroups; + } + public int ID { get; set; } + + public void AddGroup(Group g) + { + usersToGroups.AddUserToGroup(this, g); + } + } +} diff --git a/src/MediatorPattern/UserToGroup/UsersToGroups.cs b/src/MediatorPattern/UserToGroup/UsersToGroups.cs new file mode 100644 index 0000000..49a9778 --- /dev/null +++ b/src/MediatorPattern/UserToGroup/UsersToGroups.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MediatorPattern.UserToGroup +{ + /// + /// Mediator + /// + public class UsersToGroups + { + List> usersToGroups; + + public UsersToGroups() + { + usersToGroups = new List>(); + + } + + public void AddUserToGroup(User user, Group group) + { + if (!usersToGroups.Any(ug => ug.Item1.ID == user.ID && ug.Item2.ID == group.ID)) + usersToGroups.Add(new Tuple(user, group)); + } + + public void RemoveUserFromGroup(User user, Group group) + { + usersToGroups.RemoveAll(t => t.Item1.ID == user.ID && t.Item2.ID == group.ID); + } + + } +}