Add partial example UsersToGroups
This commit is contained in:
@@ -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();
|
||||
|
||||
15
src/MediatorPattern/UserToGroup/Group.cs
Normal file
15
src/MediatorPattern/UserToGroup/Group.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.UserToGroup
|
||||
{
|
||||
/// <summary>
|
||||
/// Colleague
|
||||
/// </summary>
|
||||
public class Group
|
||||
{
|
||||
public int ID { get; set; }
|
||||
}
|
||||
}
|
||||
25
src/MediatorPattern/UserToGroup/User.cs
Normal file
25
src/MediatorPattern/UserToGroup/User.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.UserToGroup
|
||||
{
|
||||
/// <summary>
|
||||
/// Colleague
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/MediatorPattern/UserToGroup/UsersToGroups.cs
Normal file
33
src/MediatorPattern/UserToGroup/UsersToGroups.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.UserToGroup
|
||||
{
|
||||
/// <summary>
|
||||
/// Mediator
|
||||
/// </summary>
|
||||
public class UsersToGroups
|
||||
{
|
||||
List<Tuple<User, Group>> usersToGroups;
|
||||
|
||||
public UsersToGroups()
|
||||
{
|
||||
usersToGroups = new List<Tuple<User, Group>>();
|
||||
|
||||
}
|
||||
|
||||
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>(user, group));
|
||||
}
|
||||
|
||||
public void RemoveUserFromGroup(User user, Group group)
|
||||
{
|
||||
usersToGroups.RemoveAll(t => t.Item1.ID == user.ID && t.Item2.ID == group.ID);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user