#include <stdio.h>
#include <stdlib.h>
typedef struct _Coffee
{
void (*have_coffee)();
} Coffee;
typedef struct _Tea
{
void (*have_tea)();
} Tea;
void have_black_coffee()
{
printf("have black coffee!\n");
}
void have_white_coffee()
{
printf("have white coffee!\n");
}
void have_red_tea()
{
printf("have red tea!\n");
}
void have_green_tea()
{
printf("have green tea!\n");
}
typedef struct _CoffeeTeaShop
{
Coffee *(*sell_coffee)();
Tea *(*sell_tea)();
} CoffeeTeaShop;
Coffee *sell_black_coffee()
{
Coffee *pCoffee = (Coffee *)malloc(sizeof(Coffee));
if (NULL == pCoffee)
{
return NULL;
}
pCoffee->have_coffee = have_black_coffee;
return pCoffee;
}
Coffee *sell_white_coffee()
{
Coffee *pCoffee = (Coffee *)malloc(sizeof(Coffee));
if (NULL == pCoffee)
{
return NULL;
}
pCoffee->have_coffee = have_white_coffee;
return pCoffee;
}
Tea *sell_red_tea()
C语言实现抽象工厂模式
最新推荐文章于 2024-08-22 15:36:33 发布
这段代码实现了一个动态的咖啡与茶销售系统,包括咖啡和茶的结构体定义,不同口味咖啡和茶的饮用函数,以及售卖不同种类咖啡和茶的函数。系统根据输入类型创建咖啡店对象,售卖指定的咖啡和茶,并调用相应的饮用函数。在main函数中展示了如何使用该系统。

最低0.47元/天 解锁文章
699

被折叠的 条评论
为什么被折叠?



