[Tutor] Re: Factory classes

Scot W. Stevenson scot@possum.in-berlin.de
Wed, 4 Sep 2002 01:32:31 +0200


Hi, 

> What is a factory class?  I have seen this term before, but I'm not
> sure how it is different from any other class.  Don't all class
> definitions create instances for you (handing you a reference to the
> instance)?

Let's see if I understand factories by trying to explain them [note to Erik 
and others: Before you believe _any_ of this, wait and see what the real 
programmers say <g>]: 

A factory function or class produces objects depending on the parameters 
given to it, the same way a real factory will produce different kinds of, 
say, bicycles, depending on what color, size, or type you want. You use a 
factory function when you know that you are going to need an object of a 
certain type - but not of which subtype - when you write the program. This 
is sort of like building a bicycle factory, but not knowing if red or 
green will be the favorite color next year. So you give the factory the 
ability to retool on the fly, that is, produce either a green or red 
bicycle depending on the demand.

My attempt at writing a factory function would be something like this 
(Python 2.2, I am not sure about older versions because I'm subclassing 
"object" here):

=============================================
class spraycan_factory(object):
    """Produce spraycan objects that paint walls"""

    class spraycan(object):
        """This is what we build in the factory"""
        # Fill me up with a color when I am manufactured
        def __init__(self, color):
            self.color = color
        # Spray my color all over when I am called
        def __call__(self):
            print 'Psssss! Your wall is now %s' % self.color

    # Return a spraycan of the color required
    def __call__(self, color='white'):
        new_can = self.spraycan(color)
        return new_can
===============================================

With this behaviour (file is "factory.py"): 

===============================================
Python 2.2 (#1, Mar 26 2002, 15:46:04)
[GCC 2.95.3 20010315 (SuSE)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import factory
>>> can_plant = factory.spraycan_factory()
>>> can_plant
<factory.spraycan_factory object at 0x815f9d4>
>>> can1 = can_plant('blue')
>>> can1
<factory.spraycan object at 0x8160504>
>>> can1()
Psssss! Your wall is now blue
>>> can2 = can_plant('red')
>>> can2()
Psssss! Your wall is now red
>>> can3 = can_plant()
>>> can3()
Psssss! Your wall is now white
===============================================

The term itself is out of a book called "Design Patterns: Elements of 
Reusuable Object-Orientated Software" by four gentlemen named Gamma, Helm, 
Johnson and Vlissides. It seems to be required reading for Real Computer 
Scientists: Stand next to one at a cocktail party for long enough, and 
sooner or later, they'll start talking about "the gang of four"...

Well, at least that's how I understand factories. Now if somebody could 
tell me if any of this is actually correct, I'd feel much better =8).

Y, Scot

-- 
 Scot W. Stevenson wrote me on Wednesday, 4. Sep 2002 in Zepernick, Germany  
       on his happy little Linux system that has been up for 1869 hours       
        and has a CPU that is falling asleep at a system load of 0.00.