vendredi 2 janvier 2015

When should I use event-based programming?


I have been passing callbacks or just triggering the functions from other function in my programs to make things happen once tasks complete. When something finishes, I trigger the function directly:



var ground = 'clean';

function shovelSnow(){
console.log("Cleaning Snow");
ground = 'clean';
}

function makeItSnow(){
console.log("It's snowing");
ground = 'snowy';
shovelSnow();
}


But I've read about many different strategies in programming, and one that I understand to be powerful, but have not yet practiced, is event-based (I think a method I read about was called "sub-pub"):



var ground = 'clean';

function shovelSnow(){
console.log("Cleaning Snow");
ground = 'clean';
}

function makeItSnow(){
console.log("It's snowing");
ground = 'snowy';
$(document).trigger('snow');
}

$(document).bind('snow', shovelSnow);


I'd like to understand the objective, strengths, and weaknesses of event-based programming vs just calling all of your functions from within other functions. In which programming situations does event-based programming make sense to use?





Aucun commentaire:

Enregistrer un commentaire