What is factory method ???
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: What is factory method ???

  1. #1
    Join Date
    Dec 2011
    Posts
    10

    Talking What is factory method ???

    Hello. Today I have heard a new tearm in C++ programing,and I do not understand it very well, so I was hoping that someone can give me a example and short explanation of factory method.

    Links also are appreciate. Thank very much!!!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: What is factory method ???

    Normally, an object is created by it's type's constructor. However, there are times when this is not possible or not useful. A couple of examples might be:

    1) Creation of objects is restricted by making constructor private
    2) Not enough information at point of creation to know concrete type to create, only base class
    3) State is a required parameter at object creation which should not be visible to the creator (eg, unique ID).

    For whatever reason, an object cannot simply be created on the stack or the heap as usual. Therefore, in order to create it we use the factory pattern. This means we call a function or a method of a factory object that returns the constructed object.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: What is factory method ???

    Here's a link to a page that describes the factory pattern as well as some other patterns http://en.wikibooks.org/wiki/C++_Pro...esign_Patterns
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: What is factory method ???

    Quote Originally Posted by Lindley View Post
    Normally, an object is created by it's type's constructor. However, there are times when this is not possible or not useful. A couple of examples might be:

    1) Creation of objects is restricted by making constructor private
    2) Not enough information at point of creation to know concrete type to create, only base class
    3) State is a required parameter at object creation which should not be visible to the creator (eg, unique ID).

    For whatever reason, an object cannot simply be created on the stack or the heap as usual. Therefore, in order to create it we use the factory pattern. This means we call a function or a method of a factory object that returns the constructed object.
    That's the wrong way of looking at it. Creational design patterns are abstractions you introduce proactively to improve your design, not something you resort to when ad-hoc creation "as usual" doesn't work.

    There's a great deal of confusion as to what the "factory pattern" really means. It's not enougt to have a function/method that creates and returns objects as you claim. At least not if you're referring to the Factory Method pattern (as defined in Design Patterns by Gamma and others). This pattern specifically deals with the situation where a method returns base class objects and hides which derived class objects are actually created.
    Last edited by nuzzle; January 24th, 2012 at 02:46 AM.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center