I want to implement a serial communication class which can be used both on linux and windows platform. Is factory method suitable for this class? How to deal with header files on different platform?for example I want to compile the code on windows so I can't use the header files on linux, Should I use the pre-processor instead?
//pseudocode
class ComDevice
{
virtual void getBytes()=0;
};
class LinuxComDevice:public ComDevice
{
void getBytes();
};
class WindowsComDevice:public ComDevice
{
void getBytes();
};
class DeviceFactory
{
ComDevice createDevice()
{
if(platformIsW开发者_JAVA百科indows())
return new WindowsComDevice();
else return new LinuxComDevice();
};
};
You could use Factory. However Factory would be a little overkill if you are only going to have Windows/Linux. If you anticipate that you would be asked to support lot many platforms in the future then makes sense to do what you have done.
How to deal with header files on different platform?
Didn't quite understand this part. What do you want to compile?
精彩评论