Add partial example UsersToGroups

This commit is contained in:
Petrutiu Mihai
2016-06-23 17:54:44 +03:00
parent a632b5a6d9
commit 06d201c917
4 changed files with 87 additions and 4 deletions

View File

@@ -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, dont play God!
";
}
private static void GoToNextStep()
{
Console.ReadKey();

View 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; }
}
}

View 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);
}
}
}

View 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);
}
}
}