I have some simple scheduling of various events in my application and I used this class hierarchy:
abstract class ScheduledEvent
{
public DateTime When{ get; set; }
public abstract void Trigger();
}
class WhateverEvent : ScheduledEvent
{
public override void Trigger();
}
class AnotherEvent : ScheduledEvent
{
public override void Trigger();
}
class Scheduler
{
public AddEvent(ScheduledEvent event)
}
My application works fine but I don't like the design because whenever I want to add a new event I have to add a new descendant to the hierarchy and all books say prefer composition over inheritance so I tried this approach:
interface ITriggerable
{
void Trigger();
}
class WhateverEvent : ITriggerable
{
public void Trigger();
}
abstract class ScheduledEvent
{
private ITriggerable triggerable;
public ScheduledEvent(ITriggerable t) {
triggerable = t;
}
public DateTime When{ get; set; }
public void Trigger() { triggerable.Trigger(); }
}
But problem occurred immediately. For every type of event there was a form for editing of its properties like this:
class WhateverForm : Form
{
WhateveverForm(WhateverEvent ev) { ... }
}
This form had available all properties of WhateverEvent
and also property When
of ScheduledEvent
. With my new hierarchy this property is not available anymore. I can of course add reference to ScheduledEvent
to WhateverEvent
but I don't like it.
My question is how would you design your class hierarchy and why? It may seem trivial for you but I don't have much real experience with design and I want to do it right.