Hello guys, I want to use the Mediator Design Pattern in my projects to create more robust and responsive User Interfaces. I am used to use this pattern wtih Javascript since its allow you to do Black Magic ( to call functions with variable names and variable number and type of parameters) but I never did it in C#, so I am looking for tips about its implementation.
Basically I want a mediator object to receive messages about updates in any area of the system. Then the mediator would “spread” these news to other objects interested on it.
Lets say that I have a list of players, when I click on ‘delete’ Buttom from a user from it I could just publish this event:
mediator.publish( "delete", [userIndex, "Dead"]);
And the interested objects that were subscribed to this mediator would be responsible to response to this update. A text label could just update the number of users, the dataManager could delete it from the data manager and it goes on.
Looking for available solutions I found some links like this Javascript one or the SendMessage function from Unity3D but any of these seem to be the solution for me. Then I tried to adapt the idea that I did on my Javascript Project.
I didn’t test the following code, it was just a sketch code:
/** Class responsible to mediate messages in the system **/
public class Mediator
{
// List of objects that will be notified about any news in the system
public List<MediatorSubscriber> subscribers;
public void subscribe( MediatorSubscriber ms )
{
subscribers.Add( ms );
}
public virtual void publish( string key, List<Object> pars )
{
foreach( MediatorSubscriber ms in subscribers )
{
ms.receiveNews( key, pars );
}
}
public Mediator()
{
//does nothing
}
}
/** Class that represent the objects interested in system updates **/
public class MediatorSubscriber
{
public virtual void receiveNews( string key, List<Object> pars )
{
Debug.LogError("News system not configured");
}
}
/** Example of a kind of a generic subscriber interested in data updates **/
public class DataSubscriber :: MediatorSubscriber
{
public virtual void deleteUser( int index, string cause )
{
//does nothing
}
public virtual void usersLoaded( List<Users> u )
{
//does nothing
}
// Convert abstract news to commands
public override void receiveNews( string key, List<Object> pars )
{
if( key == "delete" )
{
Integer inx = pars[0] as int;
string cause = pars[1] as string;
deleteUser( inx, cause );
}
else if( key == "usersLoaded" )
{
List<User> users = pars[0] as List<Users>;
usersLoaded( users );
}
else
{
Debug.LogError("Invalid key");
}
}
}
/** Example of a subscriber specialization to be used in a list or table game object **/
public class UIListManipulator : DataSubscriber, MonoBehaviour
{
// Filled by Inspector drag and drop
public GameObject listPanel;
public Mediator mediator;
void Start()
{
mediator.subscribe( this );
}
public override void usersLoaded( List<Users> u )
{
// fill the UI list with a loop
}
public override void deleteUser( int index, string cause )
{
// delete the line with the deleted user
}
}
This way I would have a well-documented way to describe event messages and decouple the UI part from the rest of system, what do you guys think about it, do you have suggestions? Thanks for the attention.