#include <stdio.h>#include <stdlib.h>#include <string.h>/*建造者模式示例,详细说明及类图参见设计模式之禅 p106*//*作者chengrile, 转载注明出处!*//*行为顺序数组*/#define SEQUENCE_SIZE 4#define SEQUENCE_STRINGLEN 15static unsigned char aucSequence [ SEQUENCE_SIZE ][ SEQUENCE_STRINGLEN ] = { 0 };/*aucSequence 数据内容形如:aucSequence[4][15] = {"start","stop""alarm""engineBoom"}*//*抽象产品类 (抽象车模)*/typedef struct _CarModel CarModel ;struct _CarModel{unsigned char * pSequence ; /*指向行为顺序的指针*//*车模的公用方法*/void ( * start )( void ); //发动void ( * stop )( void ); //停车void ( * alarm )( void ); //鸣笛void ( * engineBoom )( void ); //引擎轰鸣void ( * run )( CarModel * pCarModel ); //车模行驶,根据行为顺序行驶void ( * setSequence )( unsigned char * pParam1 , unsigned char * pParam2 ,unsigned char * pParam3 , unsigned char * pParam4 ); //设置行为顺序/*析构函数*/void ( * DeleteCarModel )( CarModel * pCarModel );};/*抽象车模的run方法实现*/void run ( CarModel * pCarModel ){int i = 0 ;unsigned char * pTmp = pCarModel -> pSequence ;for ( i = 0 ; i < SEQUENCE_SIZE ; i ++ ){if ( 0 == strcmp ( pTmp , "start" )){pCarModel -> start ();}else if ( 0 == strcmp ( pTmp , "stop" )){pCarModel -> stop ();}else if ( 0 == strcmp ( pTmp , "alarm" )){pCarModel -> alarm ();}else if ( 0 == strcmp ( pTmp , "engineBoom" )){pCarModel -> engineBoom ();}else{//error handle}pTmp += SEQUENCE_STRINGLEN ;}}/*抽象车模的setSequence方法实现*/void setSequence ( unsigned char * pParam1 , unsigned char * pParam2 ,unsigned char * pParam3 , unsigned char * pParam4 ){memset ( aucSequence , 0 , sizeof ( aucSequence ));strcpy ( aucSequence [ 0 ], pParam1 );strcpy ( aucSequence [ 1 ], pParam2 );strcpy ( aucSequence [ 2 ], pParam3 );strcpy ( aucSequence [ 3 ], pParam4 );return ;}void DeleteCarModel ( CarModel * pCarModel ){if ( NULL != pCarModel ){free ( pCarModel );pCarModel = NULL ;}}CarModel * CreateCarModel ( void ){CarModel * pCarModel = ( CarModel * ) malloc ( sizeof ( CarModel ));if ( NULL == pCarModel ){//printfreturn pCarModel ;}memset ( pCarModel , 0 , sizeof ( CarModel ));pCarModel -> pSequence = aucSequence ;pCarModel -> DeleteCarModel = DeleteCarModel ;pCarModel -> run = run ;pCarModel -> setSequence = setSequence ;return pCarModel ;}/*具体产品类 (继承抽象产品类)*//*benz车模*/typedef struct _BenzModel BenzModel ;struct _BenzModel{CarModel carModel ;};void BenzAlarm ( void ){printf ( "Benz alarm \n " );}void BenzEngineBoom ( void ){printf ( "Benz engineBoom \n " );}void BenzStart ( void ){printf ( "Benz start \n " );}void BenzStop ( void ){printf ( "Benz stop \n " );}BenzModel * CreateBenzModel ( void ){BenzModel * pBenzModel = ( BenzModel * ) CreateCarModel ();if ( NULL == pBenzModel ){//printfreturn pBenzModel ;}pBenzModel -> carModel . alarm = BenzAlarm ;pBenzModel -> carModel . engineBoom = BenzEngineBoom ;pBenzModel -> carModel . start = BenzStart ;pBenzModel -> carModel . stop = BenzStop ;return pBenzModel ;}/*BWM车模*//*与 Benz车模类似,代码略*//*抽象的组装者*/typedef struct _CarBuilder CarBuilder ;struct _CarBuilder{void ( * setSequence )( unsigned char * pParam1 , unsigned char * pParam2 ,unsigned char * pParam3 , unsigned char * pParam4 );CarModel * ( * getCarModel )( CarBuilder * pCarBuilder );};/*具体组装者*//*Benz组装者*/typedef struct _BenzBuilder BenzBuilder ;struct _BenzBuilder{CarBuilder carBuilder ; //继承抽象组装者BenzModel * pBenzModel ; //组装者内部有所要组装的对象实体指针void ( * DeleteBenzBuiler )( BenzBuilder * pBenzBuilder );};void DeleteBenzBuiler ( BenzBuilder * pBenzBuilder ){if ( NULL != pBenzBuilder -> pBenzModel ){free ( pBenzBuilder -> pBenzModel ); //先释放掉内部成员pBenzBuilder -> pBenzModel = NULL ;free ( pBenzBuilder ); //再释放掉自己pBenzBuilder = NULL ;}}CarModel * getBenzModel ( CarBuilder * pCarBuilder ){return ( CarModel * )(( BenzBuilder * ) pCarBuilder ) -> pBenzModel ; //返回组装者组装的对象}BenzBuilder * CreateBenzBuilder ( void ){BenzBuilder * pBenzBuilder = ( BenzBuilder * ) malloc ( sizeof ( BenzBuilder ));BenzModel * pBenzModel = CreateBenzModel ();if ( NULL == pBenzBuilder ){return NULL ;}memset ( pBenzBuilder , 0 , sizeof ( BenzBuilder ));if ( NULL == pBenzModel ){return NULL ;}pBenzBuilder -> pBenzModel = pBenzModel ;pBenzBuilder -> DeleteBenzBuiler = DeleteBenzBuiler ;pBenzBuilder -> carBuilder . setSequence = setSequence ;pBenzBuilder -> carBuilder . getCarModel = getBenzModel ;return pBenzBuilder ;}/*导演类*/typedef struct _Director Director ;struct _Director{BenzBuilder * pBenzBuilder ; /*导演类中包含具体的建造者*///BwnBuilder *pBwnBuilder; /*可以有很多建造者,代码略*//*各种不同行为顺序的车模*/CarModel * ( * getABenzModel )( Director * pDirector );CarModel * ( * getBBenzModel )( Director * pDirector );/* ... */void ( * DeleteDirector )( Director * pDirector );};CarModel * getABenzModel ( Director * pDirector ){/*传递A类Benz车模行为顺序到benz车模建造者*/pDirector -> pBenzBuilder -> carBuilder . setSequence ( "engineBoom" , "start" , "alarm" , "stop" );/*建造者按照要求组装出一个benz车模*/return ( BenzModel * ) pDirector -> pBenzBuilder -> carBuilder . getCarModel (( CarBuilder * ) pDirector -> pBenzBuilder );}CarModel * getBBenzModel ( Director * pDirector ){/*传递B类Benz车模行为顺序到benz车模建造者*/pDirector -> pBenzBuilder -> carBuilder . setSequence ( "start" , "engineBoom" , "alarm" , "stop" );/*建造者按照要求组装出一个benz车模*/return ( BenzModel * ) pDirector -> pBenzBuilder -> carBuilder . getCarModel (( CarBuilder * ) pDirector -> pBenzBuilder );}void DeleteDirector ( Director * pDirector ){if ( NULL != pDirector -> pBenzBuilder ){pDirector -> pBenzBuilder -> DeleteBenzBuiler ( pDirector -> pBenzBuilder ); /*先释放内部对象*/free ( pDirector );}}Director * CreateDirector ( void ){Director * pDirector = ( Director * ) malloc ( sizeof ( Director ));if ( NULL == pDirector ){return NULL ;}memset ( pDirector , 0 , sizeof ( Director ));pDirector -> pBenzBuilder = CreateBenzBuilder ();if ( NULL == pDirector -> pBenzBuilder ){return NULL ;}pDirector -> getABenzModel = getABenzModel ;pDirector -> getBBenzModel = getBBenzModel ;pDirector -> DeleteDirector = DeleteDirector ;return pDirector ;}/*场景*/void main (){int i = 0 ;/*定义一个导演实体*/Director * pDirector = CreateDirector ();if ( NULL == pDirector ){return ;}/*建造n辆A类Benz*/for ( i = 0 ; i < 10 ; i ++ ){pDirector -> getABenzModel ( pDirector ) -> run (( CarModel * ) pDirector -> pBenzBuilder -> pBenzModel );printf ( "-------- \n " );}/*建造n辆B类Benz*/for ( i = 0 ; i < 10 ; i ++ ){pDirector -> getBBenzModel ( pDirector ) -> run (( CarModel * ) pDirector -> pBenzBuilder -> pBenzModel );printf ( "-------- \n " );}pDirector -> DeleteDirector ( pDirector );}
C语言实现《设计模式之禅》之建造者模式
最新推荐文章于 2023-02-08 07:30:35 发布