• Post Reply Bookmark Topic Watch Topic
  • New Topic

Factory Method Pattern  RSS feed

 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have read about this design pattern on wiki but I don't understand this definition: "Define an interface for creating an object, but let the classes that implements the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses". In their example, I can't see the interface and the subclass.

 
Sresh Rangi
Ranch Hand
Posts: 54
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The examples were not added by the same people that added the definition. The example they give matches the definition of "Simple Factory" but the definition matches "Factory Method".

An example that matches the definition given would be more like:



A class can create ImageReaders by calling createImageReader on an ImageReaderFactory but the type that is created can be configured externally giving this class different types of ReaderFactory.
 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
However, that mean for each concrete class of ImageReader, we have to create 1 Factory class (GifReaderFactory, JpegReaderFactory ) that implements ReadFactory?
For me, this is a kind of redundancy. It seems that the example on wiki looks shorter and better, doesn't it?

Thank you.


Sresh Rangi wrote:The examples were not added by the same people that added the definition. The example they give matches the definition of "Simple Factory" but the definition matches "Factory Method".

An example that matches the definition given would be more like:



A class can create ImageReaders by calling createImageReader on an ImageReaderFactory but the type that is created can be configured externally giving this class different types of ReaderFactory.


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swerrgy Smith wrote:I have read about this design pattern on wiki but I don't understand this definition: "Define an interface for creating an object, but let the classes that implements the interface decide which class to instantiate.

Well, first off, I think you may be confusing two things:
  • The "Factory" pattern.
  • Factory methods.
  • They are NOT the same.

    The first is what I think you are thinking about, and the rest of the implementation has been well described by Sresh.

    A Factory method is a static method used as an alternative to a constructor. A very simple example is:
    Integer two = new Integer(2);
    and
    Integer two = Integer.valueOf(2);
    both of which do basically the same thing; except the second one does it much better .

    Good examples of the Factory pattern are the JDBC DriverManager and DataSource interfaces, which are "factories" that allow each database provider to provide their own Connections (java.sql.Connection) for your databasing pleasure. For more details, read the tutorials.

    HIH

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Swerrgy Smith wrote:I have read about this design pattern on wiki

    I must apologise.

    You were quite correct: the pattern is called the "Factory method pattern"; both in Wiki AND in 'The Bible'. I guess I've always just lopped off the "method" myself when thinking about the pattern to save confusion with Java "factory methods".

    Hopefully, the rest of the post contains some useful stuff for you though.

    Winston
     
    Swerrgy Smith
    Ranch Hand
    Posts: 94
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Swerrgy Smith wrote:I have read about this design pattern on wiki

    I must apologise.

    You were quite correct: the pattern is called the "Factory method pattern"; both in Wiki AND in 'The Bible'. I guess I've always just lopped off the "method" myself when thinking about the pattern to save confusion with Java "factory methods".

    Hopefully, the rest of the post contains some useful stuff for you though.

    Winston


    Thanks Winston, actually your previous post was very helpful!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!