一個用指標函式 ( 或是委派 ) 簡化switch程式碼的方式

  • 2542
  • 0
  • c++
  • 2011-11-10

Use funtion point to condense your "switch".

當我們開發程式

遇到有多種選擇的時候

常常會用到switch來解決這些問題

但是隨著程式碼的修改

選項的增加

switch變的真是越來越樂樂長XD

尤其當你連每個switch選項要做的事情也都寫在一起的時候

程式碼維護起來簡直就是一種災難 >"<

這邊提供一個小方法

大家可以參考一下

 

這是原本的寫法

	#include "stdafx.h"

extern void AppPause();

const int METHOD_MAX = 3;


void Method1(int iParam)
{
    ::printf("\n%s = %d","Method1", iParam);
}


void Method2(int iParam)
{
    ::printf("\n%s = %d","Method2", iParam);
}


void Method3(int iParam)
{
    ::printf("\n%s = %d","Method3", iParam);
}


int _tmain(int argc, _TCHAR* argv[])
{
    for(int i=0; i<METHOD_MAX; i++)
    {
        switch(i)
        {
        case 0:
            ::Method1(i);
            break;

        case 1:
            ::Method2(i);
            break;

        case 2:
            ::Method3(i);
            break;
        }
    }
    
    ::AppPause();
    
    return 0;
}


void AppPause()
{    
    // 不要理我, 我只是讓程式暫停的迴圈=..=a        
    char cPause;        
    do        
    {                
        cPause = getchar();                
        if(cPause == EOF)                        
            break;        
    } while(cPause != '\n');
}

 

 

這是用指標函式的寫法

	#include "stdafx.h"

extern void AppPause();

typedef void (*pfnMethod)(int iParam);

const int METHOD_MAX = 3;


void Method1(int iParam)
{
    ::printf("\n%s = %d","Method1", iParam);
}


void Method2(int iParam)
{
    ::printf("\n%s = %d","Method2", iParam);
}


void Method3(int iParam)
{
    ::printf("\n%s = %d","Method3", iParam);
}


// 以後有新的函式只要在後面再加一個
pfnMethod arr_pfnMethod[] = {Method1, Method2, Method3};


int _tmain(int argc, _TCHAR* argv[])
{
    for(int i=0; i<METHOD_MAX; i++)
        arr_pfnMethod[i](i);
    
    ::AppPause();
    
    return 0;
}


void AppPause()
{    
    // 不要理我, 我只是讓程式暫停的迴圈=..=a        
    char cPause;        
    do        
    {                
        cPause = getchar();                
        if(cPause == EOF)                        
            break;        
    } while(cPause != '\n');
}

 

 

 

當然C#也能用

	using System;

namespace PointFunctionCS
{
    class Program
    {
        private delegate void delMethod(int iParam_);

        private static delMethod[] arr_delMethod = {
                                                        new delMethod(Method1),
                                                        new delMethod(Method2),
                                                        new delMethod(Method3)
                                                    };
        
        private static void Main(string[] args)
        {
            for (int i = 0; i < arr_delMethod.Length; i++)
                arr_delMethod[i](i);

                Console.ReadLine();
        }


        private static void Method1(int iParam_)
        {
            Console.WriteLine("Method1 = " + iParam_.ToString());
        }


        private static void Method2(int iParam_)
        {
            Console.WriteLine("Method2 = " + iParam_.ToString());
        }


        private static void Method3(int iParam_)
        {
            Console.WriteLine("Method3 = " + iParam_.ToString());
        }//method        
    }// class
}// namespace