-
January 23rd, 2012, 02:16 PM
#1
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!!!
-
January 23rd, 2012, 03:29 PM
#2
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.
-
January 23rd, 2012, 03:53 PM
#3
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
-
January 24th, 2012, 03:34 AM
#4
Re: What is factory method ???
Originally Posted by Lindley
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 03: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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|