I'm sick of people throwing the word 'factory' around.
So often people use it to describe any 'class' that's somehow involved in some 'pattern'.
It just pisses me off.
Here's an indication (courtesy GoF) of what a factory really is:
Abstract Factory: An interface for creating objects without specifying their concrete classes.
e.g.
public interface IWidgetFactory {
IScrollBar CreateScrollBar();
IWindow CreateWindow();
}
public class MyWidgetFactory : IWidgetFactory {
IScrollBar IWidgetFactory.CreateScrollBar() { return new MyScrollBar(); }
IWindow IWidgetFactory.CreateWindow() { return new MyWindow(); }
}
In this case, IWidgetFactory is the 'abstract factory' and MyWidgetFactory is the 'factory'.
Factory Method: An interface for creating an object that defers instantiation to subclasses. A subclass decides what concrete class to instantiate.
e.g. From the Abstract Factory example an implementation of IWidgetFactory.CreateScrollBar (like the implementation on MyWidgetFactory) is a Factory Method.
The reason that the Factory Method is mentioned separately is because often you don't have a dedicated abstract factory. Sometimes you just have an interface that does 'all sorts of things' and one of the things that it will do is create stuff for you.
That's it. That's what a factory is. It's something that has a method that will return an instance of a thing for you, so that you only have to concern yourself with the interface of the object it will return, and not the particulars of a concrete implementation.
John.
Nice! I was just in a patterns training class and The Gof book was the main text. It is a great text. The fact that it is largely unchanged for the last 10 years and still used as a class text is indicitive of its soundness. I seem to have aquired the electronic version of it if you are interested.
I think all programmers who have read it hold it in the same regard as the K&R C book.
I won't need the electronic one though thanks Matthew, I've got my hardcopy gathering coffee stains right here.. :)
I notice that it's actually visible in this picture:
https://www.jj5.net/blog-archive-2005/1708.html
Burried in the mess though.. :P
John.
Oh I have a hardcopy too...I just love textual searches though. :)
You mean you do know the whole book off by heart yet..? :)
I actually really do like books that ship with an electronic version. It's so much better reading from a screen. Turning pages is so cumbersome, and such a waste of time.
I have been reading a book before, and thought "I'll just search for 'fubar'" and then realised that I was reading a book... :P
I learned C# from the electonic version of Insdie C# from MS Press. I read the whole book in one sitting, from cover to cover...
Still, I wouldn't be real happy taking an electronic version of a book from a stranger on the internet. Knowing my luck, someone would use it as a basis to sue me for something. (Seriously, I have the worst luck)
I don't refer to my GoF book that much these days, so I won't really have much use of an electronic version. Thanks for the offer though.
If you want to read a cool electonic book, read this one:
http://www.virtualschool.edu/mon/Quality/PirsigZen/
John.