An abstract factory class is used to define an infrastructure for modular code. What does this mean ? Say you have a situation where you have a set of filters that you might want to apply to an image. In traditional C++ code, you would make a class or method for each filter, and would then have to hard-code the name of each of the different functions wherever you want to use them. Abstract factories provide a mechanism for having a set of such filters and calling them by name at runtime, generally with introspection. Introspection meaning a program can ask (at runtime) for a list of all of the available filters, and their parameters, documentation, etc. So, a GUI presenting the user with a list of filters would not need to modify their code at all when a new filter was added to the system. It would all happen transparently. EMAN2 uses this infrastructure for a range of different types of functions, including processors (1 image in -> 1 image out), reconstructors (many images in -> 1 higher dimension image out), projectors (1 high dimension image in -> many lower dimension images out), etc. This particular file documents the infrastructure used to make this happen. You don't really need to read this in most cases unless you want to implement an entirely new type of modular class.

The EMAbstractFactory class is defined in EMAN2.py. It can be used for designing factory based python code. It is initially taken from a factory in python, where the first two discussion points (on that page) have been duly noted and related alterations have occurred.

   1         >>from EMAN2 import EMAbstractFactory
   2         >>f=EMAbstractFactory()
   3         >>class A:pass
   4         >>f.register("createA",A)
   5         >>f.createA()
   6         <__main__.A instance at 01491E7C>
   7         
   8         >>> class B:
   9         ...     def __init__(self, a,b=1):
  10         ...             self.a=a
  11         ...             self.b=b
  12         ...             
  13         >>> f.register("createB",B,1,b=2)
  14         >>> f.createB()
  15         >>> b=f.createB()
  16         >>> 
  17         >>> b.a
  18         1
  19         >>> b.b
  20         2
  21         
  22         >>> class C:
  23         ...     def __init__(self,a,b,c=1,d=2):
  24         ...             self.values = (a,b,c,d)
  25         ... 
  26         >>> f.register("createC",C,1,c=3)
  27         >>> c=f.createC(2,d=4)
  28         >>> c.values
  29         (1, 2, 3, 4)
  30         
  31         >>> f.register("importSerialization",__import__,"cPickle")
  32         >>> pickle=f.importSerialization()
  33         >>> pickle
  34         <module 'cPickle' (built-in)>
  35         >>> f.register("importSerialization",__import__,"marshal")
  36         >>> pickle=f.importSerialization()
  37         >>> pickle
  38         <module 'marshal' (built-in)>
  39         
  40         >>> f.unregister("importSerialization")
  41         >>> f.importSerialization()
  42         Traceback (most recent call last):
  43           File "<interactive input>", line 1, in 
  44         AttributeError: Factory instance has no attribute 'importSerialization'

Eman2FactoriesInPython (last edited 2011-01-15 05:44:20 by SteveLudtke)