针对深度嵌套关联条件的面向对象设计模式探究
深度嵌套条件的面向对象优化方案
对于这种依赖前序结果的深度嵌套条件分支,除了过程式的扁平化函数拆分,我们可以通过面向对象的封装思想,将每个步骤的逻辑独立为对象,实现流程与业务逻辑的解耦,下面是两种落地方案:
一、带状态传递的责任链模式
普通责任链模式侧重请求的传递处理,这里我们调整为每个处理器持有后续处理器,同时完成自身的“转换+判断”逻辑:若满足条件则将结果传递给下一个处理器,否则执行对应失败分支。
代码实现
首先定义处理器接口:
interface Processor<T, U> { setNext(processor: Processor<U, any>): Processor<T, U>; process(input: T): void; }
接着实现每个步骤的处理器:
- 输入非空检查处理器
class InputCheckProcessor implements Processor<Input, A> { private next?: Processor<A, B>; private onNull: () => void; constructor(onNull: () => void) { this.onNull = onNull; } setNext(processor: Processor<A, B>): Processor<Input, A> { this.next = processor; return this; } process(input: Input): void { if (input == null) { this.onNull(); // 对应原逻辑中的dosomething5 return; } const a = func1(input); this.next?.process(a); } }
- A值校验处理器
class CheckAProcessor implements Processor<A, B> { private next?: Processor<B, C>; private onFail: () => void; constructor(onFail: () => void) { this.onFail = onFail; } setNext(processor: Processor<B, C>): Processor<A, B> { this.next = processor; return this; } process(a: A): void { if (!p(a)) { this.onFail(); // 对应dosomething4 return; } const b = func2(a); this.next?.process(b); } }
- B值校验处理器
class CheckBProcessor implements Processor<B, C> { private next?: Processor<C, void>; private onFail: () => void; constructor(onFail: () => void) { this.onFail = onFail; } setNext(processor: Processor<C, void>): Processor<B, C> { this.next = processor; return this; } process(b: B): void { if (!p(b)) { this.onFail(); // 对应dosomething3 return; } const c = func3(b); this.next?.process(c); } }
- 最终C值处理处理器
class FinalCProcessor implements Processor<C, void> { private onPass: () => void; private onFail: () => void; constructor(onPass: () => void, onFail: () => void) { this.onPass = onPass; this.onFail = onFail; } setNext(_: Processor<any, any>): Processor<C, void> { return this; // 无后续处理器 } process(c: C): void { p(c) ? this.onPass() : this.onFail(); // 对应dosomething1/dosomething2 } }
组装与使用
// 定义各分支逻辑 const doSomething1 = () => { /* ... */ }; const doSomething2 = () => { /* ... */ }; const doSomething3 = () => { /* ... */ }; const doSomething4 = () => { /* ... */ }; const doSomething5 = () => { /* ... */ }; // 构建责任链 const processorChain = new InputCheckProcessor(doSomething5) .setNext(new CheckAProcessor(doSomething4)) .setNext(new CheckBProcessor(doSomething3)) .setNext(new FinalCProcessor(doSomething1, doSomething2)); // 执行逻辑 processorChain.process(input);
这种模式下,每个处理器只负责自身的校验与转换逻辑,新增或修改步骤仅需调整对应处理器,符合开闭原则。
二、Pipeline(管道)模式
将每个“转换+校验”的步骤封装为独立的Step对象,由管道类统一管控流程顺序,按线性方式执行步骤,直到某个步骤失败或进入最终处理。
代码实现
定义步骤接口与管道类:
interface Step<T, U> { transform(input: T): U; predicate(result: U): boolean; onFail(): void; } interface FinalStep<T> { process(input: T): void; } class Pipeline<T> { private steps: Step<any, any>[] = []; private finalStep?: FinalStep<any>; addStep<T, U>(step: Step<T, U>): Pipeline<U> { this.steps.push(step); return this as unknown as Pipeline<U>; } setFinalStep<T>(step: FinalStep<T>): void { this.finalStep = step; } execute(input: T): void { let currentInput: any = input; // 先处理输入null的情况 if (currentInput == null) { doSomething5(); return; } // 执行后续步骤 for (const step of this.steps) { currentInput = step.transform(currentInput); if (!step.predicate(currentInput)) { step.onFail(); return; } } this.finalStep?.process(currentInput); } }
组装与使用
// 定义各分支逻辑(同上) const doSomething1 = () => { /* ... */ }; const doSomething2 = () => { /* ... */ }; const doSomething3 = () => { /* ... */ }; const doSomething4 = () => { /* ... */ }; const doSomething5 = () => { /* ... */ }; // 构建管道 const pipeline = new Pipeline<Input>() .addStep({ transform: (input) => func1(input), predicate: (a) => p(a), onFail: doSomething4 }) .addStep({ transform: (a) => func2(a), predicate: (b) => p(b), onFail: doSomething3 }) .addStep({ transform: (b) => func3(b), predicate: () => true, // 校验逻辑交给最终步骤 onFail: () => {} }); // 设置最终处理步骤 pipeline.setFinalStep({ process: (c) => p(c) ? doSomething1() : doSomething2() }); // 执行逻辑 pipeline.execute(input);
管道模式更适合固定顺序的线性流程,代码结构更简洁,流程管控集中在管道类中。
方案对比
- 责任链模式:每个处理器自主决定是否传递流程,适合存在分支走向的场景,扩展性更强。
- 管道模式:流程线性可控,代码更简洁,适合固定顺序的链式处理。
两种方案的核心都是将嵌套逻辑拆解为独立对象,实现业务逻辑与流程控制的解耦,相比过程式的函数拆分,更符合面向对象的封装与开闭原则。
内容的提问来源于stack exchange,提问作者beatrice
相关产品推荐
相关产品推荐

