Results 1 to 4 of 4

Thread: Factory Pattern

  1. #1
    userj2ee is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default Factory Pattern

    Hi,

    I have some confusion about the use of Factory pattern.
    I understand the concept of Factory pattern. Following is my understanding.

    A simple factory returns an instance of any one of several possible classes that have a common parent class.
    The common parent class can be an abstract class, or an interface.
    The calling program typically has a way of telling the factory what it wants, and the factory makes the decision which subclass should be returned to the calling program. It then creates an instance of that subclass, and then returns it to the calling program.

    My question is as the factory class would create an instance of subclass but will always return it as a reference to super class, what will happen if sub class adds some more methods? How those methods can be called as the returned reference is of super class which doesnt have any knowledge of the methods added in sub class?

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by userj2ee View Post
    My question is as the factory class would create an instance of subclass but will always return it as a reference to super class, what will happen if sub class adds some more methods? How those methods can be called as the returned reference is of super class which doesnt have any knowledge of the methods added in sub class?
    A Factory can't decide what you want to use the product for; if it delivers the wrong type of object, the factory doesn't meet your needs. You either have to downcast the delivered product (a hack) or you have to create another method in your factory or redesign ...

    kind regards,

    Jos

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    You either have to downcast the delivered product (a hack) or you have to create another method in your factory or redesign ...
    A good example of this can be seen in the createXxxBorder methods of BorderFactory.

    db

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    A good example of this can be seen in the createXxxBorder methods of BorderFactory.

    db
    Yep, some borders are more 'special' than their Border interface so the factory has to return their class type instead of just a Border.

    kind regards,

    Jos

Similar Threads

  1. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-14-2010, 11:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •