Class Factory

ClassFactory - one of several DesignPatterns that abstract the creation of object instances.

If you find yourself wishing for a VirtualConstructor (creation of instances from a runtime-selected class), you probably need some kind of ClassFactory:


I'm not sure I understand why you call these "class factories"? Surely it's objects they're making. Am I missing something? (If it weren't for the second sentence of the page, I could just assume that actually "class" is parallel to "category" but at a lower level of the taxonomy...)

I've been contemplating the utility of MetaClasses lately. That is, classes whose instances are also classes. Can anyone weigh in on the utility of such beasts? I'm going to think about this some more, and hopefully implement one, and post my own thoughts. -- AndrewMccormick

Perhaps "InstanceFactory?Method/Function/Object" would be "more correct." But we've gotten into the habit of calling them ClassFactories. -- JeffGrigg

Yes, surely a ClassFactory is where SoftwareLabourers work? -- MatthewAstley

Andrew: Yes, in a language that has classes, it's useful for those classes to be objects that you can pass around - and so if every object is supposed to be an instance of a class, then a class ought to be an instance of a metaclass. That's the way it works in Smalltalk, and it's pretty good. The Point class object is a factory for Point objects. And constructor methods get inherited properly, and all that.

An even better approach, I think, is to just get rid of classes altogether and do PrototypeBasedProgramming. If you want a new object, you clone an existing object. This gives you all the benefits (I think) of having classes as first-class values, with none of the mind-bending confusion of metaclasses.

--AdamSpitz


[CategoryPattern]


EditText of this page (last edited August 11, 2009) or FindPage with title or text search