943,541 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3565
  • C++ RSS
Apr 2nd, 2007
0

Factory design pattern

Expand Post »
I have this class:

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kevinmscs is offline Offline
4 posts
since Apr 2007
Apr 3rd, 2007
0

Re: Factory design pattern

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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Apr 3rd, 2007
0

Re: Factory design pattern

Click to Expand / Collapse  Quote originally posted by thekashyap ...
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:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kevinmscs is offline Offline
4 posts
since Apr 2007
Apr 3rd, 2007
0

Re: Factory design pattern

I think i got this one. I'll see if anything else coming up.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kevinmscs is offline Offline
4 posts
since Apr 2007
Apr 4th, 2007
0

Re: Factory design pattern

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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: 80-bit long doubles
Next Thread in C++ Forum Timeline: array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC