Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern for different kinds of format [closed]

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

like image 508
xtiger Avatar asked Dec 01 '25 06:12

xtiger


1 Answers

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;
  }
}
like image 154
shenku Avatar answered Dec 04 '25 12:12

shenku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!