Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3108981
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-02-12 15:32:06

include
#include



//烤肉师傅类,只负责烤串工作
struct Barbecuer
{
    char name[10];
    void (*BakeMutton)();
    void (*BakeChickenWing)();
};
//执行命令
void BakeMutton(void)
{
   printf("Bake mutton\n");
}
void BakeChickenWing(void)
{
   printf("Bake ChickenWing\n");
}



struct ICommand_vtbl
{
    void(*ExecuteCommand)(struct ICommand *);
};
struct ICommand
{
    struct ICommand_vtbl *vptr;
  //  char name[20];
    struct Barbecuer *p_receiver;
};
void based_dance(void)
{

}
void ICommand_Execute(struct ICommand *pb)
{
    pb->vptr->ExecuteCommand(pb);
}
struct ICommand_vtbl ICommand_table =
{
        based_dance
};

void ICommand_init(struct ICommand *pb)
{
    pb->vptr = &ICommand_table;
}




//具体命令类:烤羊肉串命令
struct BakeMuttonCommand
{
    struct ICommand super;
};

//抽象命令类:是执行具体操作的接口
void ExecuteCommand_BakeMutton(struct BakeMuttonCommand *p)
{
     struct BakeMuttonCommand *sp_receiver;
    sp_receiver=(struct BakeMuttonCommand *)malloc(sizeof( struct BakeMuttonCommand));
    sp_receiver=p;
 //   printf("%s\n","BakeMutton");
    sp_receiver->super.p_receiver->BakeMutton();
}

struct ICommand_vtbl BakeMutton_table=
{
    (void(*)(struct ICommand *))&ExecuteCommand_BakeMutton

};


struct BakeMuttonCommand *creat_BakeMuttonCommand(struct BakeMuttonCommand *pd,struct Barbecuer *p)
{
   struct BakeMuttonCommand *sp_command;
   sp_command=(struct BakeMuttonCommand *)malloc(sizeof(struct BakeMuttonCommand));
   sp_command=pd;
   sp_command->super.vptr= &BakeMutton_table;
   sp_command->super.p_receiver=p;


   return sp_command;
}
//具体命令类:烤鸡翅串命令
struct BakeChickenWingCommand
{
    struct ICommand super;
};
void ExecuteCommand_BakeChickenWing(struct BakeChickenWingCommand *p)
{
    struct BakeChickenWingCommand *sp_receiver;
    sp_receiver=(struct Barbecuer *)malloc(sizeof( struct Barbecuer));
    sp_receiver=p;
  //  printf("%s\n","BakeChickenWing");
    sp_receiver->super.p_receiver->BakeChickenWing();
}
struct ICommand_vtbl BakeChickenWing_table=
{
    (void(*)(struct ICommand *))&ExecuteCommand_BakeChickenWing

};
 struct BakeChickenWingCommand * creat_BakeChickenWingCommand(struct BakeChickenWingCommand *pd,struct Barbecuer *p)
{

   struct BakeChickenWingCommand *sp_command;
   sp_command=(struct BakeChickenWingCommand *)malloc(sizeof(struct BakeChickenWingCommand));
   sp_command=pd;
   pd->super.vptr= &BakeChickenWing_table;
 //  strcpy(sp_command->super.name,command_name);
   pd->super.p_receiver=p;

    return sp_command;
}


//服务员类
struct ICommand * p_commandList[10];
struct Waiter
{
    struct Waiter * (*SetOrder)(struct ICommand *command);
    void (*Notify)(void);
     //这里相当于一个命令对象队列

};
int Command_num=0;

void Waiter_SetOrder(struct ICommand *command)
{
  //  int i;
 //   struct Waiter *p;
 //   p=(struct Waiter *)malloc(2*sizeof(struct Waiter));
    p_commandList[Command_num]=command;
    Command_num++;

}

void Waiter_Notify(void)
{
  //  struct Waiter *sp;
 //   sp=(struct Waiter *)malloc(sizeof( struct Waiter));
 //   sp=p;
      while (Command_num--)
    {
      ICommand_Execute(p_commandList[Command_num]);
    }

}
struct Waiter *creat_Waiter(void)
{
    struct Waiter * sp_waiter;
    sp_waiter=(struct Waiter *)malloc(sizeof(struct Waiter));
    sp_waiter->SetOrder=Waiter_SetOrder;
    sp_waiter->Notify=Waiter_Notify;

    return sp_waiter;

}
struct Barbecuer * creat_Barbecuer(char *name)
{
    struct Barbecuer *sp_cook;
    sp_cook=( struct Barbecuer *)malloc(sizeof(struct Barbecuer));
    strcpy(sp_cook->name,name);
    sp_cook->BakeMutton=BakeMutton;
    sp_cook->BakeChickenWing=BakeChickenWing;

    return sp_cook;
}
int main(int argc, char *argv[])
{
    //生成烤肉师傅、服务员、订单对象
    struct Barbecuer *p_cook;
    p_cook=creat_Barbecuer("zhang");
 //   printf("%s\n",p_cook->name);

    struct ICommand b;
    ICommand_init(&b);
    struct BakeMuttonCommand *p_mutton;
    struct BakeChickenWingCommand *p_chickenwing;
    p_mutton=(struct BakeMuttonCommand *)malloc(sizeof(struct BakeMuttonCommand));
    p_chickenwing=(struct BakeChickenWingCommand *)malloc(sizeof(struct BakeChickenWingCommand));

    p_mutton=creat_BakeMuttonCommand(p_mutton,p_cook);
 //   printf("%s\n",p_mutton->super.p_receiver->name);
    p_chickenwing=creat_BakeChickenWingCommand(p_chickenwing,p_cook);
 //   printf("%s\n",p_chickenwing->m.name);
    struct ICommand *b1_ptr = (struct ICommand *)p_mutton;
    struct ICommand *b2_ptr = (struct ICommand *)p_chickenwing;

    struct Waiter *p_waiter;
    p_waiter=creat_Waiter();
 //   p_waiter=creat_Waiter();
//    struct ICommand *p_ICommand;
  //  p_ICommand=(struct ICommand *)malloc(sizeof(struct ICommand));

 //   ICommand_Execute(b1_ptr);
  //  ICommand_Execute(p_waiter->*());
    //将订单对象推送到命令队列
    p_waiter->SetOrder(b1_ptr);
    p_waiter->SetOrder(b2_ptr);
 //   ICommand_Execute(b1_ptr);
    ICommand_Execute(p_commandList[0]);
    ICommand_Execute(p_commandList[1]);
//printf("%s\n",p_chickenwing->m.name);
    //服务员通知烤肉师傅具体订单
    p_waiter->Notify();

  //  SAFE_DELETE(p_cook);
  //  SAFE_DELETE(p_mutton);
 //  SAFE_DELETE(p_chickenwing);
 //   SAFE_DELETE(p_waiter);

    return 0;
}
阅读(1704) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~