Board index » cppbuilder » object factory implementation problem

object factory implementation problem


2005-06-03 11:22:19 AM
cppbuilder107
In the object factory document, it always say " the implementation of
product also must register this function with the
productFactory..." for example I have a product named "grape"
// class declairation in grape.h
class grape: public fruit{
private:
.....
public:
__fastcall grape();
};
also, I have a object factory named "productFactory" which is defined as
Singleton.
// class declairation
class productFactory
{
public:
typedef fruit* (*CreateFruitCallBack)();
bool RegisterFruit(int fruitId, CreateFruitCallBack cFruitFn);
bool UnregisterFruit(int fruitId);
fruit* CreateFruit(int fruitId);
private:
.....
protected:
......
}
Now how can i implement the sentence of "the implementation of product also
must register this function with the
productFactory..." ? How can i create concrete product and register it
within class "grape"???
does it means that I should declair a function in the <grape.h>like this:?
class grape: public fruit{
private:
.....
public:
__fastcall grape();
};
fruit* getGrape(void);
???
// in the graph.cpp file
....
fruit* getGrape()
{
return new grape;
}
.....
The problem is still. How can i put the following registration code::::
<productFactory::Instance().RegisterFruit(10,getGrape);>????
 
 

Re:object factory implementation problem

"xubo" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...
Quote
Now how can i implement the sentence of "the implementation of
product also must register this function with the productFactory..." ?
How can i create concrete product and register it within class "grape"???
does it means that I should declair a function in the <grape.h>like
this:?
Yes.
Quote
How can i put the following registration code::::

<productFactory::Instance().RegisterFruit(10,getGrape);>????
That is exactly what you do.
Gambit
 

Re:object factory implementation problem

Sorry. my last question is where to put registration code
<productFactory::Instance().RegisterFruit(10,getGrape);>???
the registration code can't put into function getGrape(void); If it is not
put in getGrape(), how can program execute this part of
code(productFactory::Instance().RegisterFruit(10,getGrape);)??
Quote
>How can i put the following registration code::::
>
><productFactory::Instance().RegisterFruit(10,getGrape);>????

That is exactly what you do.


Gambit


 

{smallsort}

Re:object factory implementation problem

"xubo" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...
Quote
Sorry. my last question is where to put registration code
Anywhere you want. Preferrably someplace where it will only be called once,
such as at program startup, like in your MainForm constructor, or in main()
or WinMain() directly, or in a static object that is created globally.
Gambit