Thread: Need comment on Factory Pattern Example

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Need comment on Factory Pattern Example

    Hi All,

    I am studying design patterns and create a program for "Factory Pattern ".

    I have read various tutorials and create my own with the help of it.

    I just need your comments that whether this implementation is correct or it can be made

    accurate according to Factory pattern.

    Code:
    #include<iostream>
    using namespace std;
    
    class Pizza
    {
    public:
        virtual void displayPrice() = 0;   
    };
    
    class MacDonald : public Pizza
    {
    public:
        int m_price;
    
        MacDonald() {}
    
        MacDonald( int price )
        {
            m_price = price;
        }
    
        void displayPrice()
        { 
            cout<<"The Rate of MacDonald Pizza is "<<120;
        } 
    };
    
    class HotMillion : public Pizza
    {
    public:
        int m_price;
    
        HotMillion(){}
    
        HotMillion(int price )
        {
           m_price = price;     
        }
    
        void displayPrice()
        { 
            cout<<"The Rate of HotMillion Pizza is "<<200;
        } 
    };
    
    class Factory
    {
    public:
    
    int m_index;
        Factory(int index)
        {
            m_index = index;
        } 
    
         Pizza* show()
         {    
            switch( m_index )
            {
                case 1:
                new  MacDonald;
                break; 
                
                case 2:
                new  HotMillion;
                break;
            } 
        }
    };
    
    
    int main()
    {
        Factory a( 1 ); 
        Pizza *p = a.show();
        p->displayPrice();
    }
    Its running fine and values are hard coded for time being.

    Please give your valuable suggestion.

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
         Pizza* show()
         {    
            switch( m_index )
            {
                case 1:
                new  MacDonald;
                break; 
                
                case 2:
                new  HotMillion;
                break;
            } 
        }
    Using a switch pattern such as this is usually a poor Factory (I think at least).
    A better approach might be a virtual function.

    Typically, you may derive your objects from the factories and overload a New function that is virtual and returns an object of itself.
    The Factory can then call this virtual function New() and return a correct object.
    With this, you wouldn't need a switch at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A factory, generically, is any object, function, or class that can create another object -- or retrieve/find an object (picture the factory as a special warehouse) and return a reference/pointer to it. If a factory creates an object, it is also usually a good idea to have a defined manner to destroy the object when finished with it.

    Ideally, a factory would not need to be modified to support new object types (i.e. you would not need to hardcode some function to create each types, and modify that function when you introduce a new type). It is also often a good idea to prevent objects from being created by arbitrary code. There are several techniques available to achieve such things.

    I would have the factory class distinct from the objects it creates/finds/etc. Cars do not manufacture themselves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about the Factory pattern.
    By h3ro in forum C++ Programming
    Replies: 14
    Last Post: 11-27-2008, 04:55 PM
  2. Comment using // or /* &&& */
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 03-16-2005, 02:45 PM
  3. Abstract Factory pattern
    By Just in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2005, 10:58 AM
  4. Tab->Space converter and comment colorizer
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-30-2005, 05:46 PM
  5. text pattern recognition
    By mtsmox in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2002, 08:38 AM