Skip to content

Instantly share code, notes, and snippets.

@justinmchase justinmchase/Design.cs
Last active Aug 29, 2015

Embed
What would you like to do?
Simple Composition Design
interface IObject
{
string Name { get; }
IObject Parent { get; }
void Add(string name, IComponent component);
void Add(IObject child);
void Remove(IObject child);
IComponent GetComponent(string name);
IEnumerable<IObject> GetChildren();
void SendMessage(string message, params object[] parameters);
}
interface IComponent
{
IObject Object { get; }
}
// A naieve implementation of SendMessage
public void SendMessage(string message, params object[] parameters)
{
var types = parameters.Select(a => a == null ? typeof(object) : a.GetType()).ToArray();
foreach (var component in this.components.Values)
{
var method = component.GetType().GetMethod(message, types);
if (method != null)
{
// Call methods on the component via reflection
method.Invoke(component, parameters);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.