I need to design a receipt object that will take line items in a transaction and format them for a 40 column display. In the future I will also need another format for regular sized printer paper.
The transaction has different types of line items (items, comments, discounts, tenders, etc.).
My first thought was to create an interface for each of these so that they would be responsible for formatting themselves, and I could add a new method to the interface for each type of receipt format I need. My next thought was to create one class for each type of receipt format I need and let it be responsible for looking at what type each line is and formatting it appropriately.
So my question is whether there is a better design pattern that I may be overlooking, and if not then is there significant reason to favor one of the above designs over the other?
So I could add something like this:
public interface IReceiptFormat
{
string FormatFor40Column();
string FormatForRegularPaper();
}
to my Item, Comment, etc. classes. Or I could create something like this:
public ReceiptFormatterFor40Column
{
public Ticket Ticket {get; private set;}
public ReceiptFormatterFor40Column(Ticket ticket)
{
Ticket = ticket;
}
public List<string> GenerateReceipt()
{
var lines = new List<string>();
foreach(var line in Ticket.Lines)
{
// check what type of object line is and add
// add an appropriately formatted string to lines
}
}
}