i have a class Row.
This class should have a Content-property. Currently the content is of type: List<IRowContent>
(IRowContent is a interface)
Other classes Column and TextContent, ImageContent implements the interface IRowContent.
I can add now some Columns to the list or real “content” (Text or a image).
But you can also add columns and text/image. But if a row contains text/image it should not contain another item.
How can i design my class-structure to support this?
Edit: some additionals infos:
I want to build a layout with “fluent interfaces” http://en.wikipedia.org/wiki/Fluent_interface
And my idee is to prevent wrong use by intellisense of VisualStudio.
Here my classes:
The Layout have a column-list.
class Layout
{
//Attributes
public Color Background { get; set; }
public List<Column> Columns { get; set; }
public uint Margin { get; set; }
public Layout AddColumn(Column c)
{
return null;
}
public Layout SetColumnList(List<Column> c)
{
return null;
}
}
The column has a list of content (IColumnContent). The column itself is from IRowContent.
class Column : IRowContent
{
public List<IColumnContent> Content { get; private set; }
public Column AddToContent(IColumnContent obj)
{
return null;
}
public Column SetContent(List<IColumnContent> objs)
{
return null;
}
}
Same for Row with IRowContent:
class Row : IColumnContent
{
public List<IRowContent> Content { get; private set; }
//...
}
ImageContent and TextContent implements both interfaces:
class TextContent : IRowContent, IColumnContent
class ImageContent : IRowContent, IColumnContent
If you declare the interface
You can override a collection’s insert and remove methods to support this behavior: