mardi 13 novembre 2018

Composite design pattern search in typescript

I have a defined pattern as below :

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;
  }
}

If we see this as a tree representation, ConditionInterface is either a leaf or a node. My research pointed only people searching through nodes which did not help me.

How can I implement addCondition as a recursive function to search through all sub nodes ? I have no idea how to do it.

Aucun commentaire:

Enregistrer un commentaire