Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory, Abstract Factory and Factory Method

I am really confused about these three terms.

My understanding is that:

  • in the Factory pattern, there is no concrete factory. The factory builds the new objects according to the parameters.

  • in Abstract Factory pattern, there are multiple concrete factories. The client has to create different concrete factories explicitly.

Is that right?

What are the other differences?

Furthermore, what is the Factory Method pattern? Is it same as the Factory pattern?

like image 385
skydoor Avatar asked Jan 17 '10 03:01

skydoor


People also ask

Is Abstract Factory a factory of factories?

Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

What is Abstract Factory in Java?

Abstract Factory design pattern provides approach to code for interface rather than implementation. Abstract Factory pattern is “factory of factories” and can be easily extended to accommodate more products, for example we can add another sub-class Laptop and a factory LaptopFactory.

What is Abstract Factory used for?

The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes. This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles.

Why would a Factory Method design pattern be a more appropriate design than an Abstract Factory design pattern?

The idea behind the Factory Method pattern is that it allows for the case where a client doesn't know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job while Abstract Factory pattern is best utilized when your system has to create multiple families of ...


2 Answers

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 don't know about "Concrete Factory." I've heard the term, but never given it too much thought.

Factory Method

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 HtmlElement objects:

var newDiv = document.createElement('div'); 

This isn't a great example though, as an important part of the Factory Method is polymorphism. If I could extend document to define another class which defines another createElement this would be prime Factory Method material.

Abstract Factory

An abstract factory is meant to "provide an interface for creating families of related or dependent objects without specifying concrete classes.

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.

That class could have methods createWindow, createButton, createScrollBar 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.

Addendum - Concrete Factory

I believe that a Concrete Factory is any non-abstract implementation of Abstract Factory or Factory method.

So, when I write my own generalization of document which overrides createElement, the class I create is a Concrete Factory.

Likewise, while WidgetFactory would be an Abstract Factory, SwingWidgetFactory would be a concrete factory.

like image 119
Richard JP Le Guen Avatar answered Oct 04 '22 21:10

Richard JP Le Guen


The best way to learn is definately to read. Take a look at this: http://www.dofactory.com/Patterns/Patterns.aspx.

Factory method's defer creation of objects to sub classes. This means that a base class/interface is defined however client code doesn't create an object against this interface. Sub classes which implement the interface are left to initialize an object.

Abstract factories can be found here: http://www.dofactory.com/Patterns/PatternAbstract.aspx#_self2

There is already good information on that site. The next best thing is to check wiki:

http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29

http://javadesign-patterns.blogspot.com/ is a good place to learn design patterns

like image 24
JonH Avatar answered Oct 04 '22 22:10

JonH