I'm implementing the exporting feature for my project. I have a question about design pattern .
What is the best pattern for implementing a feature that allows exporting from database to different kind of format(i.e. plain text, CSV, XML, PDF...).?
In my case, I have to choose among Template Method, Proxy and Observer. Which should i choose?
Thank you
You are (potentially) looking for the Strategy pattern
whereby an algorithm's behaviour can be selected at runtime
Essentially you might have something like an IExporter, then multiple implimentations XmlExporter, PlainTextExporter etc. then at run time you could select one or more of the to actually execute, the execution call would be the same but the result would be different.
Like:
public abstract class ExporterBase
{
abstract public bool Export(Record record);
}
public class DatabaseExporter : ExporterBase
{
public bool Export(Record record)
{
// TODO: Write to DB
return true;
}
}
public class CsvExporter : ExporterBase
{
public bool Export(Record record)
{
// TODO: Write to Csv file
return true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With