代码之家  ›  专栏  ›  技术社区  ›  An-droid

typescript中的复合设计模式搜索

  •  0
  • An-droid  · 技术社区  · 6 年前

    我的定义模式如下:

    ConditionInterface: Interface {
      getId(): string;
    }
    
    class FilterCompositeModel {
        condition: MultiConditionModel;
    
    /**
      * Add a condition to a multiCondition
      *
      * @param {ConditionInterface} toAddCondition
      * @param {MultiConditionModel} parentCondition
      */
      public addCondition(toAddCondition: ConditionInterface, parentCondition: 
      MultiConditionModel): void {
         // Find parentCondition and add toAddCondition to it
      }
    }
    
    class MultiConditionModel implements ConditionInterface {
        id: string;
        elements: ConditionInterface[];
    
      getId(): string {
        return this.id;
      }
    }
    
    class SingleConditionModel implements ConditionInterface {
        id: string;
    
      getId(): string {
        return this.id;
      }
    }
    

    如果我们将其视为树表示,那么conditioninterface要么是叶,要么是节点。我的研究指出,只有通过节点搜索的人对我没有帮助。

    我如何实现 addCondition 作为递归函数搜索所有子节点?我不知道怎么做。

    0 回复  |  直到 6 年前
    推荐文章