In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to write the pattern code of the chain of responsibility in C language". The content in the article is simple and clear, and it is easy to learn and understand. let's study and learn how to write the pattern code of the chain of responsibility in C language.
Introduction:
The chain of responsibility pattern is a behavior pattern that allows you to send a request along the handler chain. After receiving the request, each handler can process the request or pass it on to the next processor on the chain.
Function:
Pass the requests one by one in the way of the chain
Analogy:
When a company applies for reimbursement, it must first be approved by leader, then by HR, then by finance, and finally by the boss, who will pass on the request at one level.
Example: # include # include # include "list.h" static LIST_HEAD (chain_head); enum {PASS, REFUSE,}; typedef struct {int money; char* files;} info_t;typedef int (* request_handle) (info_t* info); typedef struct {request_handle func; ListObj list;} chain_node_t;int leader (info_t* info) {assert (info! = NULL); if (info- > money)
< 1000) //金额小于1000就行 { return PASS; } return REFUSE;}int hr(info_t* info){ assert(info != NULL); if(info->Money
< 2000 && info->Files! = NULL) / / the amount is less than 2000 and the materials are complete {return PASS;} return REFUSE;} int boss (info_t* info) {assert (info! = NULL); if (info- > files! = NULL) / / as long as the materials are complete, there is no shortage of money {return PASS;} return REFUSE } / / approval process: leader- > hr- > bosschain_node_t req_table [] = {{. Func = leader}, {. Func = hr}, {. Func = boss}}; void main (void) {/ * add the approval node to the responsibility chain * / for (int I = 0; I)
< sizeof(req_table) / sizeof(req_table[0]); i++) { list_insert_before(&chain_head, &(req_table[i].list)); } /*报销内容*/ info_t info = {.money = 900, .files = "files"}; ListObj *node; list_for_each(node, &chain_head) { chain_node_t* req = list_entry(node, chain_node_t, list); if(req->Func (& info)! = PASS) {/ * reimbursement failed * / printf ("Failed\ r\ n"); return;}} / * reimbursement succeeded * / printf ("Success\ r\ n"); return } Thank you for your reading. the above is the content of "how to write the C language chain of responsibility pattern code". After the study of this article, I believe you have a deeper understanding of how to write the C language chain of responsibility pattern code. the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 256
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.