First of all I would like to tell this is my course work project question but I think this is really advanced one for my level. I am given a scenario in which I hava EventCounter object with single property named capturedEvents like this:
function EventCounter()
{
this.capturedEvents = {};
}
EventCounter.prototype.capture = function (eventName)
{
if (typeof this.capturedEvents[eventName] === 'undefined') {
this.capturedEvents[eventName] = 0;
}
this.capturedEvents[eventName]++;
}
EventCounter has one method named capture that captures the value of event when triggered from any where in the application. But the capturedEvents property of the EventCounter is susceptible to be change or deleted from other places within the application. I want to prevent unuthorized update/deletion of value of the capturedEvents property.
So I have implemented the same thing using closures like below:
function EventCounter() {
var capturedEvents = {};
this.update = function (eventName)
{
if (typeof capturedEvents[eventName] === 'undefined') {
capturedEvents[eventName] = 0;
}
capturedEvents[eventName]++;
}
this.getCounter = function ()
{
return capturedEvents;
}
}
I want to ask is this approach safe enough? I am also given a hint to use Mediator desing pattern but I have no idea about. Can anyone give his input if this is the proper way or there is something better that can be done? And how to use mediator for this? Thanks a lot
Aucun commentaire:
Enregistrer un commentaire