Why wouldn't each concrete organism invoke it's parent's Born event when it's birthed. Something like:
public abstract class Organism
{
public event EventHandler<EventArgs> Born;
public string Name { get; set; }
public Organism()
{
}
public void Birth()
{
this.raiseBorn(new EventArgs());
}
protected void raiseBorn(EventArgs args)
{
EventHandler<EventArgs> handler = this.Born;
if (handler != null)
handler(this, args);
}
}
// Concrete organisms ========--------
public class Dog : Organism
{
public Dog()
{
this.Name = "Dog";
}
public override string ToString()
{
return this.Name;
}
}
public class Cat : Organism
{
public Cat()
{
this.Name = "Cat";
}
public override string ToString()
{
return this.Name;
}
}
// Creature Factory ========--------
public class CreatureCreator : List<Organism>
{
public event EventHandler<BornArgs> CreaturBorn;
private void raiseCreatureBorn(BornArgs args)
{
EventHandler<BornArgs> handler = this.CreaturBorn;
if (handler != null)
handler(this.CreaturBorn, args);
}
public void CreateCreature<T>() where T : Organism, new()
{
Organism o = new T();
o.Born += o_Born;
this.Add(o);
o.Birth();
}
private void o_Born(object sender, EventArgs e)
{
this.raiseCreatureBorn(new BornArgs((Organism)sender));
}
}
public class BornArgs : EventArgs
{
public Organism Creature { get; set; }
public BornArgs(Organism o)
{
this.Creature = o;
}
}
// Usage ========--------
static void Main(string[] args)
{
CreatureCreator deity = new CreatureCreator();
deity.CreaturBorn += deity_CreaturBorn;
deity.CreateCreature<Dog>();
deity.CreateCreature<Cat>();
Console.ReadKey();
}
static void deity_CreaturBorn(object sender, BornArgs e)
{
Console.WriteLine(e.Creature.ToString() + " was born");
}
output:
Dog was born
Cat was born