js策略模式使用

主要是用来优化if的

  if (this.mapHighlightInfo.highlightType == "instant-river") {
              let highInfo: InstantRiverHighlightInfo = <InstantRiverHighlightInfo>this.mapHighlightInfo;
              if (highInfo.subHighlightInfo.subType == "instant-river-sensor-station") {
                this.labelPoints = highInfo.subHighlightInfo.relatedSensorReports.map(report => {
                  return {
                    lat: report.lat,
                    lng: report.lng,
                    properties: <InstantWaterReport>report
                  }
                });
              }else if (highInfo.subHighlightInfo.subType == "instant-river-primary-sensor-stations" || highInfo.subHighlightInfo.subType == "instant-river-grade") {
                this.labelPoints = highInfo.subHighlightInfo.sensorReports.map(report => {
                  return {
                    lat: report.lat,
                    lng: report.lng,
                    properties: <InstantWaterReport>report
                  }
                });
              }
          } else if (this.mapHighlightInfo.highlightType == "weekly-river") {
              let highInfo: WeeklyRiverHighlightInfo = <WeeklyRiverHighlightInfo>this.mapHighlightInfo;
              if (highInfo.subHighlightInfo.subType == "weekly-river-sensor-station") {
                this.labelPoints = highInfo.subHighlightInfo.relatedSensorReports.map(report => {
                  return {
                    lat: report.lat,
                    lng: report.lng,
                    properties: <InstantWaterReport>report
                  }
                });
              }else if (highInfo.subHighlightInfo.subType == "weekly-river-primary-sensor-stations" || highInfo.subHighlightInfo.subType == "weekly-river-grade") {
                this.labelPoints = highInfo.subHighlightInfo.sensorReports.map(report => {
                  return {
                    lat: report.lat,
                    lng: report.lng,
                    properties: <InstantWaterReport>report
                  }
                });
              }
          } else if (this.mapHighlightInfo.highlightType == "lake-river") {
            let highInfo: LakeRiverHighlightInfo = <LakeRiverHighlightInfo>this.mapHighlightInfo;
            if (highInfo.subHighlightInfo.subType == "lake-river-sensor-station") {
                this.labelPoints = highInfo.subHighlightInfo.relatedSensorReports.map(report => {
                  return {
                    lat: report.lat,
                    lng: report.lng,
                    properties: <InstantWaterReport>report
                  }
                });
              }else if (highInfo.subHighlightInfo.subType == "lake-river-primary-sensor-stations" || highInfo.subHighlightInfo.subType == "lake-river-grade") {
                this.labelPoints = highInfo.subHighlightInfo.sensorReports.map(report => {
                  return {
                    lat: report.lat,
                    lng: report.lng,
                    properties: <InstantWaterReport>report
                  }
                });
              }
          } else if (this.mapHighlightInfo.highlightType == "drinking-river") {
            let highInfo: DrinkingRiverHighlightInfo = <DrinkingRiverHighlightInfo>this.mapHighlightInfo;
            if (highInfo.subHighlightInfo.subType == "drinking-river-sensor-station") {
                this.labelPoints = highInfo.subHighlightInfo.relatedSensorReports.map(report => {
                  return {
                    lat: report.lat,
                    lng: report.lng,
                    properties: <InstantWaterReport>report
                  }
                });
              }else if (highInfo.subHighlightInfo.subType == "drinking-river-primary-sensor-stations" || highInfo.subHighlightInfo.subType == "drinking-river-grade") {
                this.labelPoints = highInfo.subHighlightInfo.sensorReports.map(report => {
                  return {
                    lat: report.lat,
                    lng: report.lng,
                    properties: <InstantWaterReport>report
                  }
                });
              }
          }
handles = [
        {
            type: "instant-river",
            subType: "instant-river-sensor-station",
            action: () => this.relatedSensorReportFuns()
        },
        {
            type: "instant-river",
            subType: "instant-river-primary-sensor-stations",
            action: () => this.sensorReportFuns()
        },
        {
            type: "instant-river",
            subType: "instant-river-grade",
            action: () => this.sensorReportFuns()
        },

        {
            type: "weekly-river",
            subType: "weekly-river-sensor-station",
            action: () => this.relatedSensorReportFuns()
        },
        {
            type: "weekly-river",
            subType: "weekly-river-primary-sensor-stations",
            action: () => this.sensorReportFuns()
        },
        {
            type: "weekly-river",
            subType: "weekly-river-grade",
            action: () => this.sensorReportFuns()
        },

        {
            type: "lake-river",
            subType: "lake-river-sensor-station",
            action: () => this.relatedSensorReportFuns()
        },
        {
            type: "lake-river",
            subType: "lake-river-primary-sensor-stations",
            action: () => this.sensorReportFuns()
        },
        {
            type: "lake-river",
            subType: "lake-river-grade",
            action: () => this.sensorReportFuns()
        },

        {
            type: "drinking-river",
            subType: "drinking-river-sensor-station",
            action: () => this.relatedSensorReportFuns()
        },
        {
            type: "drinking-river",
            subType: "drinking-river-primary-sensor-stations",
            action: () => this.sensorReportFuns()
        },
        {
            type: "drinking-river",
            subType: "drinking-river-grade",
            action: () => this.sensorReportFuns()
        }
    ];

匹配的代码

let type: string = this.mapHighlightInfo.highlightType;
    let subType: string = this.mapHighlightInfo.subHighlightInfo.subType;
     this.handles.forEach(h => {
         if (h.type == type && h.subType == subType) {
             h.action();
         }
     });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值