Home > Blockchain >  How to use strategy pattern in nodejs
How to use strategy pattern in nodejs

Time:11-26

I am making some rest calls in my code based upon the value in a variable. Each of these rest calls takes a different number of parameters but the response remains the same.

if (type=="Membership"){
    reponse=await claimRepo.findDetailsByMembersip(memberNumber)
}
else if (type=="Claim"){
    reponse=await claimRepo.findDetailsByClaim(claimNumber)
}
else if (type=="Date"){
    reponse=await claimRepo.findDetailsByDate(date)
}

Now inside the claimRepo class

async findDetailsByMembersip(memberNumber){
    const data=requests.get('someurl')//make the call
    return {
        data,
        statusCode
    }
}

async findDetailsByClaim(claimNumber){
    const data=requests.get('someurl')//make the call
    return {
        data,
        statusCode
    }
}

async findDetailsByDate(date){
    const data=requests.get('someurl')//make the call
    return {
        data,
        statusCode
    }
}

Is there a way to get rid of these if-else blocks and implement this using the strategy pattern ?

CodePudding user response:

Here is the working code example mentioned below for Strategy Pattern. You can modify code according to your need;

function findDetailsByMembersip(memberNumber) {
    this.call = (memberNumber) => {
        const data = 1; //requests.get('someurl')//make the call
        return {
            data,
            memberNumber
        }
    }
}

function findDetailsByClaim(claimNumber) {
    this.call = (claimNumber) => {
        const data = 2; //requests.get('someurl')//make the call
        return {
            data,
            claimNumber
        }
    }
}

function findDetailsByDate(date) {
    this.call = (date) => {
        const data = 3; //requests.get('someurl')//make the call
        return {
            data,
            date
        }
    }
}

function StrategyManager() {
    this.strategy = null;

    this.setStrategy = (strategy) => {
        this.strategy  = strategy;
    }

    this.call = (args) => {
        return this.strategy.call(args);
    }
}

const strategy = new StrategyManager();
const membership = new findDetailsByMembersip();
const claim = new findDetailsByClaim();
const dates = new findDetailsByDate();

strategy.setStrategy(membership);
console.log(strategy.call('membership'));

strategy.setStrategy(claim);
console.log(strategy.call('claims'));

strategy.setStrategy(dates);
console.log(strategy.call('dates'));

CodePudding user response:

I'd use an obj (acting as a dictionary): keys are the type (the if condition) and values the references to the function to call

const fnsByType = {
  "Membership": claimRepo.findDetailsByMembersip,
  "Claim": claimRepo.findDetailsByClaim
  "Date": claimRepo.findDetailsByDate
}

then you might:

const doCall = async (type, param) => fnsByType[type](param)

Note that you need to provide accurate params to doCall function.

doCall("Membership", memberNumber).then(...)
doCall("Claim", claimNumber).then(...)
doCall("Date", date).then(...)
  • Related