help me in implementing Factory design pattern

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diwakar09
    New Member
    • Sep 2007
    • 39

    help me in implementing Factory design pattern

    i am using vc++
    [code=cpp]
    #include<iostre am>
    using namespace std;
    class esteem;
    class zen;
    class maruti
    {
    char name[10];
    public:
    maruti(){}
    void getdata()
    {
    cout<<"get name\n";
    cin>>name;
    }
    maruti* getobject()
    {
    if (name == "esteem")
    return esteem.Instance ();
    else
    return new zen();
    }
    virtual void print() {}
    };
    class esteem:public maruti
    {
    static esteem* pinstance; // initialize pointer
    public:
    esteem(){}
    static esteem *Instance ()
    {
    if (pinstance == 0) // is it the first call?
    {
    pinstance = new esteem(); // create sole instance
    }
    return pinstance; // address of sole instance
    }
    void print()
    {
    cout<<"in esteem\n";
    }

    };
    class zen:public maruti
    {
    public:
    zen(){}
    void print()
    {
    cout<<"in zen\n";
    }

    };

    int main()
    {
    maruti *m,*k,j;
    m=&j;
    m->getdata();
    k = m->getobject();
    k->print();
    return 0;
    }



    [/code]

    this is the whole code
    i am getting errors
    error C2512: 'zen' : no appropriate default constructor available
    this is the main error
    so tried another way to create the object of esteem class
    but even this is showing erros
    please help me
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I am assuming maruti is your factory and esteem and zen are classes for which you want the factory to create an objects.

    First then, the classes you want objects of do not derive from the factory. That is, the object produced by the factory is not a kind of factory.

    Your factory maruti::getobje ct() needs to return either an esteem* or a zen* and it can't do both. And you can't derive esteem and zen from maruti.

    You have very many things not correct here.
    1) your static esteem::pInstan ce variable was never created
    2) you cannot define a static method inside the class declaration. You can do that only with member funcuons
    3) esteem and zen to not derive from maruti
    4) Your main() should look like:
    [code=cpp]
    int main()
    {
    maruti* theFactory = new maruti;
    esteem* obj = CreateEsteem(th eFactory);

    return 0;
    }
    [/code]

    That is, you create the factory and then pass the factorry to a create function to crate the required object:
    [code=cpp]
    esteem* CreateEsteem(ma ruti* arg)
    {
    return arg->getesteemobjec t();
    }
    [/code]

    The maruti::geteste emobject() returns an esteem*:
    [code=cpp]
    esteem* maruti::geteste emobject()
    {
    return esteem::Instanc e();

    }
    [/code]

    This is all based on the fact that esteem and zen are independent classes.

    If they are not, then they need to derive from a base class and the factory method creates a derived object and returns a base class pointer. This would be a parameterized factory:
    [code=cpp]
    quality* maruti::getobje ct(marutitype arg)
    {
    if (arg == ESTEEM)
    {
    return esteem::Instanc e();
    }
    if (arg == ZEN)
    {
    return new zen; }

    }
    [/code]

    where:

    class maruti
    {
    public:
    enum marutitype {ESTEEM, ZEN);
    etc...
    [/code]

    Comment

    Working...