Javascript 忽略if-else阶梯并实施战略设计模式

Javascript 忽略if-else阶梯并实施战略设计模式,javascript,java,design-patterns,typescript,strategy-pattern,Javascript,Java,Design Patterns,Typescript,Strategy Pattern,我正在尝试实施战略设计模式 我有一个简单的if-else梯形图,如下所示: if(dataKeyinresponse === 'year') { bsd = new Date(moment(new Date(item['key'])).startOf('year').format('YYYY-MM-DD')) nestedbed = new Date(moment(new Date(item['key'])).endOf('year').

我正在尝试实施战略设计模式

我有一个简单的if-else梯形图,如下所示:

       if(dataKeyinresponse === 'year') {
           bsd = new Date(moment(new Date(item['key'])).startOf('year').format('YYYY-MM-DD'))
           nestedbed = new Date(moment(new Date(item['key'])).endOf('year').format('YYYY-MM-DD'));
        } else if(dataKeyinresponse === 'quarter') {
            let tempDate = new Date(moment(new Date(item['key'])).add(2, 'months').format('YYYY-MM-DD'));
            // nestedbed = new Date(moment(new Date(item['key'])).add(3, 'months').format('YYYY-MM-DD'));
            nestedbed = new Date(moment(tempDate).endOf('month').format('YYYY-MM-DD'));
        } else if(dataKeyinresponse === 'month') {
            nestedbed = new Date(moment(new Date(item['key'])).endOf('month').format('YYYY-MM-DD'));
        } else if(dataKeyinresponse === 'week') {
            //Relying more on the ES start date for week
            nestedbed = new Date(moment(new Date(item['key'])).weekday(7).format('YYYY-MM-DD'));

        } else {
          // bed = bucketStartDate;
          nestedbed = new Date(item['key']);
        }
我在其上实施了战略模式:

interface emptyBucketInterface {
    fnGetEmptyBuckets();
}
class year implements emptyBucketInterface {
    fnGetEmptyBuckets() {
        bsd = new Date(moment(new Date(item['key'])).startOf('year').format('YYYY-MM-DD'))
        nestedbed = new Date(moment(new Date(item['key'])).endOf('year').format('YYYY-MM-DD'));
        return {
            "bsd": bsd,
            "nestedbed": nestedbed
        };
    }
}
class quarter implements emptyBucketInterface {
    fnGetEmptyBuckets() {
        let tempDate = new Date(moment(new Date(item['key'])).add(2, 'months').format('YYYY-MM-DD'));
        nestedbed = new Date(moment(tempDate).endOf('month').format('YYYY-MM-DD'));
        return {
            "tempDate": tempDate,
            "nestedbed": nestedbed
        };
    }
}
class month implements emptyBucketInterface {
    fnGetEmptyBuckets() {
        nestedbed = new Date(moment(new Date(item['key'])).endOf('month').format('YYYY-MM-DD'));
        return {
            "nestedbed": nestedbed
        };
    }
}
class week implements emptyBucketInterface {
    fnGetEmptyBuckets() {
        nestedbed = new Date(moment(new Date(item['key'])).weekday(7).format('YYYY-MM-DD'));
        return {
            "nestedbed": nestedbed
        };
    }
}
但我对如何基于条件调用特定类感到困惑

像上面的if-else梯形图一样,它检查
dataKeyinresponse
值,然后执行一些语句

但在战略模式中,这里如何查看条件,然后执行该类


任何帮助都将不胜感激。

一个简单的战略模式示例:

public class SomeClass {
    private final Map<String, EmptyBucketInterface> strategies = new HashMap<String, EmptyBucketInterface>();

    public SomeClass() {
        strategies.put("year", new Year());
        strategies.put("quarter", new Quarter());
        strategies.put("month", new Month());
        strategies.put("week", new Week());
    }

    public void doAction(String action) {
        strategies.get(action).fnGetEmptyBuckets();
    }
}
公共类SomeClass{
私有最终映射策略=新HashMap();
公共类(){
策略。把(“年”,新年());
策略。出售(“季度”,新季度());
策略。put(“月”,newmonth());
策略。放置(“周”,新周());
}
公共void操作(字符串操作){
strategies.get(action.fGetEmptyBuckts();
}
}
您可以看看这个:在工厂中封装策略创建


建议:你应该用大写的首字母命名你的类
emptyBucketInterface
=>
emptyBucketInterface

看起来你已经改变了返回对象的创建方式,但实际上没有改变与if-else梯形图相关的任何内容。您最好使用switch语句,或者使用名为‘year’、‘quarter’、‘month’函数的对象<代码>操作[datakeyinresponse]()?@编写代码的可怕袋熊typescript@jdphenix你能给我一个答案吗?因为我不明白你的评论