忘记密码

成功验证手机号即可找回密码

请输入密码
请确认密码
用户登录
请输入密码
忘记密码
注册免费试用
复制成功
请粘贴在新的浏览器或标签页内打开
确定
账户余额不足10元

为了不影响您的正常使用,请及时充值!

我知道了

恭喜您注册成功!
已成功领取账户余额

25 1
立即领取

手机号绑定成功!
已成功领取账户余额

25 1
立即领取
首页> API接口使用问题> C语言如何接入芝麻HTTP代码demo?

C语言如何接入芝麻HTTP代码demo?

API接口使用问题 发布日期 2021-07-09         

*代理模式: 为其他对象提供一种代理以控制对这个对象的访问*/

/*普通代理:  调用者只能访问代理角色,而不能访问真实角色。 调用者只知道代理而

不用知道真实角色,屏蔽了真实角色的变更对高层模块的影响。*

#include

#include

#include


/*抽象游戏者  代理*/

typedef struct _IGamePlayer IGamePlayer;

struct _IGamePlayer

{

    void (*login)(char *pName, char *pPassword, void *);

    void (*killBoss)(void *);

    void (*upgrade)(void *);

    void (*DeleteIGamePlayer)(void *);

};

/*游戏者   具体*/

typedef struct _GamePlayer GamePlayer;

struct _GamePlayer

{

    IGamePlayer gmplayer;   /*继承抽象游戏者*/

    char acName[10];        /*游戏者姓名*/

    void (*DeleteGamePlayer)(GamePlayer *pGamePlayer);

};



void login(char *pName, char *pPassword, void *pGamePlayer)

{

    printf("user :%s is login in !\n", pName);

}



void killBoss(void *pGamePlayer)

{

    printf("%s is killing the boss! \n", ((GamePlayer *)pGamePlayer)->acName);

}

void upgrade(void *pGamePlayer)

{

    printf("%s is upgrading !\n", ((GamePlayer *)pGamePlayer)->acName);

}



void DeleteGamePlayer(GamePlayer *pGamePlayer)

{

    if(pGamePlayer)

    {

        free(pGamePlayer);

        pGamePlayer = NULL;

    }

}

GamePlayer *CreateGamePlayer(IGamePlayer *pIGamePlayer, char *pName)

{

    GamePlayer *pGamePlayer = NULL;

    if(!pIGamePlayer)

    {

        return NULL;

    }

    else

    {

        pGamePlayer = (GamePlayer *)malloc(sizeof(GamePlayer));

        if(!pGamePlayer)

        {

            return NULL;

        }

        memset(pGamePlayer, 0, sizeof(GamePlayer));

        strcpy(pGamePlayer->acName, pName);

        pGamePlayer->DeleteGamePlayer = DeleteGamePlayer;

        pGamePlayer->gmplayer.login = login;

        pGamePlayer->gmplayer.killBoss = killBoss;

        pGamePlayer->gmplayer.upgrade = upgrade;

        pGamePlayer->gmplayer.DeleteIGamePlayer = pGamePlayer->DeleteGamePlayer;

        return pGamePlayer;

    }

}


/*普通代理的代理者*/

typedef struct _GamePlayerProxy GamePlayerProxy;

struct _GamePlayerProxy

{

    IGamePlayer gmplayer;   /*继承抽象游戏者*/

    GamePlayer *pGamePlayer; /*被代理者*/

    void (*DeleteGamePlayerProxy)(GamePlayerProxy *pGamePlayerProxy);

};



void ProxykillBoss(void * pGamePlayerProxy)

{

((GamePlayerProxy *)pGamePlayerProxy)->pGamePlayer->gmplayer.killBoss(((GamePlayerProxy *)pGamePlayerProxy)->pGamePlayer);    /*代理执行被代理者的方法*/

}



void Proxylogin(char *pName, char *pPassword, void *pGamePlayerProxy)

{

    ((GamePlayerProxy *)pGamePlayerProxy)->pGamePlayer->gmplayer.login(pName, pPassword, pGamePlayerProxy);

}

void Proxyupgrade(void *pGamePlayerProxy)

{

    ((GamePlayerProxy *)pGamePlayerProxy)->pGamePlayer->gmplayer.upgrade(((GamePlayerProxy *)pGamePlayerProxy)->pGamePlayer);

}



void DeleteGamePlayerProxy(GamePlayerProxy *pGamePlayerProxy)

{

    if(pGamePlayerProxy->pGamePlayer)

    {

        pGamePlayerProxy->pGamePlayer->DeleteGamePlayer(pGamePlayerProxy->pGamePlayer);

    }

    if(pGamePlayerProxy)

    {

        free(pGamePlayerProxy);

        pGamePlayerProxy = NULL;

    }

    return;

}

GamePlayerProxy *CreateProxy(char *pName)

{

    GamePlayerProxy *pGamePlayerProxy = NULL;



    pGamePlayerProxy = (GamePlayerProxy *)malloc(sizeof(GamePlayerProxy));

    if(!pGamePlayerProxy)

    {

        return NULL;

    }

    memset(pGamePlayerProxy, 0, sizeof(GamePlayerProxy));



    pGamePlayerProxy->pGamePlayer = CreateGamePlayer(

                            (IGamePlayer *)pGamePlayerProxy, pName);  /*创建被代理者*/



    pGamePlayerProxy->DeleteGamePlayerProxy = DeleteGamePlayerProxy;

    pGamePlayerProxy->gmplayer.login = Proxylogin;

    pGamePlayerProxy->gmplayer.killBoss = ProxykillBoss;

    pGamePlayerProxy->gmplayer.upgrade = Proxyupgrade;

    pGamePlayerProxy->gmplayer.DeleteIGamePlayer = pGamePlayerProxy->DeleteGamePlayerProxy;

    return pGamePlayerProxy;

}

/*场景*/

void main()

{

    /*定义一个代理者*/

    IGamePlayer *pPlayProxy = (IGamePlayer *)CreateProxy("zhangsan"); //转换为抽象类, 即场景只依赖于抽象类



    /*开始打游戏,记下时间戳*/

    printf("game start time is : 2014-1-21 9:00\n");



    /*代理登录*/

    pPlayProxy->login("zhangsan", "password", pPlayProxy);



    /*代理打怪*/

    pPlayProxy->killBoss(pPlayProxy);



    /*代理升级*/

    pPlayProxy->upgrade(pPlayProxy);

 /*游戏结束*/

    printf("game end time is :2014-1-21 10:00");



    /*释放资源*/

    pPlayProxy->DeleteIGamePlayer(pPlayProxy);

}


  • 关注公众号享更多福利

  • 微信扫码联系客服

  • VIP专属在线咨询通道:2852367132[点击咨询]

    QQ号码客服:1583070235

    客户经理:18351866771

    套餐购买相关问题解说集 [查看详情]

1万IP

18552891303

扫微信客服
咨询产品

售后

18068702572

扫微信客服
咨询产品

客户
定制

客户定制

18552891303

18905201382

扫微信客服
咨询产品

大客户套餐服务

18115265820

18115265820

扫微信客服
咨询产品

置顶

关注芝麻HTTP公众号
获取产品最新咨询

扫微信客服
获取产品最新咨询