C语言实现《设计模式之禅》之模板模式

#include
#include
#include




/*C语言实现设计模式之禅  模板模式   类图详见p95*/
/*作者chengrile,转载注明出处!*/

#define FALSE  0
#define TRUE   1
typedef int BOOLEAN;


/*抽象模板类*/
typedef struct _HummerModel HummerModel;
struct _HummerModel
{
    /*基本方法*/
    void (*start)(void);
    void (*stop)(void);
    void (*alarm)(void);
    void (*engineBoom)(void);


    /*模板方法*/
    void (*run)(HummerModel *pHummerModel);


    /*钩子方法*/
    BOOLEAN (*isAlarm)(void);
};
/*作者chengrile,转载注明出处!*/

/*模板方法的实现框架*/
void run(HummerModel *pHummerModel)
{
    pHummerModel->start();
    pHummerModel->engineBoom();
    if(pHummerModel->isAlarm())
    {
        pHummerModel->alarm();
    }
    pHummerModel->stop();
    return;
}


HummerModel *CreateHummerModel(void)
{
    HummerModel *pHummerModel = (HummerModel *)malloc(sizeof(HummerModel));
    if (NULL == pHummerModel)
    {
        return NULL;
    }
    memset(pHummerModel, 0, sizeof(HummerModel));
    pHummerModel->run = run;  /*挂接模板方法*/
    return pHummerModel;
}




/*具体产品类, 继承模板类*/
typedef struct _HumerH1Model HumerH1Model;
static BOOLEAN s_HumerH1ModelAlarmFlag = FALSE;
struct _HumerH1Model
{
    HummerModel *phummerModel;
    void (*setAlarm)(BOOLEAN isAlarm);
    void (*DeleteHumerH1Model)(HumerH1Model *pHumerH1Model);
};


void HumerH1SetAlarm(BOOLEAN isAlarm)
{
    s_HumerH1ModelAlarmFlag = isAlarm;
}


BOOLEAN HumerH1IsAlarm(void)
{
    return s_HumerH1ModelAlarmFlag;
}


void HumerH1Start(void)
{
    printf("HumerH1Model start...\n");
}


void HumerH1Alarm(void)
{
    printf("HumerH1Model alarm...\n");
}


void HumerH1EngineBoom(void)
{
    printf("HumerH1Model engineBoom...\n");
}


void HumerH1Stop(void)
{
    printf("HumerH1Model stop...\n");
}


void DeleteHumerH1Model(HumerH1Model *pHumerH1Model)
{
    if(pHumerH1Model->phummerModel != NULL)
    {
        free(pHumerH1Model->phummerModel);
    }
    if(pHumerH1Model != NULL)
    {
        free(pHumerH1Model);
        pHumerH1Model = NULL;
    }
}


HumerH1Model *CreateHumerH1Model(void)
{
    HumerH1Model *pHumerH1Model = (HumerH1Model *)malloc(sizeof(HumerH1Model));
    if(NULL == pHumerH1Model)
    {
        return NULL;
    }
    memset(pHumerH1Model, 0, sizeof(HumerH1Model));
    
    pHumerH1Model->phummerModel = CreateHummerModel();
    if(NULL == pHumerH1Model->phummerModel)
    {
        return NULL;
    }


    /*实例化子类自有方法*/
    pHumerH1Model->setAlarm = HumerH1SetAlarm;
    pHumerH1Model->DeleteHumerH1Model = DeleteHumerH1Model;


    /*将父类中的基本方法实例化, 模板方法就使用父类方法*/
    pHumerH1Model->phummerModel->start = HumerH1Start;  
    pHumerH1Model->phummerModel->engineBoom = HumerH1EngineBoom;
    pHumerH1Model->phummerModel->alarm = HumerH1Alarm;
    pHumerH1Model->phummerModel->stop = HumerH1Stop;


    /*将父类中的钩子方法实例化*/
    pHumerH1Model->phummerModel->isAlarm = HumerH1IsAlarm;


    
    return pHumerH1Model;
}


/*场景*/
void main()
{
    int input = 0;
    HumerH1Model *pHumerH1Model = NULL;


    pHumerH1Model = CreateHumerH1Model();
    if(!pHumerH1Model)
    {
        return;
    }


    printf("----------H1型号悍马-----------\n");
    printf("是否需要喇叭响? 0-不需要  1-需要\n");
    scanf("%d\n", &input);


    if(1 == input)
    {
        pHumerH1Model->setAlarm(TRUE);
    }
    else
    {
        pHumerH1Model->setAlarm(FALSE);
    }


    pHumerH1Model->phummerModel->run(pHumerH1Model->phummerModel);


    pHumerH1Model->DeleteHumerH1Model(pHumerH1Model);
    
}

你可能感兴趣的:(设计模式)