• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Factory Vs Abstract Factory Pattern

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple factory is also a pattern to me. It hides the creation of the object. Does Ilja agree?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple factory example from HFDP:

 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic