Just some notes when implementing it using C++:

When using C++, to make your implementation scale better you can mix the AbstractFactoy with the TemplateMethod and obtain the same result with less code:

template<class T>
class FactoryClass
{
public:
        static T* Create() { return new T(); }
};

class X {};
class Y {};
class Z {};

and then use the "FactoryClass" just like this:

auto_ptr<X> spX( FactoryClass<X>::Create() );
auto_ptr<Y> spY( FactoryClass<Y>::Create() );
auto_ptr<Z> spZ( FactoryClass<Z>::Create() );

If you want to add a new class to the abstract FactoryClass you don't need to change it's implementation, but just guarantee that the new class have a public default constructor!

PS: I decided to use the auto_ptr (that live in the "memory" header and in the "std" namespace), because it automatic manage the lifetime o the object allocated by the operator "new" and guarantee the "Basic Guarantee of Exception Safety"! But it's no relevant here... :o)


-- Eduardo Franco


The AbstractFactory pattern differs in that it is classes specifically
designed for constructing groups of related classes. For instance (again, in
Java) your application could have abstract 'ApplicationInterfaceFactory'
class or interface:

public interface ApplicationInterfaceFactory {
  public MainMenu createMainMenu();
  public DataEntry createDateEntry();
  public ResultsPresentation createResultsPresentation();
}

and we then supply as many implementations of this interface that provide
related classes:

public class AWTInterfaceFacory {
  public MainMenu createMainMenu() {
    return new AWTMainMenu();
  }
  public DataEntry createDataEntry() {
    return new AWTDataEntry();
  }
  public ResultsPresentation createResultsPresentation();
    return new AWTResultsPresentation();
  }
}

public class SwingInterfaceFacory {
  public MainMenu createMainMenu() {
    return new SwingMainMenu();
  }
  public DataEntry createDataEntry() {
    return new SwingDataEntry();
  }
  public ResultsPresentation createResultsPresentation();
    return new SwingResultsPresentation();
  }
}

public class CLIInterfaceFacory {
  public MainMenu createMainMenu() {
    return new CLIMainMenu();
  }
  public DataEntry createDataEntry() {
    return new CLIDataEntry();
  }
  public ResultsPresentation createResultsPresentation();
    return new CLIResultsPresentation();
  }
}

public class AudioInterfaceFacory {
  public MainMenu createMainMenu() {
    return new AudioMainMenu();
  }
  public DataEntry createDataEntry() {
    return new AudioDataEntry();
  }
  public ResultsPresentation createResultsPresentation();
    return new AudioResultsPresentation();
  }
}

As you can see, each of the subclasses provides a set of related objects to
the client object. An AbstractFactory pattern is a bit like a collection of
FactoryMethod patterns.

Hope that wasn't too code intensive ;-)

Russell Dodd


_______________________________________________
patterns-discussion mailing list
[EMAIL PROTECTED]
http://mail.cs.uiuc.edu/mailman/listinfo/patterns-discussion

Reply via email to