switch-case a new way

来源:互联网 发布:c语言 随机数 编辑:程序博客网 时间:2024/05/21 12:35
This design pattern solves the problem of having to implement huge switch - case statements and makes dynamic switch - case not dependent on the number of cases.
  • Download source - 85.3 Kb

Overview

This design pattern solves the problem of having to implement huge switch - case statements and makes dynamic switch - case not dependent on the number of cases.

Problem Description

If we have a program used for interpreting and executing commands, as like a Linux shell, and if we assume that we have 300 commands that needs to interpreted and executed, we would need to make a switch - case statement containing 300 cases, and what if we have more than 300 commands? Then, we would need more than 300 cases to handle all those commands. Here, we make a Design Pattern to solve this problem.

Design Pattern Steps

Step 1

Create for every case, only one separate function to handle all the logic of the case.

Collapse | Copy Code
void  HandleCase1 (int caseNumber) {    printf(" The Handled Case Number Is = %d",caseNumber);}void  HandleCase2 (int caseNumber) {    printf(" The Handled Case Number Is = %d",caseNumber);}void  HandleCase3 (int caseNumber) {    printf(" The Handled Case Number Is = %d",caseNumber);}

Put all of above function into a separate file and name it as HandleCases.cpp.

Step 2

Compile the above file as a shared object (dynamic library) using the following commands:

  1. g++ -c -fpic HandleCases.cpp

    The above command generates an object file HandleCases.o.

  2. g++ -shared -lc -o HandleCases.so HandleCases.o

    Now, we have a dynamic library containing all the case implementations.

Step 3

Create your main application where we will use this library instead of switch-cases.

  1. Define a handle to our dynamic library.
    Collapse | Copy Code
    void* FunctionLib;
  2. Define a pointer to the called function.
    Collapse | Copy Code
    void  (*Function)(int);
  3. Open the dynamic library using the dlopen function.
    Collapse | Copy Code
    FunctionLib = dlopen("dynamic library path", RTLD_LAZY);
  4. Get the desired function pointer using the dlsym function.
    Collapse | Copy Code
    Function =(void(*)(int))dlsym( FunctionLib, "HandleCase1");
  5. Call this function using the above function pointer.
    Collapse | Copy Code
    (*Function)(1);
  6. Finally, close the dynamic library.
    Collapse | Copy Code
    dlclose(FunctionLib);

Complete Code of Step 3

Collapse | Copy Code
#include <stdlib.h>#include <stdio.h>#include <dlfcn.h> // int main(int argc,char*argv[]){    // handle to Dynamic Libary    void* FunctionLib;     // Pointer to called function    void  (*Function)(int);     // Pointer to read the error    char *error;     // Open Dynamic Loadable Libary with absolute path     FunctionLib = dlopen("HandleCases.so",RTLD_LAZY);        if ((error = dlerror()) != NULL)    {        printf ("%s /n", error);        exit(1);    }    // point to our function need to call    Function =(void(*)(int))dlsym( FunctionLib, "HandleCase1");     if ((error = dlerror()) != NULL)     {        printf ("%s/n", error);        exit(1);    }    // call our function    (*Function)(1);    // close Dynamic libary    dlclose(FunctionLib);     return (0);}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝代购是假货怎么办 闲鱼对方不在怎么办 小米商城退款慢怎么办 小米手机第三方拿货是怎么办 oppo手机卡被锁怎么办 下巴粉刺特别多怎么办 苹果6sp手机卡怎么办 苹果手机无服务怎么办 京东买电脑没发票怎么办 买东西发票丢了怎么办 在天猫上买了假货怎么办 苹果发票丢了怎么办 iphone8屏幕摔了怎么办 在手机店买到翻新机怎么办 信用卡网上买东西退款怎么办 在唯品会买到假的护肤品怎么办 天猫买东西发票怎么办 支付宝无法收款怎么办 买到苹果假货怎么办 16周岁怎么办手机分期 淘宝打假扣分了怎么办 买到不合格食品怎么办 买到不合格面膜怎么办 买假货怎么处理怎么办 咸鱼买手机被骗怎么办 华为p9超级卡怎么办 花呗分期退货怎么办 华为荣耀10网咯好卡怎么办 官网买手机坏了怎么办 三星c5发热严重怎么办 iphone7一直重启怎么办 预售商品不发货怎么办 vivo手机跑电快怎么办 手机跑电太快了怎么办 手机home键失灵怎么办 小米手机发货慢怎么办 京东卡1元流量怎么办 苹果手机系统太大怎么办 苹果电池用电快怎么办 苹果6手机漏电怎么办 iphone7一直e网怎么办