当前位置:编程学习 > C/C++ >>

适配器模式C语言实现

【代码清单】
 
typedef.h
 
[html] 
#ifndef __TYPEDEF_H__  
#define __TYPEDEF_H__  
  
#include <stdio.h>  
#include <stdlib.h>  
  
#ifdef __cplusplus  
extern "C" {  
#endif  
  
typedef enum _Ret  
{  
    RET_OK,  
    RET_FAIL  
}Ret;  
  
#define return_if_fail(p)\  
    if(!(p)){\  
    printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\  
    return;}  
#define return_val_if_fail(p, ret)\  
    if(!(p)){\  
    printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\  
    return (ret);}  
#define SAFE_FREE(p) if(p != NULL){free(p); p = NULL;}  
  
#ifdef __cplusplus  
}  
#endif  
  
#endif  
 
#ifndef __TYPEDEF_H__
#define __TYPEDEF_H__
 
#include <stdio.h>
#include <stdlib.h>
 
#ifdef __cplusplus
extern "C" {
#endif
 
typedef enum _Ret
{
RET_OK,
RET_FAIL
}Ret;
 
#define return_if_fail(p)\
if(!(p)){\
printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\
return;}
#define return_val_if_fail(p, ret)\
if(!(p)){\
printf("%s:%d Warning:"#p"Failed\n",__func__,__LINE__);\
return (ret);}
#define SAFE_FREE(p) if(p != NULL){free(p); p = NULL;}
 
#ifdef __cplusplus
}
#endif
 
#endif
player.h
 
[html]
#ifndef __PLAYER_H__  
#define __PLAYER_H__  
  
#include "typedef.h"  
  
#ifdef __cplusplus  
extern "C" {  
#endif  
  
struct _Player;  
typedef struct _Player Player;  
  
struct _Player  
{  
    char *name;  
    void (*attack)(void *player);  
    void (*defend)(void *player);  
    void (*destroy)(void *player);  
};  
  
static inline void player_attack(Player *thiz)  
{  
    return_if_fail(thiz != NULL);  
    if(thiz->attack != NULL)  
    {  
        thiz->attack(thiz);  
    }  
}  
  
static inline void player_defend(Player *thiz)  
{  
    return_if_fail(thiz != NULL);  
    if(thiz->defend != NULL)  
    {  
        thiz->defend(thiz);  
    }  
}  
  
static inline void player_destroy(Player *thiz)  
{  
    return_if_fail(thiz != NULL);  
    if(thiz->destroy != NULL)  
    {  
        thiz->destroy(thiz);  
    }  
}  
  
#ifdef __cplusplus  
}  
#endif  
  
#endif  
 
#ifndef __PLAYER_H__
#define __PLAYER_H__
 
#include "typedef.h"
 
#ifdef __cplusplus
extern "C" {
#endif
 
struct _Player;
typedef struct _Player Player;
 
struct _Player
{
char *name;
void (*attack)(void *player);
void (*defend)(void *player);
void (*destroy)(void *player);
};
 
static inline void player_attack(Player *thiz)
{
return_if_fail(thiz != NULL);
if(thiz->attack != NULL)
{
thiz->attack(thiz);
}
}
 
static inline void player_defend(Player *thiz)
{
return_if_fail(thiz != NULL);
if(thiz->defend != NULL)
{
thiz->defend(thiz);
}
}
 
static inline void player_destroy(Player *thiz)
{
return_if_fail(thiz != NULL);
if(thiz->destroy != NULL)
{
thiz->destroy(thiz);
}
}
 
#ifdef __cplusplus
}
#endif
 
#endif
 
forwardsplayer.h
 
[html] 
#ifndef __FORWARDSPLAYER_H__  
#define __FORWARDSPLAYER_H__  
  
#include "player.h"  
  
#ifdef __cplusplus  
extern "C" {  
#endif  
  
struct _Forwards;  
typedef struct _Forwards Forwards;  
  
struct _Forwards  
{  
    Player player;  
};  
  
Forwards *ForwardsCreate(char *name);  
  
#ifdef __cplusplus  
}  
#endif  
  
#endif  
 
#ifndef __FORWARDSPLAYER_H__
#define __FORWARDSPLAYER_H__
 
#include "player.h"
 
#ifdef __cplusplus
extern "C" {
#endif
 
struct _Forwards;
typedef struct _Forwards Forwards;
 
struct _Forwards
{
Player player;
};
 
Forwards *ForwardsCreate(char *name);
 
#ifdef __cplusplus
}
#endif
 
#endif 
 
forwardsplayer.c
 
[html] 
#include <stdio.h>  
#include <stdlib.h>  
#include "forwardsplayer.h"  
  
static void forwards_attack(void *thiz)  
{  
    printf("前锋 %s 进攻\n", ((Forwards *)thiz)->player.name);  
}  
  
static void forwards_defend(void *thiz)  
{  
    printf("前锋 %s 防守\n", ((Forwards *)thiz)->player.name);  
}  
  
static v
补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,