27
loading...
This website collects cookies to deliver better user experience
module
word could have different meanings for each person and lead to confusion.module
as a set of files or components that involves a use case or functionality in our application just for one actor. I have to say that this definition is no as specific as I would like to be because it relies on the programmer's experience to decide on what implies an actor and a use case.setInterval
method to accomplish that.var number = 0;
var id = setInterval(function() {
console.log(number);
number++;
}, 1000);
Timer
entity that will be in charge of executing some functionality every second of a frequency passed. The code for the Timer
entity could be like the following:function Timer(frequency) {
let intervalId = null;
return {
start: function(job) {
intervalId = setInterval(job, frequency * 1000);
},
stop: function() {
if(null !== intervalId) {
clearInterval(intervalId);
}
}
};
}
start
the timer and another to stop
it. A timer is just a tool for our system with no other concern than run tasks with a frequency.Job
entity that will be in charge of executing a task. In our case, we have to define one job for printing the Natural numbers sequence:function PrintNaturalNumbersJob() {
let number = 0;
return {
next: function() {
console.log(number);
number++;
}
};
}
Next
method prints the current number and increments the same variable for the next time.App
entity defined in our code. It will combine both previous entities in the following code:function App() {
let frequency = 1; // In seconds
let timer = new Timer(frequency);
let job = new PrintNaturalNumbersJob();
timer.start(job.next);
}