c: Visitor Pattern in windows 10
/** * @file validator.h * @author your name (you@domain.com) * @brief 访问者模式 Visitor Pattern 来源: C现代编程 集成开发环境、设计模式、极限编程、测试驱动开发、重构、持续集成 日.花井志生著,杨文轩译,人民邮电出版社 * @version 0.1 * @date 2023-10-21 * * @copyright Copyright (c) 2023 * */ #ifndef _VALIDATOR_H_ #define _VALIDATOR_H_ #include <stddef.h> #include <stdbool.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * */ struct ValidatorVisitor; /** * @brief * */ typedef struct Validator { bool (* const validate)(struct Validator *pThis, int val); void (* const accept)(struct Validator *pThis, struct ValidatorVisitor *pVisitor); } Validator; /** * @brief * */ typedef struct { Validator base; const int min; const int max; } RangeValidator; /** * @brief * */ typedef struct { Validator base; int previousValue; } PreviousValueValidator; /** * @brief * */ typedef struct ValidatorVisitor { void (* const visitRange)(struct ValidatorVisitor *pThis, RangeValidator *p); void (* const visitPreviousValue)(struct ValidatorVisitor *pThis, PreviousValueValidator *p); } ValidatorVisitor; /** * @brief * * @param pThis * @param pVisitor */ void acceptRange(Validator *pThis, ValidatorVisitor *pVisitor); /** * @brief * * @param pThis * @param pVisitor */ void acceptPrevious(Validator *pThis, ValidatorVisitor *pVisitor); /** * @brief * * @param pThis * @param val * @return true * @return false */ bool validateRange(Validator *pThis, int val); /** * @brief * * @param pThis * @param val * @return true * @return false */ bool validatePrevious(Validator *pThis, int val); /** * @brief * */ #define newRangeValidator(min, max) \ {{validateRange, acceptRange}, (min), (max)} /** * @brief * */ #define newPreviousValueValidator \ {{validatePrevious, acceptPrevious}, 0} #ifdef __cplusplus } #endif #endif
/** * @file validator.c * @author your name (you@domain.com) * @brief 访问者模式 Visitor Pattern 来源: C现代编程 集成开发环境、设计模式、极限编程、测试驱动开发、重构、持续集成 日.花井志生著,杨文轩译,人民邮电出版社 * @version 0.1 * @date 2023-10-21 * * @copyright Copyright (c) 2023 * */ #include <stdbool.h> #include <stdio.h> #include "include/validator.h" /** * @brief * * @param p * @param val * @return true * @return false */ bool validateRange(Validator *p, int val) { RangeValidator *pThis = (RangeValidator *)p; return pThis->min <= val && val <= pThis->max; } /** * @brief * * @param p * @param val * @return true * @return false */ bool validatePrevious(Validator *p, int val) { PreviousValueValidator *pThis = (PreviousValueValidator *)p; if (val < pThis->previousValue) return false; pThis->previousValue = val; return true; } /** * @brief * * @param p * @param pVisitor */ void acceptRange(Validator *p, ValidatorVisitor *pVisitor) { pVisitor->visitRange(pVisitor, (RangeValidator *)p); } /** * @brief * * @param p * @param pVisitor */ void acceptPrevious(Validator *p, ValidatorVisitor *pVisitor) { pVisitor->visitPreviousValue(pVisitor, (PreviousValueValidator *)p); }
/** * @file validatorView.h * @author your name (you@domain.com) * @brief 访问者模式 Visitor Pattern 来源: C现代编程 集成开发环境、设计模式、极限编程、测试驱动开发、重构、持续集成 日.花井志生著,杨文轩译,人民邮电出版社 * @version 0.1 * @date 2023-10-21 * * @copyright Copyright (c) 2023 * */ #ifndef _VALIDATORVIEW_H_ #define _VALIDATORVIEW_H_ #include <stddef.h> #include <stdbool.h> #include <stdlib.h> #include "validator.h" #ifdef __cplusplus extern "C" { #endif /** * @brief * * @param p * @param pBuf * @param size */ void printValidator(const Validator *p, char *pBuf, size_t size); #ifdef __cplusplus } #endif #endif
/** * @file validatorView.c * @author your name (you@domain.com) * @brief 访问察者模式 Visitor Pattern 来源: C现代编程 集成开发环境、设计模式、极限编程、测试驱动开发、重构、持续集成 日.花井志生著,杨文轩译,人民邮电出版社 * @version 0.1 * @date 2023-10-21 * * @copyright Copyright (c) 2023 * */ #include <stdio.h> #include "include/validator.h" /** * @brief * * @param pThis * @param p */ static void rangeView(ValidatorVisitor *pThis, RangeValidator *p); /** * @brief * * @param pThis * @param p */ static void previousValueView(ValidatorVisitor *pThis, PreviousValueValidator *p); /** * @brief * */ typedef struct ViewVisitor { ValidatorVisitor base; char *pBuf; size_t size; } ViewVisitor; /** * @brief * * @param p * @param pBuf * @param size */ void printValidator(Validator *p, char *pBuf, size_t size) { ViewVisitor visitor = {{rangeView, previousValueView}, pBuf, size}; p->accept(p, &visitor.base); } /** * @brief * * @param pThis * @param p */ static void rangeView(ValidatorVisitor *pThis, RangeValidator *p) { ViewVisitor *pVisitor = (ViewVisitor* )pThis; snprintf(pVisitor->pBuf, pVisitor->size, "Range(%d-%d)", p->min, p->max); } /** * @brief * * @param pThis * @param p */ static void previousValueView(ValidatorVisitor *pThis, PreviousValueValidator *p) { ViewVisitor *pVisitor = (ViewVisitor* )pThis; snprintf(pVisitor->pBuf, pVisitor->size, "Previous"); }
调用:
#include "include/validator.h" #include "include/validatorView.h" int main() { printf("hello c world, \n"); printf("你好,中国\n"); RangeValidator v = newRangeValidator(0, 9); char buf[32]; printValidator(&v.base, buf, sizeof(buf)); strcmp("Range(0-9)", buf); printf("访问者模式1: %s\n",buf); PreviousValueValidator v2 = newPreviousValueValidator; char buf2[32]; printValidator(&v2.base, buf2, sizeof(buf2)); strcmp("Previous", buf2); printf("访问者模式1: %s\n",buf2); return 0; }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)