wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Factory Vs Abstract Factory Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Factory Vs Abstract Factory Pattern" Watch "Factory Vs Abstract Factory Pattern" New topic
Author

Factory Vs Abstract Factory Pattern

Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Hi

What is the basic difference between Factory Pattern and abstract factory pattern.
I have read these pattern on a book but could not understand core diffeence between them

Pls explain, or post a url
Also, When do we use which pattern.
[ September 15, 2005: Message edited by: Michael Ernest ]

~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Lots of people use "factories" to decouple the creation of an object from their code. (Similar to a delegate.) An abstract factory, strictly speaking, is used to create families of objects. A factory method is used to defer creation of an object to a subclass.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What book did you see it in?

The Factory pattern isn't a GoF pattern, and in fact use of the name doesn't seem to be consistent.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
There is Abstract Factory and Factory Method patterns in my GoF book. Then HFDP speaks about simple factory, but points out that it is not a pattern.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Yeah, sorry -- "Factory" isn't a pattern strictly. I was trying to say that by noting that people treat their "factory" like a delegate, but call it a factory merely because it creates something. I guess I should have done a step further and mentioned that it isn't a pattern.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Vladas Razas:
Then HFDP speaks about simple factory, but points out that it is not a pattern.


Why isn't it a pattern?
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
Why isn't it a pattern?


I am not sure. I got that impression from HFDP. As I understood what they said is: Factory Method, Abstract Factory and simple Factory. And simple Factory is not a pattern. Maybe because it's not in GoF?

I think Factory Method is a case of Template Method. Implementation seems very much alike. Only intention differs.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Vladas Razas:
I am not sure. I got that impression from HFDP. As I understood what they said is: Factory Method, Abstract Factory and simple Factory. And simple
Factory is not a pattern. Maybe because it's not in GoF?


There are a lot of design patterns out there that are not in the GoF book - GoF was only the start...


I think Factory Method is a case of Template Method. Implementation seems very much alike. Only intention differs.


I'd say Factory Method is a special case of Template Method. Abstract Factory is a special case of Strategy.
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
I'd say Factory Method is a special case of Template Method. Abstract Factory is a special case of Strategy.


In my opinion Facade is alike to Adapter. Is it pattern at all?

Can we scrap Singleton? Its almost anti-pattern now. At least in it's simpliest form = coupling, problems with inheritance.

So how many truly unique patterns left?

P.S. Builder is a strange one, rare and complex. It's an exercise to come up with example of use.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

The primary motivation behind design patterns -- or at least documenting them, as GoF does -- was to promote reuse of effective and well-tested relationships among independent types. Not surprisingly, a lot of those relationships look the same in simple diagrams, so expressing Intent is a key part of the catalog.

I have seen on some odd places a 'Factory pattern' described as the degenerate case of a Factory Method. Rather than deferring construction to a subclass, in this case the class produces objects without the direct use of a constructor. An example I'm digging from a dusty memory:

produced a button for a GUI that had its listener set up along with its label (duh). The method did nothing more than get around a lot of tedious set-up for a button-rich graphic environment.

Not really a pattern; just a convenience. I suppose it had a resemblance, however faint, to a "pattern," which for quite some time was a magic word to for getting a Pavlovian response from a developer. Probably it was someone's idea of borrowing the currency of that word to draw some attention.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Aniruddha Mukhopadhyay
Ranch Hand

Joined: Nov 15, 2000
Posts: 59
Factory Pattern:
----------------

A Factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it.
Usually all of the classes it returns have a common parent class and common
methods, but each of them performs a task differently and is optimized for
different kinds of data.

There are several similar variations on the factory pattern:

1. The base class is abstract and the pattern must return a complete working
class.
2. The base class contains default methods and is only subclassed for cases
where the default methods are insufficient.
3. Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share the same method names but may do something quite different.

Abstract Factory Pattern:
-------------------------

The Abstract Factory pattern is one level of abstraction higher than
the factory pattern. You can use this pattern when you want to return one of
several related classes of objects, each of which can return several different objects on request. In other words, the Abstract Factory is a factory object that returns one of several factories.

James Cooper's book is a good one on Design Patterns with examples in Java:

http://www.patterndepot.com/put/8/JavaPatterns.htm


Aniruddha
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Simple factory is also a pattern to me. It hides the creation of the object. Does Ilja agree?


Groovy
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Pradip Bhat:
Simple factory is also a pattern to me. It hides the creation of the object. Does Ilja agree?


As I don't know an authoritative description of what a "simple" Factory is, I have some trouble deciding wether it's a pattern...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Vladas Razas:
In my opinion Facade is alike to Adapter. Is it pattern at all?


As far as I remember, Facade is quite different from Adapter, not only in intent, but even in structure: a Facade typically hides the interactions of a number of objects, whereas Adapter changes the interface of a single object.


Can we scrap Singleton? Its almost anti-pattern now. At least in it's simpliest form = coupling, problems with inheritance.


I'd rather say that Singleton is a pattern that is only rarely appropriate to apply.


P.S. Builder is a strange one, rare and complex. It's an exercise to come up with example of use.


Strange. I use it all the time...
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
The Abstract Factory Pattern and Factory Method pattern are the same thing only different The names are not from Java so it's a bit misleading if you try to read too much in to the names.

Factory method returns an object based on a token:

RockFactory // Factory method
{
Rock getRock( Rock.Type type );
}

Abstract factory is a set of factory methdos. You use it when you have a family of objects that you want to fetch from a single source. You create a factory based on a type and all of the individual objects are created based on the "container" type.

LandscapeFactory // Abstract Factory
{
LandscapeFactory( LandscapeType type);

Rock getRock();
Bird getBird();
House getHouse();
}
[ September 15, 2005: Message edited by: Rick O'Shay ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Rick O'Shay:
Factory method returns an object based on a token:

RockFactory // Factory method
{
Rock getRock( Rock.Type type );
}


No, sorry, that's not Factory Method. Factory Method really is a special case of Template Method: an abstract base class defines an abstract method that is supposed to create objects, subclasses implement that factory method differently.

The main difference between Factory Method and Abstract Factory is that the former uses inheritance, whereas the latter uses composition.
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
Simple factory example from HFDP:

Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385

RockFactory // Factory method
{
Rock getRock( Rock.Type type );
}


This is closest to simple factory.


LandscapeFactory // Abstract Factory
{
LandscapeFactory( LandscapeType type);

Rock getRock();
Bird getBird();
House getHouse();
}


This implementation is mix of Simple and Abstract factory. Except for it is not abstract I would highly recommend Head First Design Patterns book.

Best regards!
Shivdas Nair
Greenhorn

Joined: Sep 19, 2005
Posts: 1
Again , I would start by recommending the Head First Design Patterns book...I have just started reading this book and I think it gives a pretty good idea of which these two patterns.

Both patterns encapsulate object creation logic.The benefit of encapsulating object creation using the factory method is that the client programs are de-coupled from the product creation logic.Especially when the product is available in different flavours(e.g. cheese Pizza, Veggie Pizza , etc..) and these flavours can be further customized to "specific" tastes (Texan , Californian, Indian , Chinese etc. :-), you will see that the Factory method pattern gives you a neat way hide the complexity of these combinations from the client program.Also, besides encapsulating the object creation logic, the Factory method pattern also allows you to define a framework for how the object is eventually to be used and delivered to the client.Thus you will only encasulate the logic of putting together the ingredients of the pizza, while the baking , cutting and packing process will remain same across franchise's across the world...If you refer to the HFDP book , you will get a very clear idea of how this is achieved using the Factory method pattern.


Abstract Factory on the other hand allows you even more flexibity in terms of creating these different pizza's customized to local "tastes"...what if the Pizza chain wants to open a new outlet in Turkey and while allowing the pizza to be tailored to the Turkish taste , still it must enforce that the the ingredients used in Turkish Pizza should use one of the existing interfaces defined for the Dough , Sauce etc.Or for example in India, where you get Paneer Mushroom Pizza and Panner is not a known ingredient anywhere else in the world (right ? ), how will you allow your program to be flexible enough to accomodate these variations ...This mix and match is made possible by using the Abstract Factory pattern..

In Factory method pattern , you have just one factory , which deals with the product creation....In Abstract factory pattern, you have the product creation factory and one more factory to create the ingredients of the product.I remember this better as "Singly abstracted" Factory Pattern and "Doubly Abstracted" Factory Pattern :-)

I hope this was helpful....But surely , do read the chapter on Factory pattern in the HFDP book.This is explained in a much better and clever way there.

Thanks
Shiv
Robert Watkins
Greenhorn

Joined: Sep 20, 2005
Posts: 25
Rick O'Shay wrote:

Factory method returns an object based on a token:




This is not the GoF style Factory Method, which is a special case of the Template Method pattern. This is more commonly known as a Factory Object - that is, an object whose main responsibility is to instantiate other ojbects (usually related objects).

Usually, when I write these, I start with the Static Factory - a static method on a class that creates instances of the class (or subclasses). When I get some related objects, or I need to have some state in the factory, then it's time to extract the Factory Object.

Abstract Factory is an evolutionary step-up from Factory Object - it's what evolves when you make two similar Factory Objects.


Software is too expensive to build cheaply...
Anirudh Bhatnagr
Greenhorn

Joined: Sep 01, 2010
Posts: 12
abstract factory pattern can use factory method to actulay implement different types of object creation, but it is not necessary.
I would say Abstract Factory Pattern is more into design product creations and how different variants of the product would be created , it is to supply a "kit" to create product and its different variants. It may use prototype/factory method and other builder pattern inside when actualy instantiating the object.
So In Abstract Factory Pattern ,we would provide an interface of product which can be used to create its families without specifying their concrete implementations.

example :

Factory Method :



For Abstract Factory Pattern :




Hope this clarifies, senior ranch members please validate/correct this.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Factory Vs Abstract Factory Pattern