dsy1971 2013-12-22 05:44
浏览 66
已采纳

USSD(状态机)应用算法

I have created a USSD application in PHP, Since USSD requests are unique ( new request for each selection ) I need a way to track them , Track in the sence i need a way to store the flow of the application, In my USSD app there are Static menus as well as Dynamic menus ( Retrieved from the Database ) , My problem is , I have created the app with lots of if, else and switch statements, And It's really difficult to update the menu items. So can anyone suggest me a good algorithm to create this USSD app? I mean a efficient way to tackle the problem, I need a way where I could be able to add and remove menus according the clients request, When I do that , my code shouldn't break. Can anyone help me on this?

-Thanks & Regards

PS: I have read this and this and it doesn't actually answer my question.

  • 写回答

3条回答 默认 最新

  • dongsha9208 2014-01-17 08:44
    关注

    The best method working with USSD is making each command a separate class. This way you can work , debug and maintain a large USSD application

    Example

    $ussd = new USSDMessage(); // USSD Class 
    
    $main = new Command("menu"); // Starting Command 
    
    // Register multiple command 
    $main->register(new Command("help"));
    $main->register(new Command("account"));
    $main->register(new Command("transfer"));
    $main->register( new Recharge("reacharge"));
    
    $main->run($ussd); // Run Request 
    

    Basic Class Structure

    // Class To receve message from USSD
    class USSDMessage {
        function next();
        function saveSession();
    }
    
    // Basic Command  Class 
    class Command {
        private $name;
        private $cmd = array();
    
        function __construct($name) {
            $this->name = $name;
        }
    
        function register(Command $menu) {
            $this->cmd[$menu->getName] = $menu;
        }
    
        function run(USSDMessage $ussd) {
            $key = $ussd->next(); // get next inpur from ussd
    
            if (isset($this->cmd[$key])){
                $ussd->run($ussd);
            }
        }
    }
    
    // Recharge Command Example 
    class Recharge extends Command {
    
        function run(USSDMessage $ussd) {
            $key = $ussd->next();
    
            switch ($key){
                case "1":
                    // Proccess card recharge
                    // Do Somthing 
                    $ussd->saveSession(); // End Session 
                    break;
                case "2":
                    // Process direct deposit
                    // Do Somthing
                    $ussd->saveSession(); // End Session
                    break;
                default:
                    // Format Not supported
                    // Do Somthing
                    $ussd->saveSession(); // End Session
                    break;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?