Factory design pattern

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 4
Reputation: kevinmscs is an unknown quantity at this point 
Solved Threads: 0
kevinmscs kevinmscs is offline Offline
Newbie Poster

Factory design pattern

 
0
  #1
Apr 2nd, 2007
I have this class:

  1. class Pizza{
  2. protected:
  3. string desc;
  4. public:
  5. Pizza(){
  6. desc = "unknown pizza";
  7. }
  8. virtual string getdesc(){
  9. return desc;
  10. }
  11. virtual double cost(){
  12. return 0;
  13. }
  14. };

And one of its subclass:

  1. class Small: public Pizza{
  2. public:
  3. Small(){
  4. desc = "Small";
  5. }
  6. double cost(){
  7. return 8.00;
  8. }
  9. };

Besides the "Small" class there are others that are similar, eg. "Medium", "Large", etc.

I want to implement a Factory class for this. However, it doesnt seem to work:
  1. class PizzaFactory: public Pizza{
  2. public:
  3. Pizza* getPizzaInstance( int id );
  4. };
  5.  
  6. Pizza* PizzaFactory::getPizzaInstance( int id ) {
  7. switch (id) {
  8. case 1:
  9. return new Small();
  10. case 2:
  11. return new Medium();
  12. default:
  13. return NULL;
  14. }
  15. }

I wonder why when i called in main, it does not give me error but the "id" parameter does not get included into the switch statement.

Any help would be much appreciated.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Factory design pattern

 
0
  #2
Apr 3rd, 2007
There is nothing wrong with your code except "class PizzaFactory: public Pizza{". You don't need to derive factory class from base class. Although this would not cause any fatal problems.
Post your main and the some more description of the problem.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: kevinmscs is an unknown quantity at this point 
Solved Threads: 0
kevinmscs kevinmscs is offline Offline
Newbie Poster

Re: Factory design pattern

 
0
  #3
Apr 3rd, 2007
Originally Posted by thekashyap View Post
There is nothing wrong with your code except "class PizzaFactory: public Pizza{". You don't need to derive factory class from base class. Although this would not cause any fatal problems.
Post your main and the some more description of the problem.

The reason i derive factory class from base class is because i want to call the member function of class Pizza directly.

The idea of this is simple. I have class Pizza, its derived classes such as Small, Medium, Large. Now that i want to have a Factory class so that by calling this Factory Class a certain derived class (Small, for instance) will be created, instead of creating new instance by calling directly Small.

Main is as follow:

  1. int main(){
  2.  
  3. PizzaFactory *p2 = new PizzaFactory;
  4. p2->getPizzaInstance(1);
  5.  
  6. cout<<p2->getdesc()<<" $"<<p2->cost()<<endl;
  7. return 0;
  8. }

It does not give any error but the pizza created is a pizza of type Pizza (generic) and not that of Small, which should have been the one since i call getPizzaINstance(1).

What am i missing?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: kevinmscs is an unknown quantity at this point 
Solved Threads: 0
kevinmscs kevinmscs is offline Offline
Newbie Poster

Re: Factory design pattern

 
0
  #4
Apr 3rd, 2007
I think i got this one. I'll see if anything else coming up.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Factory design pattern

 
0
  #5
Apr 4th, 2007
So the problem is with main() and not those classes.. Again you shouldn't derive factory from the base.. it's against the pattern..
Change the main() to:
  1. int main(){
  2. PizzaFactory *p2 = new PizzaFactory;
  3. Pizza* p = p2->getPizzaInstance(1);
  4.  
  5. cout<<p->getdesc()<<" $"<<p->cost()<<endl;
  6. return 0;
  7. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC