Static Factory Methods

Static Factory Methods

Post by Michael J. Bergi » Sat, 21 Jun 2003 13:50:55



How are static factory methods implemented in Objective-C?  In Java I do
this:

public static MyClass createMyClass( String var ) {
    return (MyClass)hashTable.get(var);

Quote:}

I have copied this with -init methods however this still causes the memory
to be allocated in alloc which I want to avoid but writing an allocWith...
Method seems like a very bad idea.  Is it normal in Objective-C to have
static factory methods such as +createMyClass ?

(Sorry to be asking so many questions, I've read a book and the Apple docs
but the design patterns don't seem to be emphasized)

 
 
 

Static Factory Methods

Post by Michael As » Sat, 21 Jun 2003 14:10:20




Quote:> How are static factory methods implemented in Objective-C?  In Java I do
> this:

> public static MyClass createMyClass( String var ) {
>     return (MyClass)hashTable.get(var);
> }

> I have copied this with -init methods however this still causes the memory
> to be allocated in alloc which I want to avoid but writing an allocWith...
> Method seems like a very bad idea.  Is it normal in Objective-C to have
> static factory methods such as +createMyClass ?

That's what the + means, a static class method.

In your .h:

+ myClassMethod;

In your .m:

+ myClassMethod
{
   // do stuff

Quote:}

If you want a factory method, then in your // do stuff, you should
return an object.

 
 
 

Static Factory Methods

Post by David Ste » Sun, 22 Jun 2003 04:58:49



Quote:

> to be allocated in alloc which I want to avoid but writing an allocWith...
> Method seems like a very bad idea.  Is it normal in Objective-C to have
> static factory methods such as +createMyClass ?

Sure.  Normally you create objects with messages such as +new or +sprintf:
and so on.

Methods like allocWith are indeed a very bad idea, because they tend to
describe how things are implemented (in low level terms as "allocation") as
opposed to what the messages should actually do (as in terms of "create me
a new instance" or "return me the shared instance to do something".

So at all costs one normally avoids allocWith

 
 
 

Static Factory Methods

Post by Marcel Weih » Tue, 24 Jun 2003 05:20:58



Quote:>How are static factory methods implemented in Objective-C?

There are no static factory methods in Objective-C.  There are
class methods, which you might call factory methods because
classes are also known as factory objects.  However, these
are not static, they are fully dynamic just like any other
method, because classes are also just objects.

Quote:> In Java I do
>this:
>public static MyClass createMyClass( String var ) {
>    return (MyClass)hashTable.get(var);
>}

So what you are trying to do is have a class-method that returns existing
(cached) instance?  Easy:

+createMyClass:(NSString*)var
{
        return [[self instanceDictionary] objectForKey:var];

Quote:}

assuming +instanceDictionary will return a dictionary with the
instances you are looking for.

Quote:>I have copied this with -init methods however this still causes the memory
>to be allocated in alloc which I want to avoid but writing an allocWith...
>Method seems like a very bad idea.  Is it normal in Objective-C to have
>static factory methods such as +createMyClass ?

It is quite common to have different class methods, yes, and these often
return instances.  In fact, virtually all instances are returned by
class methods of some shape or form, because Objective-C does not
have anything like special "constructors".  So +alloc is just a
class method (factory method) that returns a freshly allocated instance
with the isa pointer set up to point to the class and the rest
initialized to zero.

You can always implement your own, no magic about it:

        +alloc
        {
                struct objc_object *new_object=calloc( 1, (Class)self->instance_size );
                new_object->isa=self;
                return new_object;
        }

Marcel