来,展示一下工作中常用的设计模式!

39fc7ca4cd53ef1b0496507f32ccd718.png

若有收获,请记得分享和转发哦

平时我们写代码呢,多数情况都是流水线式写代码,基本就可以实现业务逻辑了。如何在写代码中找到乐趣呢,我觉得,最好的方式就是:使用设计模式优化自己的业务代码。今天跟大家聊聊日常工作中,我都使用过哪些设计模式。

db6057e5722e665f0d3612542e6d8ce5.png

工作中常用到哪些设计模式

d0e3f131e2e8a4802e46bc999f62716d.png

83fe6ddaa2c928c827dcde9612c009c6.png

8abb27b81e1293f8b5de4ef0e6fd724f.png

7f2d36b29b3e010ddf146f9bedd83bed.png

c6a63cc0fa8173258bd662b2f00be8e5.png

d2c3fac3d7ccc8387f9cf4ffd657af73.png

ed9666f6533ad20b7a69b55190c47a49.png

0b8f181801fd16329fb24056a237a12e.png

334101ec70df19cb3ba7ba2fef13924a.png

7542590a402104f21d7ad8e81faaefd7.png

583107a5361c13750b400df50891b968.png

16e47cb4c37232f21888e7a3add3eec4.png

95dad1cbba90f9df49a7a37e83c279fd.png

9c3d902af6787bab570f6cdd16195646.png

3540a500058954e2757a3414c6f887c7.png

6383a9d9f72b69ad2730912adec33c1d.png

48e8468116b3355a45912ceb9a12e434.png

6468fc7a3d127d7b66c3013db2c13d46.png

05de91db0f3c1b8052a1eeffcd328001.png

30b74cb1007f5fe37f78387fe66bd085.png

9dd27f912e01584652a0f9cb98ccfddc.png

581f22fb27a0770ad079fb1d82ad8e17.png

cee778e79ca2380cf42ec970ce876828.png

5562a90ce3723d7f2dc64d8e6dc7fa4f.png

9f6c4452de380c0466b1a8031d475564.png

230b3d43ed613b4dcb038c253b10713b.png

5d0708bfbbe0aa7f844034bd3592ceaa.png

1786e7564a7fa195fd741eb62f398baf.png

6671b4bb66e58a6ead8f4ca68eb7b404.png

2d68b693d0a653b72402f4e9dc468b06.png

ce0f5a8c0a722cceae5fc4076c157c6e.png

fd37f6367cece8520956199cc6212299.png

20ed1f4b9970fcbbdddbff46691ad9c4.png

3180edb0b4127a39143bdc6e09206d21.png

473d15c72e656f0f54949600576260d8.png

6dcf620c1c438b1d32ae750075172ddd.png

cdf1fc0d522409fce188e22a10f55101.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 当然,命令模式是一种常用设计模式,其主要思想是将请求封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。下面是一段 C 语言代码,展示了如何实现命令模式: ``` #include <stdio.h> #include <stdlib.h> // 定义一个接收者对象 typedef struct Receiver { void (*Action)(); } Receiver; void ReceiverAction() { printf("Receiver action\n"); } // 定义一个命令对象 typedef struct Command { void (*Execute)(); Receiver* receiver; } Command; void CommandExecute(Command* self) { self->receiver->Action(); } // 定义一个调用者对象 typedef struct Invoker { Command* command; } Invoker; void InvokerInvoke(Invoker* self) { self->command->Execute(); } int main() { Receiver* receiver = (Receiver*)malloc(sizeof(Receiver)); receiver->Action = ReceiverAction; Command* command = (Command*)malloc(sizeof(Command)); command->Execute = (void(*)(void))CommandExecute; command->receiver = receiver; Invoker* invoker = (Invoker*)malloc(sizeof(Invoker)); invoker->command = command; invoker->Invoke = InvokerInvoke; invoker->Invoke(invoker); free(receiver); free(command); free(invoker); return 0; } ``` 运行后的结果: ``` Receiver action ``` 希望这段代码能帮助你了解命令模式的实现。 ### 回答2: 当然可以用C语言来展示命令模式。 命令模式是一种行为型设计模式,它将一个请求封装成一个对象,从而使用户可以用不同的请求对客户进行参数化。下面是一个使用C语言实现的简单示例: ```c #include <stdio.h> // 命令接口 typedef struct { void (*execute)(void); } ICommand; // 命令实现类A typedef struct { ICommand command; } ConcreteCommandA; void executeA(void) { printf("执行命令A\n"); } // 命令实现类B typedef struct { ICommand command; } ConcreteCommandB; void executeB(void) { printf("执行命令B\n"); } // 命令调用者 typedef struct { ICommand *command; } Invoker; void setCommand(Invoker *invoker, ICommand *command) { invoker->command = command; } void executeCommand(Invoker *invoker) { invoker->command->execute(); } int main() { // 创建命令对象 ConcreteCommandA commandA; commandA.command.execute = executeA; ConcreteCommandB commandB; commandB.command.execute = executeB; // 创建命令调用者对象 Invoker invoker; // 设置命令A并执行 setCommand(&invoker, &commandA.command); executeCommand(&invoker); // 设置命令B并执行 setCommand(&invoker, &commandB.command); executeCommand(&invoker); return 0; } ``` 上述代码示例,我们定义了一个`ICommand`接口,然后创建了两个具体的命令实现类`ConcreteCommandA`和`ConcreteCommandB`,它们分别实现了`execute`方法用于执行具体的命令。 接着我们定义了一个命令调用者`Invoker`,它可以接收不同的命令对象并调用其`execute`方法来执行命令。 在`main`函数,我们创建了命令对象`commandA`和`commandB`,并且设置了命令调用者`invoker`的命令为`commandA`,然后通过调用`executeCommand`方法来执行命令。接着我们将命令调用者的命令设置为`commandB`,再次调用`executeCommand`方法来执行命令。 最终的输出为: ``` 执行命令A 执行命令B ``` 这就是一个简单的用C语言实现的命令模式示例。 ### 回答3: 当然可以!命令模式(Command Pattern) 是一种行为型设计模式,用于将请求封装成对象,以便在不同的请求发送者和接收者之间进行解耦。 下面是使用C语言展示命令模式的简单示例: 首先,我们需要一个命令接口(Command Interface),它定义了执行命令的方法: ```c typedef struct { void (*execute)(void); } Command; ``` 然后,我们创建几个不同的命令类(Command Classes),它们实现了命令接口的方法: ```c typedef struct { Command base; } ConcreteCommand1; void ConcreteCommand1_execute(void) { printf("执行命令1...\n"); } typedef struct { Command base; } ConcreteCommand2; void ConcreteCommand2_execute(void) { printf("执行命令2...\n"); } ``` 接下来,我们创建一个命令调用者(Command Invoker)类,它可以接收并执行任何命令: ```c typedef struct { Command *command; } Invoker; void Invoker_setCommand(Invoker *invoker, Command *command) { invoker->command = command; } void Invoker_executeCommand(Invoker *invoker) { if (invoker->command != NULL) { invoker->command->execute(); } } ``` 最后,我们在主函数使用这些类来演示命令模式的工作原理: ```c int main() { Invoker invoker; ConcreteCommand1 command1; ConcreteCommand2 command2; invoker.command = NULL; Invoker_setCommand(&invoker, (Command *)&command1); Invoker_executeCommand(&invoker); // 输出:执行命令1... Invoker_setCommand(&invoker, (Command *)&command2); Invoker_executeCommand(&invoker); // 输出:执行命令2... return 0; } ``` 在这个简单的示例,我们定义了两个具体命令类(ConcreteCommand1和ConcreteCommand2),它们实现了命令接口的`execute`方法。通过命令调用者(Invoker)可以动态地设置和执行不同的命令。 这就是使用C语言展示命令模式的简单示例。实际应用,命令模式可以帮助实现更复杂的命令操作,例如撤销、重做等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值