The "Alternative implementation" Python example does not implement the abstract factory pattern. — Preceding unsigned comment added by 45.72.223.70 (tal
| This article is rated Start-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
The "Alternative implementation" Python example does not implement the abstract factory pattern. — Preceding unsigned comment added by 45.72.223.70 (talk) 20:41, 24 February 2023 (UTC)
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
The example is poor. (--anon) f
We need a FactoryCreator class, which will create a specific factory based on configuration supplied by the application/client. Then the application will use that factory to create desired product.
class FactoryCreator {
public static AbstractFactory createFactory (Config conf) {
if (conf.type()==windows) {
return new WinFactory();
} else if (conf.type()==osx) {
return new OSXFactory();
}
}
}
class Application {
public static void main(String arg[]) {
AbstractFactory af = FactoryCreator.createFactory(getSystemConfig());
Button button = af.createButton(); //Button product
Menu menu = af.createMenu(); // Menu product
}
}
-- sowrov — Preceding unsigned comment added by 117.103.81.14 (talk) 06:58, 15 April 2014 (UTC)
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.
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]
Hello, Richard. Your Java code does not compile. In order to get the name of the OS, you do something like
String name = System.getProperty("os.name");
Therefore, in order for the createOsSpecificFactory() method to work you need
public static GUIFactory createOsSpecificFactory() {
String os = System.getProperty("os.name");
/* we're not interested in which version of Windows it is,
so we only look at the first three letters */
if ( os.substring(0,3).equalsIgnoreCase("win") )
return new WinFactory();
else
return new OSXFactory();
}
Regards, Mihai
Hi,
'var' is not a Java keyword. So this is not correct.
Greets, Bart. — Preceding unsigned comment added by 82.94.187.71 (talk) 14:49, 19 August 2019 (UTC)
'var' can be used for local variable declaration since Java 10. It is not a keyword but has special meaning (JLS-3.9).
--Strohtaler (talk) 00:19, 5 November 2019 (UTC)
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.
Someone should remove the autoptr - it confuses a reader inregards to the actual point of the article. JoshHolloway 17:44, 20 December 2006 (UTC)
How come other language examples have been removed. They looked useful to me. There was no discussion here. Any reason? peterl 10:58, 5 March 2007 (UTC)
I think the examples are still too many and clutter the page. --M4gnum0n (talk) 08:57, 23 January 2008 (UTC)
I agree with you, that such an amount identical of examples (C# and Java examples differ very slightly!) is unnecessery. That's why I removed all but one (say Java). If you find it wrong, feel free to restore.
The "Windows vs. OSX" button example is absurd. GUIFactory depends on both WinFactory and OSXFactory. This implies that the software can be compiled once and then deployed on either a Windows PC or an OSX machine; that it will contain code for both WinFactory and OSXFactory, and choose the correct one at runtime. OSXFactory and OSXButton will depend on some OSX headers and libraries that wouldn't exist on a Windows PC and vice versa. There is probably no way to successfully link/load this software, since only one set of headers will be available.
The C++ example is wrong. Maybe not the factory pattern itself, but it contains really bad code which misses several deletes, plus the last line ("delete x, y;") will only delete one of x and y.
See following demonstration:
#include <iostream>
struct Foo {
Foo () { ::std::cout << "Foo::Foo(), this = " << this << '\n'; }
~Foo () { ::std::cout << "Foo::~Foo(), this = " << this << '\n'; }
};
int main () {
Foo* a = new Foo();
Foo* b = new Foo();
delete a, b;
}
Output:
Foo::Foo(), this = 0x929a008 Foo::Foo(), this = 0x929a018 Foo::~Foo(), this = 0x929a008
Whoever wrote that example should hand over his c++ coding license.
Phresnel (talk) 12:39, 11 January 2009 (UTC)
From which reliable source source does the code come from? All content in WP must be veriafiable. If no source is named for the code soon, I will remove it. Please also note, that including source code written by Wikipedia editors themselves is completely against WP:OR. Offliner (talk) 22:46, 7 April 2009 (UTC)
Class diagrams are a sub-type of UML diagram.
The so-called UML diagram shown in the article is a class diagram, as produced by IBM Rational Software. Although, it's supposed to be a UML tool, it does not produce standard UML, i.e. diagrams that conform to the standard.
It would be better to rename the sections "UML Class Diagram" and "Rational Class Diagram", or something similar. It makes no sense the way it is now. Currently, the titles suggest that Class Diagrams and UML diagrams are orthogonal, and they're not.
Benevolentprof (talk) 05:30, 26 November 2013 (UTC)
Benevolentprof (talk) 02:13, 1 December 2013 (UTC)
I would like to share the following UML diagrams I have published on Design Patterns Open Online Learning. Your comments are welcomed!

In the above UML class diagram,
the Client class that requires ProductA and ProductB objects doesn't instantiate the ProductA1 and ProductB1 classes directly.
Instead, the Client refers to the AbstractFactory interface for creating objects,
which makes the Client independent of how the objects are created (which concrete classes are instantiated).
The Factory1 class implements the AbstractFactory interface by instantiating the ProductA1 and ProductB1 classes.
The UML sequence diagram shows the run-time interactions:
The Client object calls createProductA() on the Factory1 object, which creates and returns a ProductA1 object.
Thereafter,
the Client calls createProductB() on Factory1, which creates and returns a ProductB1 object.
Vanderjoe (talk) 17:05, 15 July 2017 (UTC)
This article is historically grown and provides lots of useful content. However, it seems that some cleaning actions are required, or it will soon be very difficult to read and maintain:
If you want to contribute to the cleaning action, just answer under the relevant TODO item you'll take care of, to avoid that we're walking on each-other's feet with multiple concurrent edits of the same content ;-)
If one of these items is not a good idea, please answer as well under the right argument, providing some counter arguments to the Wikipedia Manual of Style recommendation to which I referred. Christophe (talk) 22:54, 12 February 2023 (UTC)
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.