We are building our software with Model-View-Presenter passive view strategy. On our software we have a report and different kind of charts. So we have an abstract class for chart for functions that all charts will implement:
abstract class AbstractChart
{
...
}
Then we have concrete classes (models), lets say BarChart and PieChart:
class BarChart: AbstractChart
{
...
}
class PieChart: AbstractChart
{
...
}
A report can contain different kind of charts.
class Report
{
public List<AbstractChart> Charts {get; set; }
...
}
So, we have a problem on drawing different charts on a report:
class ReportPresenter
{
Report _report;
ReportView _view;
...
FillReportView(Report report)
{
foreach(AbstractChart chart in _report.Charts)
{
// Here is the problem: How do we create correct
// view and presenter for abstract chart? We need to
// create them, so we can add chart view to _view.
}
}
}
In lack of a better idea, I’ve solved this problem by making a Factory-class that creates an appropriate presenter for a model. For now I have the view created in their presenters, although it would be better to use an IoC-container like Unity.