Factory pattern in a nutshell is using the same source for all creation of a particular class, such that all instances can be prepared identically. The source being a method, probably a member of a class, you always reference this method to create a new instance instead of calling "new Class" directly in your implementation. The advantage of this is that when your implementation wants all instances to have a particular property set you can set it in this central method before it gets returned instead of having to run around your code and find everytime you instantiated it. Or even better, the factory method returns a subclass that has this property/function already built in.
http://en.wikipedia.org/wiki/Factory_method_pattern Element is not an instance nor does it a extend from Class/ Class.create so you can't swap this sort of functionality. The following will not work as you're anticipating. var CanvasElement = Class.create(Element, { myMethod : function(){ // } }); var myCanvas = new CanvasElement(); myCanvas.myMethod(); //fail -- http://positionabsolute.net On Jul 14, 12:26 pm, ronman <[email protected]> wrote: > OK on using addMethods. Makes sense, and it's working for me. Very > nice. Thanks. > > About not assigning functions after creation without a good factory > method, I'm confused. Isn't that what the example is doing on the > Prototype example at: http://prototypejs.org/learn/class-inheritance > ? > > // define a module > var Vulnerable = { > wound: function(hp) { > this.health -= hp; > if (this.health < 0) this.kill(); > }, > kill: function() { > this.dead = true; > } > > }; > > // the first argument isn't a class object, so there is no > inheritance ... > // simply mix in all the arguments as methods: > var Person = Class.create(Vulnerable, { > initialize: function() { > this.health = 100; > this.dead = false; > } > > }); > > var bruce = new Person; > bruce.wound(55); > bruce.health; //-> 45 > > On Jul 13, 4:22 pm, Matt Foster <[email protected]> wrote: > > > The second argument is accepting attributes of the HTML element. It > > isn't just assigning values to the object like Object.extend style. > > > I'd recommend using Element.addMethods, and scope it to only canvas > > elements, that way each time you create a canvas you'll have this > > method available, but it won't pollute other elements with this > > special functionality. > > Element.addMethods('canvas', ...); > > >http://prototypejs.org/api/element/addMethods > > > You could also assign the function value after creation, but unless > > you're using a good factory pattern, you could easily get instances of > > canvas without your specialized method. You can't send methods to the > > constructor like that, just won't work. The declaration for Element > > is a bit bizarre so it can be hard to find, so here it is... > > > (function() { > > var element = this.Element; > > this.Element = function(tagName, attributes) { > > attributes = attributes || { }; > > tagName = tagName.toLowerCase(); > > var cache = Element.cache; > > if (Prototype.Browser.IE && attributes.name) { > > tagName = '<' + tagName + ' name="' + attributes.name + '">'; > > delete attributes.name; > > return Element.writeAttribute(document.createElement(tagName), > > attributes); > > } > > if (!cache[tagName]) cache[tagName] = Element.extend > > (document.createElement(tagName)); > > return Element.writeAttribute(cache[tagName].cloneNode(false), > > attributes); > > }; > > Object.extend(this.Element, element || { }); > > if (element) this.Element.prototype = element.prototype; > > > }).call(window); > > > -- > > >http://positionabsolute.net > > > On Jul 13, 2:40 pm, ronman <[email protected]> wrote: > > > > Why doesn't 'getCoordinates' show up in the following code? > > > canvas.getCoordinates() throws an error of "getCoordinates is not a > > > function" > > > > I'm making a newbie error somewhere. > > > > var canvas = new Element('canvas', { > > > width: this.xform(data.size[0]) + 16 + 'px', > > > height: this.xform(data.size[1]) + 16 + 'px', > > > > // mixin to Element for canvases > > > getCoordinates: function(element){ > > > var position = this.getPosition(element), size = this.getSize > > > (); > > > var obj = {left: position.x, top: position.y, width: size.x, > > > height: size.y}; > > > obj.right = obj.left + obj.width; > > > obj.bottom = obj.top + obj.height; > > > return obj; > > > }, > > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
