In programming, If-else and switch-case are very common branching structures that rarely use these control statements in the program. But it cannot be denied that, in some scenarios, because the branching structure is too long, causing the code to be ugly and not easy to maintain, in the "refactoring" book, too long switch statements as a "bad taste." For example, when we deal with data received from the network, often because of too many kinds and write a long paragraph of if-else or Switch-case, the younger brother has been reading other people to deal with the network data code, found that there are more than 50 if-else statements, resulting in a very long function code. So the younger brother on the Internet to see the solution, there are many support the use of if-else, there are a lot of objections, for the opposition, there are various solutions, such as the use of macro shielding if-else or switch code, using the function pointer list and so on. The younger brother here only introduces two methods, one is to use a list of function pointers, and the other is to use polymorphism.
Also hope that you eldest brother elder sister, at the other way to enlighten younger brother, a lot of exchanges.
1. A list of function pointers, using a struct to encapsulate the function pointer and a string indicating the pointer, and then execute the corresponding function by matching the corresponding string.
Test code:
Copy Code
- #include <stdio.h>
#include <string.h>
/*four functions for test*/
void Functiona (int a);
void functionb (int b);
void Functionc (int c);
void Functiond (int d);
/*get the function by key and run it*/
void exec (const char* key,int value);
typedef void (*excefunction) (int);
typedef struct mapfunction{
char* key;
Excefunction fun;
}mapfunction;
Mapfunction Map[4];
int main (void)
{
Map[0].key = "a";
Map[1].key = "B";
Map[2].key = "C";
Map[3].key = "D";
Map[0].fun = &functionA;
Map[1].fun = &functionB;
Map[2].fun = &functionC;
Map[3].fun = &functionD;
Test with changing the keys
EXEC ("B", 1);
return 0;
}
void exec (const char *key, int value) {
int i=0;
for (; i<4;i++) {
if (strcmp (Key,map[i].key) ==0) {
Map[i].fun (value);
Break
}
}
}
void Functiona (int a)
{
printf ("functiona:%d\n", a+1);
}
void functionb (int b)
{
printf ("functionb:%d\n", b+2);
}
void Functionc (int c)
{
printf ("functionc:%d\n", c+3);
}
void Functiond (int d)
{
printf ("functiond:%d\n", d+4);
}
2, using the object-oriented polymorphic mechanism, the implementation of different functions of the subclass inherits the parent class, and then overloads the parent class method, in the overloaded method to implement the specific function.
Main function:
Copy Code
- #include <iostream>
#include <set>
#include <vector>
#include "test.h"
#include "Testa.h"
#include "Testb.h"
using namespace Std;
Std::vector<test*> Testvector;
void exec (const std::string key)
{
size_t i=0;
for (; I<testvector.size (); i++) {
test* test = testvector.at (i);
if (Test->getkey (). Compare (key) ==0) {
Test->test ();
Break
}
}
Do something
}
int main ()
{
cout << "Hello world!" << Endl;
Testvector.push_back (New TestA ("a"));
Testvector.push_back (New Testb ("B"));
EXEC ("B");
return 0;
}
Parent class:
Copy Code
- #ifndef Test_h
#define Test_h
#include <string>
#include <iostream>
Class Test
{
Public
Test (std::string key);
~test ();
Const std::string GetKey ();
virtual void Test () {}
Private
std::string key;
};
#endif//Test_h
#include "test.h"
Test::test (std::string key)
{
This->key = key;
}
Test::~test ()
{
This->key = "";
}
Const std::string Test::getkey ()
{
Return this->key;
}
Sub-Class A:
Copy Code
- #ifndef testa_h
#define Testa_h
#include <string.h>
#include <iostream>
#include "test.h"
Class Testa:public Test
{
Public
TestA (std::string key);
void Test ();
};
#endif//Testa_h
#include "Testa.h"
Testa::testa (std::string key): Test (key) {}
void Testa::test () {
std::cout<< "Testa::test ()";
}
Sub-Class B:
Copy Code
- #ifndef Testb_h
#define Testb_h
#include <string>
#include <iostream>
#include "test.h"
Class Testb:public Test
{
Public
TESTB (std::string key);
void Test ();
};
#endif//Testb_h
#include "Testb.h"
TESTB::TESTB (std::string key): Test (key) {}
void Testb::test ()
{
std::cout<< "Testb::test ()";
}
The younger brother Caishuxueqian, also invites the big God to instruct many.
http://www.qtcn.org/bbs/apps.php?q=diary&a=detail&did=2030&uid=163485
Use function pointers and polymorphism instead of lengthy if-else or switch-case