This is the first code of this type that I've attempted, so I'm wondering if anybody would be willing to critique it. It's "sort of" (I think) a factory class. I want to keep it as simple as possible (and still work, and be at least as elegant as Phyllis Diller - it doesn't have to be Elizabeth Taylor).
The situation is that we may eventually support any number of printer types (printing labels from a Windows CE device), but want to keep the client code to something as simple as:
var beltPrinter = new BeltPrinter();
beltPrinter.PrintLabel(price, description, barcode);
Each print job will consist of 0..N preparatory commands sent to the printer ("hey, wake up!") followed by 1..N print commands, followed by 0..N shutdown commands.
Based on that, does the following make sense? If not, what would you advise that I change?
interface IBeltPrinter
{
private List<string> printerSetupCmds;
private List<string> printLabelCmds;
private List<string> printerTeardownCmds;
void PrintLabel();
}
class BeltPrinter : IBeltPrinter
{
if (<ZebraQL220>) // don't know yet how I will determine which printer is in use - perhaps from reading a config file
{
return ZebraQL220Printer;
}
else if (<ONeal>)
{
return ONealPrinter;
}
}
class ZebraQL220Printer : IBeltPrinter
{
public void PrintLabel(string Price, string Desc, string Barcode)
{
var _price = Price;
var _desc = Desc;
var _barcode = Barcode;
printerSetupCmds = new List<string>();
printerSetupCmds.Add(''); // the format and commands, etc. will differ from other printers
. . .
printLabelCmds = new List<string>();
printLabelCmds.Add('');
. . .
printerTeardownCmds = new List<string>();
printerTeardownCmds.Add('');
. . .
foreach (string line in printerSetupCmds) {
//send to ZebraQL220
}
foreach (string line in printLabelCmds) {
//send to ZebraQL220
}
foreach (string line in printerTeardownCmds) {
//send to ZebraQL220
}
}
}
class ONealPrinter : IBeltPrinter
{
public void PrintLabel(string Price, string Desc, string Barcode)
{
var _price = Price;
var _desc = Desc;
var _barcode = Barcode;
printerSetupCmds = new List<string>();
printerSetupCmds.Add(''); // the format and commands, etc. will differ from other printers
. . .
printLabelCmds = new List<string>();
printLabelCmds.Add('');
. . .
printerTeardownCmds = new List<string>();
printerTeardownCmds.Add('');
. . .
foreach (string line in printerSetupCmds) {
//send to ONeal
}
foreach (string line in printLabelCmds) {
//send to ONeal
}
foreach (string line in printerTeardownCmds) {
//send to ONeal
}
}
}
private List<string> printerSetupCmds;
). The lineclass IBeltPrinter BeltPrinter()
is also invalid syntax. These errors in your example are significant enough to make it rather difficult to determine the intent of your code. \$\endgroup\$ – Steven Doggart Jul 25 '13 at 12:22IBeltPrinter NewBeltPrinter()
method. \$\endgroup\$ – Steven Doggart Jul 25 '13 at 16:52