Talk:Abstract factory pattern
This article is rated Start-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||||||||||||||||||||||||||||||||
|
Alternative implementation?
[edit]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)
- It seems you are right: it's some traditional factory. That's one reason more to delete it. One code sample is sufficient. Adding all possible variations does not provide added value ( MOS:CODE supports this point of view, see also this other section). If moreover the alternate implementation creates some confusions, it shall be deleted. Christophe (talk) 13:38, 25 February 2023 (UTC)
Best explanation
[edit]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).
Problems with code examples and UML diagram
[edit]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://wiki.riteme.site/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)
Huge image
[edit]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 think the image is inappropriate because there's no reason to have the abstract factory have two different create methods. The static diagram (the UML class diagram) really only needs to show the different participants in the pattern in a clean, simple way. While this image does show the factory pattern I don't think it's the best way to show it. Also, I think it would be useful to show it in the context of the example code, if we're going to do GUIToolkits, then let's show GUIToolkits in the UML. Here's an example of what it could look like (much simpler). Also, this diagram and the notation that shows the roles of the classes in the pattern is inspired by Allen Holub's book Holub on Patterns.
- [[Image:Abstract_factory.png|]]
- Bdean42 04:09, 3 September 2007 (UTC)
- Looking at how terrible the image currently on the page looks, I say we should replace it with this new one as soon as possible. That is unless there are any objections. Bdean42 04:26, 3 September 2007 (UTC)
- I changed the image to the new one. The old image can be found at here Bdean42 22:18, 3 September 2007 (UTC)
- I just noticed that the UML class diagram that was previously used is nearly an exact copy of the one at the following link. The site it was copied from is copyrighted and has a terms of service that doesn't seem to be compatible with wikipedia. same thing here Bdean42 02:19, 4 September 2007 (UTC)
When to Use
[edit]I'd like to see a "When to use" Section along with a discussion of pros/cons about using this pattern.
JAVA Code Sample
[edit]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)
C++ Code Sample Error
[edit]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.
Comment on behalf of 12.160.193.229
[edit]Someone should remove the autoptr - it confuses a reader inregards to the actual point of the article. JoshHolloway 17:44, 20 December 2006 (UTC)
Removal of other language examples
[edit]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)
- There are about 200 different languages listed in http://en.wikibooks.org/wiki/List_of_hello_world_programs . Where would you like the line be drawn? In my opinion even the current examples are too much. If an example is given, it should be given in pseudo code. —The preceding unsigned comment was added by 194.29.198.121 (talk) 12:30, 12 March 2007 (UTC).
- Of course not 200. But more than one. Patterns like this are only relevant if the language has good support for modern OO principles. Personally, I'd choose C++ or C# or Java (my preference), maybe Perl or Python. I'm guessing Visual Prolog shows the techniques well, although it's not a language I'm familiar with. peterl 04:04, 13 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.
- Wikipedia is not a specialized programming manual, I suggest that people buy a manual for their language of choice. I have removed again the extra examples. (Python and PHP this time). --Enric Naval (talk) 17:19, 31 January 2010 (UTC)
- I removed extra example again (this time C# and two examples in Python). There are code examples linked from the external links section. --Enric Naval (talk) 20:52, 28 November 2010 (UTC)
- I think, on the contrary, that good examples written in widely used languages implementing OO programmation (such as Java, C# and C++) are very much useful. Abstract principles is good but concrete examples make them much more easy to understand ! Ptyxs (talk) 13:12, 14 November 2011 (UTC)
Unrealistic / Inappropriate Example
[edit]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.
- Ever heard of Java? "Write once, run anywhere" —Preceding unsigned comment added by 141.228.106.136 (talk) 16:50, 11 January 2008 (UTC)
- Sorry, but do you really think that's what matters to people that come to this page? It's principle that's important here, not the details.
- User:NickHounsome This is not just a detail. It is the reason why factory patterns are not as popular in C++ as in languages such as Java or C#. In those languages it is easy to remove all compile time dependencies from the factory by using configuration files, dynamic loading and reflection. There is no portable way to do this in C++.
- In cases where "libraries" for multiple types of factory really are available it is possible in some circumstances to remove compile time dependencies using dictionaries initialized from the construction of static objects but this relies on having no static initializer requesting a factory and fiddling with the linker to ensure that the necessary object files are included as there will be no direct reference to them.
C++ example code is wrong.
[edit]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)
Where is the source code from?
[edit]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)
- The Java example was added in 12 September 2006 [1], and the author said that he simply adapted the C# example [2]. The C# example was added in 31 May 2003 by Benno (talk · contribs) [3]. His only other contribution is a "Hello World!" example in another article. He gives no clue on the origin of the code or on his identity.
- Either he copied it from a book, or he got it from a Microsoft website, or he wrote it from scratch. Googling the code is useless because it has been copy/pasted into zounds of blogs and powerpoint presentations, and google doesn't allow to discriminate search results by date.
- Maybe I can get a code sample from some book in the library. --Enric Naval (talk) 10:51, 4 February 2010 (UTC)
Class Diagrams and UML Diagrams section headings are misused
[edit]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)
- There are two possibilities:
- You think "Rational Class Diagram" is a useful and encyclopedic standard: the image is kept and displayed as a "Rational Class Diagram".
- You think "Rational Class Diagram" is useless: let's remove it. Ftiercel (talk) 21:18, 27 November 2013 (UTC)
- I think it's worthwhile to keep both diagrams. Just re-label the first one as "UML Class Diagram" and the second one as "Rational Class Diagram."
Benevolentprof (talk) 02:13, 1 December 2013 (UTC)
UML class and sequence diagram
[edit]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)
Time for some cleaning?
[edit]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:
- DONE: The introduction must be shortened to 1 or max 2 paragraphs and must be kept general. Detailed explanation must be moved elsewhere in the article. See also Wikipedia:WikiProject Computer science/Manual of style.
- DONE: Remove redundant class diagrams. The article currently contains 3 class diagrams. This redundancy (sometimes with different names) risk to confuse the readers. One class diagram and one sequence diagram should be sufficient. One diagram was wrong and was reverted to a previous correct version.
- TODO: Remove the alternative python code. One code sample is sufficient. Adding all possible variations does not provide added value. Wikipedia is not a source code library: see MOS:CODE in case of doubt. -- Moreover, it seems according to this section that the alternate example is misleading.
- DONE: Remove LEPUS3 chart section. There is no source for this chart (link broken, no relevant content found in wayback machine). The graphical formalism of LEPUS3 is not widely known, which makes this diagram of little use in an Encyclopaedia.
- DONE: cross-check external links - several seem broken. Moreover, Wikipedia is not meant to be an advertising place for third party website. The criteria regarding reliability, and independence must be cross-checked as well, according to Wikipedia:Independent_sources#Policies_and_guidelines_requiring_third-party_sources.
- DONE: describe the two main variants of this pattern, i.e. abstract class based (original GoF, written at a time where only rare languages provided native support for interfaces) and interface based. — Preceding unsigned comment added by Cth027 (talk • contribs) 20:29, 24 February 2023 (UTC)
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)
- Start-Class Java articles
- Low-importance Java articles
- Automatically assessed Java articles
- WikiProject Java articles
- Start-Class Computing articles
- Mid-importance Computing articles
- All Computing articles
- Start-Class Computer science articles
- Mid-importance Computer science articles
- WikiProject Computer science articles