E
Ed Fair
Hi,
I'm a long-time C programmer, I've started making the transition to "real
C++" recently. I am puzzled about how to write a factory method. Any
suggestions, pointers or references to documentation would be appreciated.
Here is some background information about what I'm doing: I have written a
large body of code that makes extensive use of mutexes. Most of my code is
portable, except for the mutexes, which are platform specific. I've
abstracted the notion of mutex with an abstract class "mutex", with pure
virtual functions get() and release() to obtain and release the mutex,
respectively. The large body of code uses pointers to these mutexes.
I am deriving "implementation classes" from mutex, for example win32Mutex,
linuxMutex, and posixMutex, on a per-platform basis and implementing get()
and release() accordingly.
Everything seems to be working OK with my simple tests, but I want to use a
"factory method" to cleanly produce (pointers to) mutexes. I can't grok how
to do this at a high-level, and I can't quite "seal off" the
platform-specific code from the large body of platform-independent code.
The idea is that I want to minimize platform-specific things in the large
body of code by calling a factory method when a mutex is needed, as opposed
to calling the (platform specific) constructors in the implementation
classes directly.
Should I:
1) Try to make the factory method a static member function of class mutex?
2) Create another class mutexMaker with member function makeNewMutex(), and
derive classes like win32MutexMaker and linuxMutexMaker from it?
3) Write a non-member function?
4) Use #IFDEF and #INCLUDE and #3 above?
5) Something else that I haven't thought of?
Thanks in advance for any insight.
ed
I'm a long-time C programmer, I've started making the transition to "real
C++" recently. I am puzzled about how to write a factory method. Any
suggestions, pointers or references to documentation would be appreciated.
Here is some background information about what I'm doing: I have written a
large body of code that makes extensive use of mutexes. Most of my code is
portable, except for the mutexes, which are platform specific. I've
abstracted the notion of mutex with an abstract class "mutex", with pure
virtual functions get() and release() to obtain and release the mutex,
respectively. The large body of code uses pointers to these mutexes.
I am deriving "implementation classes" from mutex, for example win32Mutex,
linuxMutex, and posixMutex, on a per-platform basis and implementing get()
and release() accordingly.
Everything seems to be working OK with my simple tests, but I want to use a
"factory method" to cleanly produce (pointers to) mutexes. I can't grok how
to do this at a high-level, and I can't quite "seal off" the
platform-specific code from the large body of platform-independent code.
The idea is that I want to minimize platform-specific things in the large
body of code by calling a factory method when a mutex is needed, as opposed
to calling the (platform specific) constructors in the implementation
classes directly.
Should I:
1) Try to make the factory method a static member function of class mutex?
2) Create another class mutexMaker with member function makeNewMutex(), and
derive classes like win32MutexMaker and linuxMutexMaker from it?
3) Write a non-member function?
4) Use #IFDEF and #INCLUDE and #3 above?
5) Something else that I haven't thought of?
Thanks in advance for any insight.
ed