作为c的初学者,如果写一个求四边形的周长的函数的话,我倾向于像下面这样书写:
//计算四边形的周长
#include <stdio.h>
#define RECTANGLE 0 //长方形
#define SQUARE 1 //正方形
//多边形
struct polygon{
int type;
int length;
int width;
};
int get_circumference(struct polygon pygn)
{
if(pygn.type == RECTANGLE)
{
return 2 * (pygn.length + pygn.width);
}
else if(pygn.type == SQUARE)
{
return 4 * pygn.width;
}
else
{
//todo
}
}
int main(void)
{
struct polygon pygn_rec, pygn_squ;
pygn_rec.type = RECTANGLE;
pygn_rec.width = 6;
pygn_rec.length = 10;
pygn_squ.type = SQUARE;
pygn_squ.width = 6;
printf("get cir rec is: %d\n", get_circumference(pygn_rec));
printf("get cir squ is: %d\n", get_circumference(pygn_squ));
return 0;
}
今天,看了<linux c编程一站式学习>的“函数类型和函数指针”(http://learn.akae.cn/media/ch23s08.html)后,又学到了一种更好的方式:
//计算四边形的周长
#include <stdio.h>
#define RECTANGLE 0 //长方形
#define SQUARE 1 //正方形
//多边形
struct polygon{
int type;
int length;
int width;
};
int get_rectangle_cir(struct polygon pygn)
{
return 2 * (pygn.length + pygn.width);
}
int get_square_cir(struct polygon pygn)
{
return 4 * pygn.width;
}
int (* get_circumference[])(struct polygon pygn) = {get_rectangle_cir, get_square_cir};
#define get_cir(pygn) get_circumference[pygn.type](pygn)
int main(void)
{
struct polygon pygn_rec, pygn_squ;
pygn_rec.type = RECTANGLE;
pygn_rec.width = 6;
pygn_rec.length = 10;
pygn_squ.type = SQUARE;
pygn_squ.width = 6;
printf("get cir rec is: %d\n", get_cir(pygn_rec));
printf("get cir squ is: %d\n", get_cir(pygn_squ));
return 0;
}
当调用get_cir(pygn)时,用类型字段pygn.type做索引,从指针数组get_circumference中取出相应的函数指针来调用,
也可以达到if ... else ...的效果,但相比之下这种实现更好,每个函数都只做一件事情,
而不必用if ... else ...兼顾好几件事情,比如get_rectangle_cir和get_square_cir各做各的,
互相独立,而不必把它们的代码都耦合到一个函数中。
“低耦合,高内聚”(Low Coupling, High Cohesion)是程序设计的一条基本原则,
这样可以更好地复用现有代码,使代码更容易维护。如果类型字段pygn.type又多了一种取值,
只需要添加一组新的函数,修改函数指针数组,原有的函数仍然可以不加改动地复用。