jastrachan | 19 May 18:49 2004

Re: [groovy-dev] Static factory method

Agreed - I've thought we should add something like this too. Its  
actually quite easy to implement via a patch to MetaClass which is in  
charge of instantiating new objects.

The trickiest thing to decide is whats the name of the method to use as  
the 'magic constructor method' and to ensure that inside of this method  
we really do create a new instance, rather than causing infinite  
recursion :). e.g. if Class.new() is equivalent to new Class(), then we  
could look for method on a class called make() like this

class Point {
     static Point make(x, y) {
	if (x == 0 & y == 0) {
	    return origin
	else {
	    // really do the creation
	    return new(x, y)

See this page for more details...



On 19 May 2004, at 17:08, greg.barr@... wrote:
> I'm playing around with Groovy and really like the language - I can say
> what I'm thinking in very powerful ways. One idea I got to thinking  
> about
> was having an implementation of the "static factory method"  
> pattern/idiom
> (that Josh Bloch describes in his "Effective Java" book) in the  
> language.
> The caller syntax wouldn't be any different - you'd still say new  
> X(...),
> but a static method on the class would be invoked to get an instance.  
> By
> default an object would be created as it is today - by calling a
> constructor. Overriding a special method however would allow one to  
> tailor
> object creation getting all of the advantages of "static factory  
> method"
> such as caching, varying the number of objects, etc. Living in a  
> dynamic
> language buys us other benefits such as returning an "unrelated" type
> (i.e.: the returned object needn't be a subtype of the given class, but
> would need the same methods - at least those that are invoked by the
> caller). This could come in handy as a way to plug-in a new  
> implementation
> of a third-party class without impacting calling code. I'm not sure  
> what a
> good name for the method would be. I don't really like _new that much,  
> but
> couldn't think of a better name. Something like this(args) could be  
> used
> within the static method to easily invoke a constructor - args could be
> automatically unpacked and the nearest matching constuctor invoked.  
> There
> are several other details that need consideration, but thought the idea
> might spark some interesting discussion.
> class Point {
>   x; y
> }
> p = new Point() // invokes static _new method which just creates a  
> Person
> with no-arg constructor
> class Point {
>   x; y
>   // args bundled as Object, Object[], list, varargs, or other?
>   static _new(args...) {
>     // ... hook to "create" an instance
>     // could return a subtype, a cached instance, etc.
>     // syntax: this(args) can be used to call appropriate constructor
>   }
> }
> p = new Point() // invokes static _new method
> p = new Point(x:2,y:3) // invokes static _new method
> Greg Barr
> gbarr@...
> --
> "NOTICE:  The information contained in this electronic mail  
> transmission is
> intended by Convergys Corporation for the use of the named individual  
> or
> entity to which it is directed and may contain information that is
> privileged or otherwise confidential.  If you have received this  
> electronic
> mail transmission in error, please delete it from your system without
> copying or forwarding it, and notify the sender of the error by reply  
> email
> or by telephone (collect), so that the sender's address records can be
> corrected."
> _______________________________________________
> groovy-dev mailing list
> groovy-dev@...
> http://lists.codehaus.org/mailman/listinfo/groovy-dev