大案子条形码扫描器应用程序的设计模式建议

问题描述 投票:1回答:1

我正在寻找有关用于重构工厂中使用的Windows CE条码扫描应用程序的良好设计模式/模型的一些输入。 试图使其更加面向对象,并且减少程序性

当前,该应用程序的大部分功能实际上是通过一种主要方法中的大写情况语句控制的,私有整数的值确定了处理这些情况的路径。

由于许多扫描是按顺序执行的,因此,每次条形码扫描后,代码实际上都会遍历此主要方法,直到用户退出菜单并选择其他扫描功能为止。

对于许多事务类型,代码需要按顺序通过主case语句采用多个路径。 例如,“包装”交易要求用户首先扫描其id徽章,然后扫描目标容器,然后扫描要包装在目标容器中的物品,所有这些在主要方法中都是单独的情况。

在这种情况下,一个非常简化的示例:

private int activeTrans;
private int baseTrans;
private int nextTrans;

btnPack_Click()
{
   nextTransaction = 2
   StartTransaction(1, 1)              
}

StartTransaction(int transId, int transMode)
{
  //determines the initial path(s) to take 

  switch (transMode)
  {
    case 1:  //scan parent 
      activeTrans = 4;
      baseTrans = transId;
      break;

    //a handful of other case statements representing the major types of transactions
    ...
  }

  StartBarcodeRead() //the scanner prepared to scan, once the user scans a barcode, 
                     //the HandleData method is called
}

HandleData()
{
  switch (activeTrans)
  {
    case 1:  //select target container
      DoSomeMethodForTheTargetContainer();
      activeTrans = baseTrans;

    case 2:  //pack in container
      DoThePackingMethod();

    case 3:  //scan id badge
       ValidateIdBadgeScan();
       activeTrans = baseTrans;
       baseTrans = nextTrans;

    ... //about 20 more cases for all the different transactions
}

由于每个扫描功能都有可能成为“堆栈”或按顺序运行的事务,因此该SEEMS可能是使用Command模式的可能候选者,但是我不确定。 该代码一直运行良好-我只想尝试对其进行重构,因为我可以预见,随着需求的变化或添加,代码将变得越来越复杂。

c# design-patterns architecture barcode-scanner
1个回答
1
投票

这不是一个非OO问题,因此寻找OO设计模式是不合适的(至少在重构的这个阶段)。 您需要有限状态机,因此设计一个FSM。 如果您的FSM的特定部分看起来仍然过于繁琐或复杂,请为其寻找合适的OO设计模式。

© www.soinside.com 2019 - 2024. All rights reserved.