vendredi 2 février 2018

Strategy pattern for just one function

The strategy pattern is software design pattern to selects an algorithm at runtime. See this javascript implementation for reference

See in the linked page, how the example use different classes at runtime. But, what about if you just want to select one function? Is it worth encapsulate those functions in classes? Or just selects the function to use, like in the next snipped:

function getRandomThing() {
    return Math.floor(Math.random() * thingsCount);
}

function getNextThing() {
    return currentThing++ % thingsCount;
} 

currentGetThing = getNextThing
currentGetThing()   

Is this solution correct? It works, but currentGetThing = getNextThing sounds a bit C-liked for me.

Aucun commentaire:

Enregistrer un commentaire