factory c语言实现设计模式之工厂模式

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*
* 工厂模式
* 想要扩展加盟店,只需要在FactoryInit增加不同地域风味的pizza创建方式
* 不同地域风味的pizza店想要增加口味,只要pfPZCreate 中增加风味
*
*/
typedef enum tag_PZ_TYPE_E
{
PZ_CHEESE,
PZ_PEPPERON,
PZ_CLAM,
PZ_VEGGIE
}PZ_TYPE_E;
typedef enum tag_PZ_STYLE_E
{
NY_STYLE_E,
CHI_STYLE_E
}PZ_STYLE_E;
typedef struct tag_PIZZA_S
{
void (*pfPrepare)(void);
void (*pfBake)(void);
void (*pfCut)(void);
void (*pfBox)(void);
}PIZZA_S;
typedef PIZZA_S * (*PF)(PZ_TYPE_E);
typedef struct tag_PZ_FACTORY_S
{
PF pfPZCreate;
BOOLEAN (*pfPZDelete)(PIZZA_S **);
}PZ_FACTORY_S;
typedef struct tag_PZ_STORE_S
{
PZ_STYLE_E eStyle;
PZ_TYPE_E eType;
PZ_FACTORY_S stFactory;
BOOLEAN (*pfFactoryInit)(PZ_FACTORY_S *,struct tag_PZ_STORE_S *);
BOOLEAN (*pfOrderPZ)(INT8U ,PIZZA_S **,struct tag_PZ_STORE_S *);
}PZ_STORE_S;
/*********************************************************************************************************
** Function name: PreparePZ
** Descriptions:
** input parameters:
**
** output parameters:
** Returned value:
**
*********************************************************************************************************/
void PreparePZ(void)
{
printf("prepare pizza");
}
void BakePZ(void)
{
printf("bake pizza")
}
void CutPZ(void)
{
printf("cut pizza")
}
void BoxPZ(void)
{
printf("Box pizza")
}
/*********************************************************************************************************
** Function name: FactoryInit
** Descriptions: 工厂初始化
** input parameters:
**
** output parameters:
** Returned value:
**
*********************************************************************************************************/
BOOLEAN FactoryInit(PZ_FACTORY_S *pstFactory,struct tag_PZ_STORE_S *pstStore )
{
if ((NULL == pstFactory)||(NULL == pstStore))
{
return FALSE;
}
if (NY_STYLE_E == pstStore->eStyle)
{
pstFactory->pfPZCreate = NYStylePizzaCreate;
pstFactory->pfPZDelete = PizzaDelete;
}
else if (CHI_STYLE_E == pstStore->eStyle)
{
pstFactory->pfPZCreate = ChiStylePizzaCreate;
pstFactory->pfPZDelete = PizzaDelete;
}
return TRUE;
}
/*********************************************************************************************************
** Function name: PizzaFactoryCreate
** Descriptions: 创建newyork 风味pizza
** input parameters: eType pizza类型
**
** output parameters:
** Returned value: pizza指针
**
**************************************

*******************************************************************/
PIZZA_S * NYStylePizzaCreate(PZ_TYPE_E eType)
{
PIZZA_S *pstPizza = (PIZZA_S*)malloc(sizeof(PIZZA_S));
if (NULL == pstPizza)
{
/* print err */
}
pstPizza->pfPrepare = PreparePZ;
pstPizza->pfBake = BakePZ;
pstPizza->pfCut = CutPZ;
pstPizza->pfBox = BoxPZ;
if (PZ_CHEESE == eType)
{
printf("ny style cheese pizza");
}
else if (PZ_PEPPERON == eType)
{
printf("ny style pepperon pizza");
}
else if (PZ_CLAM == eType)
{
printf("ny sytle clam pizza");
}
else if (PZ_VEGGIE == eType)
{
printf("ny style vegie pizza");
}
return pstPizza;
}

/*********************************************************************************************************
** Function name: ChiStylePizzaCreate
** Descriptions: 创建chicago 风味pizza
** input parameters: eType pizza类型
**
** output parameters:
** Returned value: pizza指针
**
*********************************************************************************************************/
PIZZA_S * ChiStylePizzaCreate(PZ_TYPE_E eType)
{
PIZZA_S *pstPizza = (PIZZA_S*)malloc(sizeof(PIZZA_S));
if (NULL == pstPizza)
{
/* print err */
}
pstPizza->pfPrepare = PreparePZ;
pstPizza->pfBake = BakePZ;
pstPizza->pfCut = CutPZ;
pstPizza->pfBox = BoxPZ;
if (PZ_CHEESE == eType)
{
printf("chi style cheese pizza");
}
else if (PZ_PEPPERON == eType)
{
printf("chi style pepperon pizza");
}
else if (PZ_CLAM == eType)
{
printf("chi style clam pizza");
}
else if (PZ_VEGGIE == eType)
{
printf("chi style veggie pizza");
}
return pstPizza;
}

/*********************************************************************************************************
** Function name: PizzaFactoryDelete
** Descriptions: 销毁pizza实例
** input parameters: ppstPizza 已存在的pizza实例
**
** output parameters: true 销毁成功 false 销毁失败
** Returned value:
**
*********************************************************************************************************/
BOOLEAN PizzaDelete(PIZZA_S **ppstPizza)
{
/* 入参检查 */
if (NULL == ppstPizza)
{
return FALSE;
}
if (NULL != *ppstPizza)
{
free(*ppstPizza);
*ppstPizza = NULL;
}
return TRUE;
}
/*********************************************************************************************************
** Function name: orderPizza
** Descriptions: pizza订单
** input parameters: ppstPizza 生产的pizza
**


** output parameters: true订单生成成功, false失败
** Returned value:
**
*********************************************************************************************************/
BOOLEAN orderPizza(PIZZA_S **ppstPizza,struct tag_PZ_STORE_S *pstStore)
{
PIZZA_S *pstback = NULL;

/* 入参检查 */
if (NULL == ppstPizza)
{
return FALSE;
}
pstStore->pfFactoryInit(&pstStore->stFactory,pstStore);
*ppstPizza = pstStore->stFactory.pfPZCreate(pstStore->eType);
pstback = *ppstPizza;
if (NULL == pstback)
{
return FALSE;
}
pstback->pfPrepare();
pstback->pfBake();
pstback->pfCut();
pstback->pfBox();
return TRUE;
}

void PizzaTestDrive(void)
{
PZ_STORE_S NYStylePZStore;
PZ_STORE_S ChicagoStylePZStore;
PIZZA_S *pNyStylePZ = NULL;
PIZZA_S *pChiStylePZ = NULL;

NYStylePZStore.eStyle = NY_STYLE_E;
NYStylePZStore.eType = PZ_CHEESE;
NYStylePZStore.pfOrderPZ = orderPizza;
NYStylePZStore.pfFactoryInit = FactoryInit;

ChicagoStylePZStore.eStyle = CHI_STYLE_E;
ChicagoStylePZStore.eType = PZ_CLAM;
ChicagoStylePZStore.pfOrderPZ = orderPizza;
ChicagoStylePZStore.pfFactoryInit = FactoryInit;

NYStylePZStore.pfOrderPZ(&pNyStylePZ,NYStylePZStore);
NYStylePZStore.stFactory.pfPZDelete(&pNyStylePZ);
ChicagoStylePZStore.pfOrderPZ(&pChiStylePZ,ChicagoStylePZStore);
ChicagoStylePZStore.stFactory.pfPZDelete(&pChiStylePZ);
}


相关文档
最新文档