Skip to content

Instantly share code, notes, and snippets.

@andresmijares
Created November 2, 2016 14:18
Show Gist options
  • Save andresmijares/d0e70f259fde5c5cd04f728caa0412c6 to your computer and use it in GitHub Desktop.
Save andresmijares/d0e70f259fde5c5cd04f728caa0412c6 to your computer and use it in GitHub Desktop.
Flyweight Sample Implementation
var Task = function(data) {
this.flyweight = Flyweight.get(data.project, data.priority, data.user, data.completed);
this.name = data.name; //this is the only unique property
// this.priority = data.priority;
// this.project = data.project;
// this.user = data.user;
// this.completed = data.completed;
}
function Flyweight(project, priority, user, completed) {
this.priority = data.priority;
this.project = data.project;
this.user = data.user;
this.completed = data.completed;
}
var FlyweightFactory = function(){
var flyweights = {};
var get = function(project, priority, user, completed){
//get all the things that are not unique
if (!flyweights[project + priority + user + completed]) {
flyweights[project + priority + user + completed] =
new Flyweight(project, priority, user, completed);
}
return flyweights[project + priority + user + completed];
};
var getCount = function() {
var count = 0;
for(var f in flyweights) count++;
return count;
}
return {
get: get,
getCount : getCount
};
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment