OOPC—接口的应用实例

接口的最后一节,应用实例,本来昨天就学习了,程序也在VC6.0上验证通过了,在书中举的例子是现实生活中手电筒,可能现在没有这种手电筒了,我小时候家里经常使用,用两节电池装在手电桶里,不过得经常更换电池,每次老爸把旧的电池换下来我就把中间的碳棒拆下来,碳棒能干啥呢?农村成长的我们也许会知道,可以用碳棒写字,当然不是在作业本上写,是在院子里写字。那时候作业本也贵,只能想不要钱的办法、、、好了不回念过去了,苦啊、、、、、
下面我们将要实现一个电池接口,供不同类型的电池使用,同样实现一个手电筒的接口,供手电筒使用;
1、电池接口

#ifndef __IC_H__
#define __IC_H__

typedef struct ICell_t ICell_t;

struct ICell_t
{
	void (*init)(void *);
	void (*SetLinkToNext)(void*, ICell_t*);
	int (*GetPower)(void*);
	
};

#endif

说明:电池是串联在手电筒里的,因此有SetLinkToNext函数;
2、手电筒接口

#ifndef __IL_H__
#define __IL_H__

typedef struct
{
	void (*init)(void*);
	void (*AddCell)(void*, void*);
	int	(*Power)(void*);
	
}ILight_t;
#endif

说明:手电筒可以装一节一节的电池,因此使用了AddCell函数;

3、电池类,下面有两个不同厂家的不同类型的电池,他们的厂家不同,电量也不同,但是使用同一个电池的接口;

#ifndef __PAN_H__
#define __PAN_H__

#include "c15ic.h"

typedef struct
{
	ICell_t panCell;
	int pw;
	ICell_t* next_cell;
}PAN_t;

void *panNew();
#endif
/*********************************************/
#include <stdlib.h>
#include "c15pan.h"

static void init(void *t)
{
	PAN_t* cthis = (PAN_t*)t;

	cthis->next_cell = NULL;
	cthis->pw = 10;
}

static void SetLink(void *t, ICell_t* nc)
{
	PAN_t* cthis = (PAN_t*)t;

	cthis->next_cell = nc;
}

static int GetPower(void *t)
{
	PAN_t* cthis = (PAN_t*)t;
	ICell_t *pc;

	//判断当前电池数量
	if (cthis->next_cell == NULL)
	{//当前只有一节电池
		return cthis->pw;
	}
	else
	{//大于一节电池
		pc = cthis->next_cell;

		return (cthis->pw + pc->GetPower(pc));
	}
}

void *panNew()
{
	PAN_t* t;
	t = (PAN_t *)malloc(sizeof(PAN_t));

	t->panCell.GetPower = GetPower;
	t->panCell.init = init;
	t->panCell.SetLinkToNext = SetLink;

	return (void*)t;
}

#ifndef __CAT_H__
#define __CAT_H__

#include "c15ic.h"
typedef struct
{
	ICell_t catCell;
	int pw;
	ICell_t *next_cell;
}CAT_t;

void *catNew();
#endif
/***********************************************/
#include <stdlib.h>
#include "c15cat.h"

static void init(void *t)
{
	CAT_t *cthis = (CAT_t*)t;

	cthis->pw = 7;
	cthis->next_cell = NULL;
}

static void SetLink(void * t, ICell_t * nc)
{
	CAT_t *cthis = (CAT_t*)t;

	cthis->next_cell = nc;
}

static int GetPower(void * t)
{
	CAT_t *cthis = (CAT_t *)t;
	ICell_t *pa;

	if (cthis->next_cell == NULL)
	{
		return cthis->pw;
	}
	
	else
	{
		pa = cthis->next_cell;
		return (cthis->pw + pa->GetPower(pa));
	}
}

void *catNew()
{
	CAT_t* t;
	t = (CAT_t*)malloc(sizeof(CAT_t));

	t->catCell.init = init;
	t->catCell.SetLinkToNext = SetLink;
	t->catCell.GetPower = GetPower;

	return (void*)t;
}

4、实现灯的类,灯类中最重要的是将电池装入灯通中,只要你灯通足够长,可以一直装入,最后可以计算出所有电池的电量和;

#ifndef __LIG_H__
#define __LIG_H__

#include "c15ic.h"
#include "c15il.h"

typedef struct
{
	ILight_t light;
	ICell_t *head,*tail;
	
}FlashLigh_t;

#endif
/************************************************/
#include <stdlib.h>
#include "c15lig.h"

static void init(void *t)
{
	FlashLigh_t * cthis = (FlashLigh_t*)t;

	cthis->head = NULL;
	cthis->tail = NULL;
}

static void AddCell(void *t, ICell_t* cell)
{
	ICell_t *pc;
	FlashLigh_t *cthis = (FlashLigh_t *)t;

	if (cthis->head == NULL)
	{
		cthis->head = cell;
		cthis->tail = cthis->head;
	}
	else
	{
		pc = cthis->tail;
		pc->SetLinkToNext(pc,cell);
		cthis->tail = cell;
	}
}

static int Power(void *t)
{
	FlashLigh_t* cthis = (FlashLigh_t *)t;
	ICell_t *pc = cthis->head;

	return pc->GetPower(pc);
}

void *flashLightNew(void)
{
	FlashLigh_t *t;
	t = (FlashLigh_t*)malloc(sizeof(FlashLigh_t));

	t->light.AddCell = AddCell;
	t->light.init = init;
	t->light.Power = Power;

	return (void *)t;
}

5、实现应用函数,初始化具体的对象,将电池对象装入灯的具体对象中,最后get出所有电池的电量和;

#include <stdio.h>
#include <stdlib.h>
#include "c15cat.h"
#include "c15lig.h"
#include "c15pan.h"
#include "c15ic.h"
#include "c15il.h"

int main()
{
	ILight_t *light;
	ICell_t *pan1,*pan2,*cat1;
	int pow;

	light = (ILight_t*)flashLightNew();
	light->init(light);

	pan1 = panNew();
	pan1->init(pan1);

	pan2 = panNew();
	pan2->init(pan2);

	cat1 = catNew();
	cat1->init(cat1);

	light->AddCell(light,pan1);
	light->AddCell(light,pan2);
	light->AddCell(light,cat1);

	pow = light->Power(light);

	printf("Power = %d\n",pow);

	return 0;
		

在VC6.0中运行,得出最后的值:27。
好了,太累了,明天还得上班,连续7天、、、醉了!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeartRain_大西瓜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值