Skill站外收录 @skilllib/skill-2117939930272411847
09-chain-of-responsibility-pattern
提供一种可扩展的链式处理机制,支持按需串联多个独立处理器,在请求流转过程中实现拦截、过滤或命令执行等多阶段逻辑,各环节可自主决定是否继续传递或提前终止流程。
软件开发 AI 技能
Skills.MD
# 责任链模式
责任链模式实践指南.
## 触发条件
当用户需要实现拦截器、过滤器、命令链等链式处理逻辑时,使用此 Skill。
## 模式类型
### 1. 拦截器链(Interceptor Chain)
```kotlin
// 拦截器接口
interface PipelineInterceptor {
fun execute(task: InterceptData): Response<BuildStatus>
}
// 拦截器链
class PipelineInterceptorChain(
private val filters: List<PipelineInterceptor>
) {
fun filter(any: InterceptData): Response<BuildStatus> {
var result = Response<BuildStatus>(OK)
run lit@{
filters.forEach {
result = it.execute(any)
if (result.isNotOk()) return@lit // 短路返回
}
}
return result
}
}
```
### 2. 命令链(Command Chain)
```kotlin
// 命令接口
interface ContainerCmd : Cmd<ContainerContext> {
override fun execute(commandContext: ContainerContext)
}
// 命令链
class ContainerCmdChain(
private val commandList: List<ContainerCmd>
) : CmdChain<ContainerContext> {
override fun nextCommand(commandContext: ContainerContext): Cmd<ContainerContext>? {
return commandList.getOrNull(commandContext.cmdFlowSeq++)
}
}
// 命令执行器
fun executeChain(chain: ContainerCmdChain, context: ContainerContext) {
var cmd = chain.nextCommand(context)
while (cmd != null) {
cmd.execute(context)
cmd = chain.nextCommand(context)
}
}
```
### 3. 过滤器链(Filter Chain)
```kotlin
// 过滤器接口
interface WebhookFilter {
fun doFilter(response: WebhookFilterResponse): Boolean
}
// 过滤器链
class WebhookFilterChain(
private val filters: List<WebhookFilter>
) : WebhookFilter {
override fun doFilter(response: WebhookFilterResponse): Boolean {
filters.forEach { filter ->
if (!filter.doFilter(response)) {
return false // 任一过滤器拒绝则终止
}
}
return true
}
}
```
## 实现示例
### 流水线启动拦截器
```kotlin
@Component
class QueueInterceptor : PipelineInterceptor {
override fun execute(task: InterceptData): Response<BuildStatus> {
// 检查队列是否已满
if (isQueueFull(task.pipelineId)) {
return Response(BuildStatus.QUEUE_TIMEOUT)
}
return Response(BuildStatus.SUCCEED)
}
}
@Component
class PermissionInterceptor : PipelineInterceptor {
override fun execute(task: InterceptData): Response<BuildStatus> {
// 检查用户权限
if (!hasPermission(task.userId, task.pipelineId)) {
return Response(BuildStatus.FAILED)
}
return Response(BuildStatus.SUCCEED)
}
}
```
### 容器命令实现
```kotlin
@Component
class CheckContainerMutexCmd : ContainerCmd {
override fun execute(commandContext: ContainerContext) {
// 检查容器互斥锁
if (hasMutex(commandContext.containerId)) {
commandContext.buildStatus = BuildStatus.QUEUE
commandContext.latestSummary = "等待互斥锁"
}
}
}
@Component
class StartContainerCmd : ContainerCmd {
override fun execute(commandContext: ContainerContext) {
// 启动容器
startContainer(commandContext.containerId)
commandContext.buildStatus = BuildStatus.RUNNING
}
}
```
## 配置方式
```kotlin
@Configuration
class InterceptorConfiguration {
@Bean
fun pipelineInterceptorChain(
interceptors: List<PipelineInterceptor>
): PipelineInterceptorChain {
// 按优先级排序
val sorted = interceptors.sortedBy { it.order() }
return PipelineInterceptorChain(sorted)
}
}
```
## 最佳实践
1. **单一职责**:每个处理器只负责一个功能
2. **顺序控制**:通过 `@Order` 或 `order()` 方法控制执行顺序
3. **短路返回**:失败时立即返回,避免不必要的处理
4. **上下文传递**:使用 Context 对象在链中传递数据
## 相关文件
- `process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/interceptor/`
- `process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/control/command/`
- `common-webhook/biz-common-webhook/src/main/kotlin/com/tencent/devops/common/webhook/service/code/filter/`
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「09-chain-of-responsibility-pattern」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次