c: chain Of Responsibility 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * @file chainOfResponsibility.h
 * @author your name (geovindu)
 * @brief  chain Of Responsibility 职责链模式
 * @version 0.1
 * @date 2023-10-28
 *  IDE: VSCODE C11 C17
 * @copyright Copyright (c) 2023
 *
 */
 
 
#ifndef  _CHAINOFRESPONSIBILITY_H_
#define _CHAINOFRESPONSIBILITY_H_
 
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
 
#ifdef __cplusplus
extern "C" {
#endif
 
/**
 * @brief 检验处理链
 *
 */
typedef struct ChainDuValidator {
    bool (* const chainDuValidator)
    (struct ChainDuValidator *pThis, int val);
} ChainDuValidator;
 
/**
 * @brief
 *
 */
typedef struct {
    ChainDuValidator base;
    const int min;
    const int max;
} ChainDuRangeValidator;
 
/**
 * @brief
 *
 */
typedef struct {
    ChainDuValidator base;
    int previousValue;
} ChainDuPreviousValueValidator;
 
/**
 * @brief
 *
 */
typedef struct ChainedValidator {
    ChainDuValidator base;
    ChainDuValidator *pWrapped;
    ChainDuValidator *pNext;
} ChainedValidator;
 
/**
 * @brief
 *
 */
typedef struct {
    int top;
    const size_t size;
    int * const pBuf;
    ChainDuValidator * const pValidator;
} Stack;
 
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDuvalidateRange(ChainDuValidator *p, int val);
 
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDuvalidatePrevious(ChainDuValidator *p, int val);
 
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDuvalidateChain(ChainDuValidator *p, int val);
 
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDupush(Stack *p, int val);
 
/**
 * @brief
 *
 * @param p
 * @param pRet
 * @return true
 * @return false
 */
bool ChainDupop(Stack *p, int *pRet);
 
 
/**
 * @brief
 *
 */
#define DunewRangeValidator(min, max) \
    {{ChainDuvalidateRange}, (min), (max)}
 
/**
 * @brief
 *
 */
#define DunewPreviousValueValidator \
    {{ChainDuvalidatePrevious}, 0}
 
 
/**
 * @brief
 *
 */
#define DunewChainedValidator(wrapped, next)       \
    {{ChainDuvalidateChain}, (wrapped), (next)}
 
 
 
/**
 * @brief
 *
 */
#define newStack(buf) {                  \
    0, sizeof(buf) / sizeof(int), (buf), \
    NULL                                 \
}
 
/**
 * @brief
 *
 */
#define DunewStackWithValidator(buf, pValidator) { \
    0, sizeof(buf) / sizeof(int), (buf),         \
    pValidator                                   \
}
 
#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
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 chainOfResponsibility.C
 * @author your name (geovindu)
 * @brief  chain Of Responsibility 职责链模式
 * @version 0.1
 * @date 2023-10-28
 * IDE: VSCODE C11 C17
 * @copyright Copyright (c) 2023
 *
 */
 
#include "include/chainOfResponsibility.h"
#include <stdbool.h>
 
/**
 * @brief
 *
 * @param p
 * @return true
 * @return false
 */
static bool isStackFull(const Stack *p) {
    return p->top == p->size;
}
 
/**
 * @brief
 *
 * @param p
 * @return true
 * @return false
 */
static bool isStackEmpty(const Stack *p) {
    return p->top == 0;
}
 
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDuvalidateRange(ChainDuValidator *p, int val) {
    ChainDuRangeValidator *pThis = (ChainDuRangeValidator *)p;
    return pThis->min <= val && val <= pThis->max;
}
 
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDuvalidatePrevious(ChainDuValidator *p, int val) {
    ChainDuPreviousValueValidator *pThis = (ChainDuPreviousValueValidator *)p;
    if (val < pThis->previousValue) return false;
    pThis->previousValue = val;
    return true;
}
 
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDuvalidate(ChainDuValidator *p, int val) {
    if (! p) return true;
    return p->chainDuValidator(p, val);
}
 
// true: 成功, false: 失敗
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDupush(Stack *p, int val) {
    if (! ChainDuvalidate(p->pValidator, val) || isStackFull(p)) return false;
    p->pBuf[p->top++] = val;
    return true;
}
 
// true: 成功, false: 失敗
 
/**
 * @brief
 *
 * @param p
 * @param pRet
 * @return true
 * @return false
 */
bool ChainDupop(Stack *p, int *pRet) {
    if (isStackEmpty(p)) return false;
    *pRet = p->pBuf[--p->top];
    return true;
}
 
/**
 * @brief
 *
 * @param p
 * @param val
 * @return true
 * @return false
 */
bool ChainDuvalidateChain(ChainDuValidator *p, int val) {
    ChainedValidator *pThis = (ChainedValidator *)p;
 
    p = pThis->pWrapped;
    if (! p->chainDuValidator(p, val)) return false;
 
    p = pThis->pNext;
    return !p || p->chainDuValidator(p, val);
}

  

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
typedef struct TestValidator {
    Validator base;
    bool result;
} TestValidator;
 
 
static bool validateTest(Validator *p, int val) {
        TestValidator *pThis = (TestValidator *)p;
        return pThis->result;
}
 
 
static TestValidator trueValidator = {{validateTest}, true};
static TestValidator falseValidator = {{validateTest}, false};
 
 
 
int main(void)
{
    printf("hello c world, \n");
    printf("你好,中国\n");
 
 
 
    RangeValidator rangeValidator = DunewRangeValidator(0, 9);
    PreviousValueValidator prevValidator = DunewPreviousValueValidator;
 
    ChainedValidator prevChain = DunewChainedValidator(&prevValidator.base, NULL);
    ChainedValidator rangeChain = DunewChainedValidator(&rangeValidator.base, &prevChain.base);
 
    int buf[16]={1,2,4,3,5,7,8};
    Stack stack = DunewStackWithValidator(buf, &rangeChain.base);
    //false, ChainDupush(&stack, -1)
    printf("%d/n",ChainDupush(&stack, -1);
   return 0:
}

https://github.com/cirocosta/observer-c
https://www.adamtornhill.com/articles/patternsinc/patternsincnew.htm
https://www.adamtornhill.com/code.htm
https://www.adamtornhill.com/articles.htm
https://www.adamtornhill.com/Patterns%20in%20C%201.pdf
https://www.adamtornhill.com/Patterns%20in%20C%202,%20STATE.pdf
https://www.adamtornhill.com/Patterns%20in%20C%203,%20STRATEGY.pdf
https://www.adamtornhill.com/Patterns%20in%20C%204,%20OBSERVER.pdf
https://www.adamtornhill.com/Patterns%20in%20C%205,%20REACTOR.pdf

https://github.com/adamtornhill/PatternsInC

https://c.helpful.codes/patterns/Observer/
https://github.com/huawenyu/Design-Patterns-in-C
https://swedishembedded.com/design-patterns/
https://blog.csdn.net/ZCShouCSDN/article/details/80217199
https://www.sciencedirect.com/book/9781856177078/design-patterns-for-embedded-systems-in-c
https://www.oreilly.com/library/view/design-patterns-for/9781856177078/
https://github.com/topics/design-patterns?l=c

  

 

posted @   ®Geovin Du Dream Park™  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-10-28 CSharp: The single-responsibility principle (SRP) in donet core 6
2015-10-28 百度地图查询两地里程
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示