Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C++
Article

Dynamic Switch-Case Design Pattern

Rate me:
Please Sign up or sign in to vote.
1.12/5 (23 votes)
9 Jan 20061 min read 54.5K   193   13   5
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.

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.

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.
    void* FunctionLib;
  2. Define a pointer to the called function.
    void  (*Function)(int);
  3. Open the dynamic library using the dlopen function.
    FunctionLib = dlopen("dynamic library path", RTLD_LAZY);
  4. Get the desired function pointer using the dlsym function.
    Function =(void(*)(int))dlsym( FunctionLib, "HandleCase1");
  5. Call this function using the above function pointer.
    (*Function)(1);
  6. Finally, close the dynamic library.
    dlclose(FunctionLib);

Complete Code of Step 3

#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.

A list of licenses authors might use can be found here


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMore discussion needed Pin
Khazbak27-Jan-06 4:24
Khazbak27-Jan-06 4:24 
GeneralWhy I voted you a 1 Pin
Jim Crafton11-Jan-06 5:49
Jim Crafton11-Jan-06 5:49 
First, the "article" barely covers the topic. I have no problem with the code being written for linux, but you don't explain what the hell is going on.
Second, I fail to see where the pattern is, or how you accomplish it based on this article.
Third, there are standard ways to do this (as mentioned by others) and I don't see what your method gains. In other words, why are you using virtual methods in a C++ class?

Basically it's just a sucky article.

¡El diablo está en mis pantalones! ¡Mire, mire!

Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!

SELECT * FROM User WHERE Clue > 0
0 rows returned

Save an Orange - Use the VCF!
GeneralCommand design pattern Pin
Daniel Turini9-Jan-06 9:43
Daniel Turini9-Jan-06 9:43 
Questionhuh ? Pin
Maximilien9-Jan-06 9:37
Maximilien9-Jan-06 9:37 
AnswerRe: huh ? Pin
Jon Weir10-Jan-06 3:21
Jon Weir10-Jan-06 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.