c: Visitor Pattern in windows 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | /** * @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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | /** * @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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * @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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /** * @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" ); } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #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(涂聚文)
分类:
Cpp programming
标签:
desgin patterns
, 設計模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-10-21 Python: Decorator Pattern
2022-10-21 Python: Composite Pattern
2013-10-21 asp and javascript: sql server export data to csv and to xls
2010-10-21 CSS Text-Shadow in Safari, Opera, Firefox and more
2009-10-21 CSS Menu Design书目录式的WEB页面