I've been thinking recently about the Liskov Substitution Principle and how it relates to my current task. I have a main form which contains a menu; into this main form I will dock a specific form as a MDI child. This specific form may or may not contain a menu. If it does, I want to merge it with the main menu. So the situation is like this:
public class MainForm
{
public void Dock(SpecificBaseForm childForm)
{
this.Dock(childForm);
if (childForm is IHaveMenu)
{
this.Menu.Merge(childForm as IHaveMenu).Menu;
}
}
}
public interface IHaveMenu
{
Menu Menu {get;}
}
public abstract class SpecificBaseForm{}
public class SpecificFormFoo : SpecificBaseForm {}
public class SpecificFormBar: SpecificBaseForm,IHaveMenu
{
public Menu Menu{get {return this.Menu;}}
}
Now the two children are not "exactly" interchangeble: one has a menu; the other doesn't. Is this a misuse of the is/as contruct in C#? If this is not correct, what would be the clean solution?