-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Find a pattern for having several instances of a module #2
Comments
Simple example: PersonFactory.js module: define(function() {
// Constructor
var Person = function(name) {
// Property
this.name = name;
// Getter
this.getName = function() {
return this.name;
}
}
return {
// Module' s factory method
createPerson: function(name) {
// Instantiate
return new Person(name);
}
}
}); Usage in another module: var factory = require("personFactory");
var person1 = factory.createPerson("Pepe"),
person2 = factory.createPerson("Juan");
person1.getName(); // Should return "Pepe"
person2.getName(); // Should return "Juan" |
Another solution would be that the "instantiable" module returns the "class" (Person) directly, so the instances would be created using the "new" keyword: require(["Person.class"], function(Person) { |
If the factory returned in the first solution has only one function, then both solutions are virtually the same to me; only a little difference when using it. But should we consider having more than one create* function in the factory? If we need that, should we always separate it in different modules? Maybe we also need to have some constants as part of the module. For coherence, I think that we should choose the same solution in all cases, even if we only need a single constructor. I like the factory pattern because it allows the module to contain more than a single constructor in case we need it, and the usage (Module.create(...)) makes sense to me. |
I don't know if I can define properly the problem since I haven't run into it myself, but I think the scenario is an application that has to add dynamically several instances of the same module. How are events managed by a single instance and not by all? How are these instances built? Are the instances modules themselves?
I think Oscar proposal is to create a factory module that returns objects. Problems with the context of "this" appear and changes to the message-bus are necessary.
I may have time in the following weeks to give it a try and see how we can keep oop away from geobricks-js. I have no alternative yet, though.
The text was updated successfully, but these errors were encountered: