设为首页 加入收藏

TOP

适配器模式C语言实现(二)
2013-07-22 17:55:55 来源: 作者: 【 】 浏览:242
Tags:适配器 模式 语言 实现

 

  guardsplayer.h

  [html] v

  #ifndef __GUARDSPLAYER_H__

  #define __GUARDSPLAYER_H__

  #include "player.h"

  #ifdef __cplusplus

  extern "C" {

  #endif

  struct _Guards;

  typedef struct _Guards Guards;

  struct _Guards

  {

  Player player;

  };

  Guards *GuardsCreate(char *name);

  #ifdef __cplusplus

  }

  #endif

  #endif

  #ifndef __GUARDSPLAYER_H__

  #define __GUARDSPLAYER_H__

  #include "player.h"

  #ifdef __cplusplus

  extern "C" {

  #endif

  struct _Guards;

  typedef struct _Guards Guards;

  struct _Guards

  {

  Player player;

  };

  Guards *GuardsCreate(char *name);

  #ifdef __cplusplus

  }

  #endif

  #endif

  guardsplayer.c

  [html]

  #include <stdio.h>

  #include <stdlib.h>

  #include "guardsplayer.h"

  static void guards_attack(void *thiz)

  {

  printf("后卫 %s 进攻\n", ((Guards *)thiz)->player.name);

  }

  static void guards_defend(void *thiz)

  {

  printf("后卫 %s 防守\n", ((Guards *)thiz)->player.name);

  }

  static void guards_destroy(void *thiz)

  {

  return_if_fail(thiz !=  NULL);

  SAFE_FREE(thiz);

  }

  Guards *GuardsCreate(char *name)

  {

  Guards *thiz = malloc(sizeof(Guards));

  if(thiz != NULL)

  {

  thiz->player.name = name;

  thiz->player.attack = guards_attack;

  thiz->player.defend = guards_defend;

  thiz->player.destroy = guards_destroy;

  }

  return thiz;

  }

  #include <stdio.h>

  #include <stdlib.h>

  #include "guardsplayer.h"

  static void guards_attack(void *thiz)

  {

  printf("后卫 %s 进攻\n", ((Guards *)thiz)->player.name);

  }

  static void guards_defend(void *thiz)

  {

  printf("后卫 %s 防守\n", ((Guards *)thiz)->player.name);

  }

  static void guards_destroy(void *thiz)

  {

  return_if_fail(thiz !=  NULL);

  SAFE_FREE(thiz);

  }

  Guards *GuardsCreate(char *name)

  {

  Guards *thiz = malloc(sizeof(Guards));

  if(thiz != NULL)

  {

  thiz->player.name = name;

  thiz->player.attack = guards_attack;

  thiz->player.defend = guards_defend;

  thiz->player.destroy = guards_destroy;

  }

  return thiz;

  }

  foreigncenterplayer.h

  [html]

  #ifndef __FORIGNCENTERPLAYER_H__

  #define __FORIGNCENTERPLAYER_H__

  #include "typedef.h"

  #ifdef __cplusplus

  extern "C" {

  #endif

  struct _ForeignCenter;

  typedef struct _ForeignCenter ForeignCenter;

  struct _ForeignCenter

  {

  char *name;

  void (*fattack)(void *player);

  void (*fdefend)(void *player);

  void (*fdestroy)(void *player);

  };

  void foreign_center_attack(void *thiz);

  void foreign_center_defend(void *thiz);

  void foreign_center_destroy(void *thiz);

  #ifdef __cplusplus

  }

  #endif

  #endif

  #ifndef __FORIGNCENTERPLAYER_H__

  #define __FORIGNCENTERPLAYER_H__

  #include "typedef.h"

  #ifdef __cplusplus

  extern "C" {

  #endif

  struct _ForeignCenter;

  typedef struct _ForeignCenter ForeignCenter;

  struct _ForeignCenter

  {

  char *name;

  void (*fattack)(void *player);

  void (*fdefend)(void *player);

  void (*fdestroy)(void *player);

  };

  void foreign_center_attack(void *thiz);

  void foreign_center_defend(void *thiz);

  void foreign_center_destroy(void *thiz);

  #ifdef __cplusplus

  }

  #endif

  #endif

  foreigncenterplayer.c

  [html]

  #include <stdio.h>

  #include <stdlib.h>

  #include "foreigncenterplayer.h"

  void foreign_center_attack(void *thiz)

  {

  printf("外籍中锋 %s 进攻\n", ((ForeignCenter *)thiz)->name);

  }

  void foreign_center_defend(void *thiz)

  {

  printf("外籍中锋 %s 防守\n", ((ForeignCenter *)thiz)->name);

  }

  void foreign_center_destroy(void *thiz)

  {

  return_if_fail(thiz !=  NULL);

  SAFE_FREE(thiz);

  }

  static ForeignCenter *ForeignCenterCreate(char *name)

  {

  ForeignCenter *thiz = malloc(sizeof(ForeignCenter));

  if(thiz != NULL)

  {

  thiz->name = name;

  thiz->fattack = foreign_center_attack;

  thiz->fdefend = foreign_center_defend;

  thiz->fdestroy = foreign_center_destroy;

  }

  return thiz;

  }

  #include <stdio.h>

  #include <stdlib.h>

  #include "foreigncenterplayer.h"

  void foreign_center_attack(void *thiz)

  {

  printf("外籍中锋 %s 进攻\n", ((ForeignCenter *)thiz)->name);

  }

  void foreign_center_defend(void *thiz)

  {

  printf("外籍中锋 %s 防守\n", ((ForeignCenter *)thiz)->name);

  }

  void foreign_center_destroy(void *thiz)

  {

  return_if_fail(thiz !=  NULL);

  SAFE_FREE(thiz);

  }

  static ForeignCenter *ForeignCenterCreate(char *name)

  {

  ForeignCenter *thiz = malloc(sizeof(ForeignCenter));

  if(thiz != NULL)

  {

  thiz->name = name;

  thiz->fattack = foreign_center_attack;

  thiz->fdefend = foreign_center_defend;

  thiz->fdestroy = foreign_center_destroy;

  }

  return thiz;

  }

  foreigncenteradapter.h

  [html]

  #ifndef __FORIGNCENTERADAPTER_H__

  #define __FORIGNCENTERADAPTER_H__

  #include "player.h"

  #ifdef __cplusplus

  extern "C" {

  #endif

  struct _ForeignCenterAdapter;

  typedef struct _ForeignCenterAdapter ForeignCenterAdapter;

  struct _ForeignCenterAdapter

  {

  Player player;

  };

  ForeignCenterAdapter *ForeignCenterAdapterCreate(char *name);

  #ifdef __cplusplus

  }

  #endif

  #endif

  #ifndef __FORIGNCENTERADAPTER_H__

  #define __FORIGNCENTERADAPTER_H__

  #include "player.h"

  #ifdef __cplusplus

  extern "C" {

  #endif

  struct _ForeignCenterAdapter;

  typedef struct _ForeignCenterAdapter ForeignCenterAdapter;

  struct _ForeignCenterAdapter

  {

  Player player;

  };

  ForeignCenterAdapter *ForeignCenterAdapterCreate(char *name);

  #ifdef __cplusplus

  }

  #endif

  #endif

  foreigncenteradapter.c

  [html]

  #include <stdio.h>

  #include <stdlib.h>

  #include "foreigncenterplayer.h"

  #include "foreigncenteradapter.h"

  static void foreign_center_adapter_attack(void *thiz)

  {

  foreign_center_attack(thiz);

  }

  static void foreign_center_adapter_defend(void *thiz)

  {

  foreign_center_defend(thiz);

  }

  static void foreign_center_adapter_destroy(void *thiz)

  {

        

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇设置缓存失效的三种方式 下一篇编码风格 - 缩进和空白

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: