利用函数指针实现高内聚、低耦合

 作为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又多了一种取值,
只需要添加一组新的函数,修改函数指针数组,原有的函数仍然可以不加改动地复用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值