Created
September 22, 2014 14:46
-
-
Save SelvinPL/3416c8fb69e83230d1e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface BTPrinter | |
{ | |
void printAt(int top, int left, String message); | |
//add more methods here | |
void selectFont(String font); | |
void cutThePaper(); | |
} | |
class ZebraPrinter implements BTPrinter | |
{ | |
public void printAt(int top, int left, String message) | |
{ | |
//it is just a sample assumption you need to send "top, left:message" | |
socket.send(top + ", " + left + ":" + message); | |
} | |
public void selectFont(String font) | |
{ | |
//we do nothing this printer do not supports fonts | |
} | |
public void cutThePaper() | |
{ | |
socket.send("cut"); | |
} | |
} | |
class BixolonPrinter implements BTPrinter | |
{ | |
public void printAt(int top, int left, String message) | |
{ | |
//it is just a sample assumption you need to send "%ttop%lleft%mmessage" | |
socket.send("%t" + top + "%l" + left + "%m" + message); | |
} | |
public void selectFont(String font) | |
{ | |
socket.send("%f" + font); | |
} | |
public void cutThePaper() | |
{ | |
socket.send("%c"); | |
} | |
} | |
class BTPrinterFactory | |
{ | |
public static public BTPrinter createInstanceFromId(String id) | |
{ | |
if(id.equals("ZebraPrinter")) | |
return new ZebraPrinter(); | |
if(id.equals("BixolonPrinter")) | |
return new BixolonPrinter(); | |
throw new UnknowDeviceException(); | |
} | |
} | |
usage: | |
String id = obtainIdFromPrinter(); | |
BTPrinter thePrinterObject = BTPrinterFactory.createInstanceFromId(id); | |
thePrinterObject.cutThePaper(); | |
thePrinterObject.printAt(10,10, "hello"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment