Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Gang Of Four "Design Patterns; Elements of Reusable Object-Oriented Software" book contains two entries, "Abstract Factory" (aka 'Virtual Constructor') and "Factory Method"... I dunno about "Concrete Factory." I've heard the term but never given it too much thought.</p> <h2>Factory Method</h2> <p>In "Factory Method" an object has a method which is responsible for the instantiation of another object. A common example would be the JavaScript document object and the creation of <code>HtmlElement</code> objects:</p> <pre><code>var newDiv = document.createElement('div'); </code></pre> <p>This isn't a great example though, as an important part of the Factory Method is polymorphism. If I could extend <code>document</code> to define another class which defines another <code>createElement</code> this would be prime Factory Method material.</p> <h2>Abstract Factory</h2> <p>An abstract factory is meant to "provide an interface for creating <em>families of related or dependent objects</em> without specifying concrete classes.</p> <p>The typical straight-out-of-the-book example is a Widget Factory; back in the day when the GoF was published, cross-platform GUI development was a bit of a hassle, so you could define an abstract widget factory class.</p> <p>That class could have methods <code>createWindow</code>, <code>createButton</code>, <code>createScrollBar</code> etc. In turn, several implementations would be defined to produce Swing widgets or AWT or whatever. Then, depending on configuration, the different class would be instantiated.</p> <h2>Addendum - Concrete Factory</h2> <p>I believe that a Concrete Factory is any non-abstract implementation of Abstract Factory or Factory method.</p> <p>So, when I write my own generalization of <code>document</code> which overrides <code>createElement</code>, the class I create is a Concrete Factory.</p> <p>Likewise, while <code>WidgetFactory</code> would be an Abstract Factory, <code>SwingWidgetFactory</code> would be a concrete factory.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload