Talk:Abstract factory pattern

From Wikipedia, the free encyclopedia

The example is poor. (--anon)

This article gives the best explanation I've ever seen about what Abstract Factory Pattern really is. Clear, concise and direct to the point.( -- Aidyn).

Merged the C# and C++ examples in one section. Mostapha


I know, a UML diagram is not everything, but I think it would help here to understand the pattern. If this is ok, I'll try to add one soon. --Bjoern.thalheim 09:38, 25 October 2005 (UTC)


I'm not too thrilled with the C++ examples returning pointers to allocated memory. This puts the onus of freeing the memory onto the calling code and is bad style and error-prone because 1) those who call the code need to be aware that as a side effect it allocates memory, and 2) memory allocation code for specific objects ends up being strewn throughout the program instead of being encapsulated in a class or at the very least encapsulated in various methods of the form getControl() / destroyControl().

I modified the code so that it returns std::auto_ptr<> objects. These will be automatically deleted when they go out of scope. I know this makes the example more complex and verbose but I think it's important that coding examples exhibit good style. --Zixyer 04:17, 19 December 2005 (UTC)

I just think the code examples should be the same as the UML diagram. The diagram is great, clear, and it would help to be able to be able to compare both, like it was done in the Visitor Pattern (http://en.wikipedia.org/wiki/Visitor_pattern) -- Gabriel

[edit] Huge image

I realise this is really a technical problem with the software, but isn't a 173KB PNG image just a little excessive? This would take at least 30 seconds to grab over a 56kbps modem. Ideally the image should be palette-reduced, but it would help shrink it a lot if the diagram didn't contain fancy gradients. In any case the uploaded file should be in SVG format, to enable easier editing and scaling. We should probably also upload a temporary smaller PNG version for use in the article which is palette reduced. I'll take care of this and similar images in a while if there's no objection. Deco 15:51, 13 April 2006 (UTC)


I'd like to see a "When to use" Section along with a discussion of pros/cons about using this pattern.

[edit] JAVA Code Sample

Hello, i took the liberty of adding a Java code sample of the AbstractFactory example. It is very similar to the C# example, other than minor differences in language construct such as :/extends, public getters/setters etc. -- [Richard]

[edit] C++ Code Sample Error

The C++ examples make no sense.

From the article:
Adding new concrete types is done by modifying the client code to use a different factory, a modification which is typically one line in one file. (The different factory then creates objects of a different concrete type, but still returns a pointer of the same abstract type as before - thus insulating the client code from change.) This is significantly easier than modifying the client code to instantiate a new type, which would require changing every location in the code where a new object is created (as well as making sure that all such code locations also have knowledge of the new concrete type, by including for instance a concrete class header file). If all factory objects are stored globally in a singleton object, and all client code goes through the singleton to access the proper factory for object creation, then changing factories is as easy as changing the singleton object.

If you look at the Factory declaration it's a pure abstract class, since it has a virtual function who's address has been set to 0 (i.e it has no implementation). This means that you can't call getFactory without first getting a pointer to one of the concerete classes and you can't because you can't call getFactory.