且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编写POS系统的***实践

更新时间:2023-02-17 17:11:07

这可能与您正在寻找的答案(!)...稍有不同.

This is probably a slightly different answer to what you were looking for(!)...

在使用外部接口"(例如打印机,现金提取等)时,总是要抽象一些东西.您可能想实施策略-模式策略.

When working with "external interfaces" (e.g. printers, cash draws etc) always abstract things. You probably want to implement strategies - Pattern Strategy.

您为现金提取创建界面:

public interface ICashDrawer
{
    void Open();
}

提供实现:

  • 一种策略是使用COM打开绘图的类
  • 另一个就像调用 Debug.WriteLine 的类一样简单,因此您在开发过程中无需将现金提取连接到PC
  • one strategy is a class that uses COM to open the draw
  • another is something as simple as a class that does a Debug.WriteLine call so you don't need a cash draw connected to your PC during development

例如

public class ComPortCashDrawer : ICashDrawer
{
    public void Open()
    {
        // open via COM port etc
    }
}

public class DebugWriterCashDrawer : ICashDrawer
{
    public void Open()
    {
        Debug.WriteLine("DebugWriterCashDrawer.Open() @ " + DateTime.Now);
    }
}

同样要进行打印,您有一个使用数据的打印界面:

Again for printing you have a print interface that takes the data:

public interface IRecieptPrinter
{
    bool Print(object someData);
}

然后您进行一个或多个实现.

then you make one or more implementations.

  • 基本打印机
  • 专业标签打印机
  • 基于文本的文本保存到文件...

例如

public class BasicRecieptPrinter : IRecieptPrinter
{
    public bool Print(object someData)
    {
        // format for a basic A4 print
        return true; // e.g. success etc
    }
}

public class SpecificXyzRecieptPrinter : IRecieptPrinter
{
    public bool Print(object someData)
    {
        // format for a specific printer
        return true; // e.g. success etc
    }
}

public class PlainTextFileRecieptPrinter : IRecieptPrinter
{
    public bool Print(object someData)
    {
        // Render the data as plain old text or something and save 
         // to a file for development or testing.
        return true; // e.g. success etc
    }
}

关于SDK,如果您出于某种原因发现您需要它,则可以使用SDK编写实现.随着时间的流逝,您可能最终会获得几种与不同的外部设备连接的方式.客户可能会在一天之内获得新的现金提取,等等.

With respect to the SDK, if down the track you find you need it for some reason you write implementations using the SDK. Over time you may end up with several ways to interface with different external devices. The client may get a new cash draw one day etc etc.

很清楚,如果您愿意的话,我可以充实我的意思,但您可能会感到不安.

Is that clear, I can flesh out what I mean if you want but you probably get my drift.

您的应用在启动时便设置了各自的实现,您可能想看看依赖注入,如果您使用容器来解决,您会发现事情变得更容易类型.

Your app sets itself up with the respective implementations at startup, you may want to take a look at Dependency injection as well and you will find things easier if you use a container to resolve the types.

var printer = container.Resolve<IRecieptPrinter>();

PK:-)