All files / src template-method.js

100% Statements 28/28
83.33% Branches 5/6
100% Functions 10/10
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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              2x             16x 1x   15x 15x                               7x     7x     7x     7x     7x     7x     7x     7x         7x       7x       7x 7x 35x                 7x       7x       7x       7x                                   2x 2x     2x 1x           2x  
/**
 * 模板方法模式(Template Method Pattern)
 * 
 * 定义第一性原理分析的 7 阶段骨架
 * 各策略可以重写特定步骤,但整体流程保持一致
 */
 
const { AnalysisStrategy } = require('./strategies/strategy');
 
class FirstPrincipleAnalyzer {
  /**
   * @param {AnalysisStrategy} strategy - 分析策略
   */
  constructor(strategy) {
    if (!(strategy instanceof AnalysisStrategy)) {
      throw new Error('strategy must be an instance of AnalysisStrategy');
    }
    this.strategy = strategy;
    this.state = {
      problem: '',
      assumptions: [],
      whyChain: [],
      fundamentalTruths: [],
      solutions: [],
      comparison: {}
    };
  }
 
  /**
   * 模板方法:执行完整分析流程
   * @param {string} problem - 问题描述
   * @returns {object} 分析报告
   */
  analyze(problem) {
    this.state.problem = problem;
    
    // 阶段 1:问题分类(已预先完成)
    this.classifyProblem(problem);
    
    // 阶段 2:假设识别与质疑
    this.identifyAndChallengeAssumptions(problem);
    
    // 阶段 3:逐层分解(5Why)
    this.decomposeWithFiveWhys(problem);
    
    // 阶段 4:基本真理验证
    this.verifyFundamentalTruths();
    
    // 阶段 5:重构方案生成
    this.generateSolutions();
    
    // 阶段 6:类比方案对比
    this.compareWithTraditionalApproaches();
    
    // 阶段 7:生成报告
    return this.generateReport();
  }
 
  classifyProblem(problem) {
    // 由策略决定分类逻辑(可重写)
    this.state.category = this.strategy.getName();
  }
 
  identifyAndChallengeAssumptions(problem) {
    this.state.assumptions = this.strategy.identifyAssumptions(problem);
  }
 
  decomposeWithFiveWhys(problem) {
    this.state.whyChain = [];
    for (let level = 1; level <= 5; level++) {
      this.state.whyChain.push({
        level,
        question: this.strategy.getWhyQuestion(problem, level),
        answer: null // 由用户交互填充
      });
    }
  }
 
  verifyFundamentalTruths() {
    this.state.truthCriteria = this.strategy.getTruthVerificationCriteria();
  }
 
  generateSolutions() {
    this.state.solutions = this.strategy.generateSolutions(this.state.fundamentalTruths);
  }
 
  compareWithTraditionalApproaches() {
    this.state.comparison.dimensions = this.strategy.getComparisonDimensions();
  }
 
  generateReport() {
    return {
      category: this.state.category,
      problem: this.state.problem,
      assumptions: this.state.assumptions,
      whyChain: this.state.whyChain,
      truthCriteria: this.state.truthCriteria,
      fundamentalTruths: this.state.fundamentalTruths,
      solutions: this.state.solutions,
      comparison: this.state.comparison
    };
  }
 
  /**
   * 填充 5Why 答案
   * @param {number} level - 层级
   * @param {string} answer - 答案
   */
  setWhyAnswer(level, answer) {
    Eif (this.state.whyChain[level - 1]) {
      this.state.whyChain[level - 1].answer = answer;
      
      // 如果到达第 5 层,提取基本真理
      if (level === 5) {
        this.state.fundamentalTruths = [answer];
      }
    }
  }
}
 
module.exports = { FirstPrincipleAnalyzer };