함수 포인터 그룹의 예

3831 단어
이 코드를 보십시오.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
 
enum response_type
{
	DUMP,
	SECOND_CHANCE,
	MARRIAGE
};
 
 
typedef struct
{
	char *name;
	enum response_type type;
}response;
 
 
void dump(response r)
{
	printf("Dear %s, 
", r.name); puts("dump function
"); } void second_chance(response r) { printf("Dear %s,
", r.name); puts("second_chance function
"); } void marriage(response r) { printf("Dear %s,
", r.name); puts("marriage function
"); } int main() { response r[] = { {"Mike", DUMP}, {"Luis", SECOND_CHANCE}, {"Matt", SECOND_CHANCE}, {"William", MARRIAGE} }; int index; for (index = 0; index < 4; index++) { switch (r[index].type) { case DUMP: dump(r[index]); break; case SECOND_CHANCE: second_chance(r[index]); break; case MARRIAGE: marriage(r[index]); break; default: break; } } return 0; }

이 프로그램은 실행할 수 있을 뿐만 아니라 정확하다.그러나 코드에는 대량의 함수 호출이 가득 차서 매번 r의 type에 따라 함수를 호출해야 한다. 이렇게 보면 다음과 같다.
		switch (r.type)
		{
		case DUMP:
			dump(r[index]);
			break;
		case SECOND_CHANCE:
			second_chance(r[index]);
			break;
		case MARRIAGE:
			marriage(r[index]);
			break;
		default:
			break;
		}

이렇게 되면 r의 네 번째 유형을 추가하면 프로그램의 모든 이런 부분을 수정해야 한다.곧 많은 코드가 유지보수를 필요로 할 뿐만 아니라, 이렇게 하면 오류가 발생하기 쉽다.
다음은 함수 포인터 배열을 만들어 위의 코드를 대체하는 방법입니다.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
 
enum response_type
{
	DUMP,
	SECOND_CHANCE,
	MARRIAGE
};
 
 
typedef struct
{
	char *name;
	enum response_type type;
}response;
 
 
void dump(response r)
{
	printf("Dear %s, 
", r.name); puts("dump function
"); } void second_chance(response r) { printf("Dear %s,
", r.name); puts("second_chance function
"); } void marriage(response r) { printf("Dear %s,
", r.name); puts("marriage function
"); } void (*replise[])(response) = { dump, second_chance, marriage }; int main() { response r[] = { {"Mike", DUMP}, {"Luis", SECOND_CHANCE}, {"Matt", SECOND_CHANCE}, {"William", MARRIAGE} }; int index; for (index = 0; index < 4; index++) { replise[r[index].type](r[index]); } return 0; }

보시다시피, 여기는 이미 그렇게 큰 switch 코드가 없습니다.그러나 프로그램은 여전히 위의 초기 프로그램과 같은 실행 결과이다.
 
이렇게 한 줄의 코드가 위의 큰 switch 코드를 대체합니다!
 
그렇다면 r의 네 번째 유형을 늘릴 때의 대가를 살펴보자.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
 
enum response_type
{
	DUMP,
	SECOND_CHANCE,
	MARRIAGE,
	DEAD
};
 
 
typedef struct
{
	char *name;
	enum response_type type;
}response;
 
 
void dump(response r)
{
	printf("Dear %s, 
", r.name); puts("dump function
"); } void second_chance(response r) { printf("Dear %s,
", r.name); puts("second_chance function
"); } void marriage(response r) { printf("Dear %s,
", r.name); puts("marriage function
"); } void dead(response r) { printf("Dear %s,
", r.name); puts("dead function
"); } void (*replise[])(response) = { dump, second_chance, marriage, dead }; int main() { response r[] = { {"Mike", DUMP}, {"Luis", SECOND_CHANCE}, {"Matt", SECOND_CHANCE}, {"William", MARRIAGE}, {"Zeng", DEAD}, }; int index; for (index = 0; index < 5; index++) { replise[r[index].type](r[index]); } return 0; }

비교해 보면 세 군데만 수정하면 대응하는 함수를 추가할 수 있다!유지 보수 대가가 크게 줄었다!

좋은 웹페이지 즐겨찾기