How can i convert this to a factory/abstract factory?
- by Amitd
I'm using MigraDoc to create a pdf document.
I have business entities similar to the those used in MigraDoc.
public class Page{
public List<PageContent> Content { get; set; }
}
public abstract class PageContent {
public int Width { get; set; }
public int Height { get; set; }
public Margin Margin { get; set; }
}
public class Paragraph : PageContent{
public string Text { get; set; }
}
public class Table : PageContent{
public int Rows { get; set; }
public int Columns { get; set; }
//.... more
}
In my business logic, there are rendering classes for each type
public interface IPdfRenderer<T>
{
T Render(MigraDoc.DocumentObjectModel.Section s);
}
class ParagraphRenderer
: IPdfRenderer<MigraDoc.DocumentObjectModel.Paragraph>
{
BusinessEntities.PDF.Paragraph paragraph;
public ParagraphRenderer(BusinessEntities.PDF.Paragraph p)
{
paragraph = p;
}
public MigraDoc.DocumentObjectModel.Paragraph Render(MigraDoc.DocumentObjectModel.Section s)
{
var paragraph = s.AddParagraph(); // add text from paragraph etc
return paragraph;
}
}
public class TableRenderer : IPdfRenderer<MigraDoc.DocumentObjectModel.Tables.Table>
{
BusinessEntities.PDF.Table table;
public TableRenderer(BusinessEntities.PDF.Table t)
{
table =t;
}
public MigraDoc.DocumentObjectModel.Tables.Table Render(Section obj)
{
var table = obj.AddTable();
//fill table based on table
}
}
I want to create a PDF page as :
var document = new Document();
var section = document.AddSection();// section is a page in pdf
var page = GetPage(1); // get a page from business classes
foreach (var content in page.Content)
{
//var renderer = createRenderer(content); //
// get Renderer based on Business type ??
// renderer.Render(section)
}
For createRenderer() i can use switch case/dictionary and return type.
How can i get/create the renderer generically based on type ?
How can I use factory or abstract factory here?
Or which design pattern better suits this problem?