javascript - Ignore if-else ladder and implements strategic design pattern -
i trying implement strategic design pattern .
i have simple if-else ladder 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 es start date week nestedbed = new date(moment(new date(item['key'])).weekday(7).format('yyyy-mm-dd')); } else { // bed = bucketstartdate; nestedbed = new date(item['key']); }
and 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 confused how invoke particular class based on condition
like in above if-else ladder checks datakeyinresponse
value , execute statements
but here in strategic pattern how see condition , execute class.
any appreciated.
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 @ this: https://www.tomasmalmsten.com/2011/01/create-factory-strategies-ifs/ encapsulate strategies creation in factory.
advice: should name classes capitalized first-letter emptybucketinterface
=> emptybucketinterface
Comments
Post a Comment