-1

I am trying to implement strategic design pattern .

I have simple if-else ladder as below:

       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']);
        }

and i implemented strategic pattern upon it:

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

but i am confused as how to invoke a particular class based on condition

Like in above if-else ladder it checks for dataKeyinresponse value and then execute some statements

but here in strategic pattern how to see for the condition and then execute that class.

any help would be appreciated.

Shikha thakur
  • 1,269
  • 13
  • 34

1 Answers1

1

A simple example of strategy pattern:

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

You may take a look at this: https://www.tomasmalmsten.com/2011/01/create-factory-strategies-ifs/ to encapsulate the strategies creation in a factory.

Advice: You should name your classes with a capitalized first-letter emptyBucketInterface => EmptyBucketInterface

alexbt
  • 16,415
  • 6
  • 78
  • 87