我正在使用MigraDoc创建一个 pdf 文档。
我有类似于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
}
在我的业务逻辑中,每种类型都有渲染类
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
}
}
我想创建一个 PDF 页面:
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)
}
对于 createRenderer() 我可以使用 switch case/dictionary 和返回类型。
我如何一般地基于类型获取/创建渲染器?
我如何在这里使用工厂或抽象工厂?
或者哪种设计模式更适合这个问题?