不同平台上的串行通信应使用什么设计模式?
我想实现一个可以在linux和windows平台上使用的串行通信类。工厂方法适合这个类吗?如何处理不同平台上的头文件?例如我想在windows上编译代码,所以不能使用linux上的头文件,我应该使用预处理器吗?
//pseudocode
class ComDevice
{
virtual void getBytes()=0;
};
class LinuxComDevice:public ComDevice
{
void getBytes();
};
class WindowsComDevice:public ComDevice
{
void getBytes();
};
class DeviceFactory
{
ComDevice createDevice()
{
if(platformIsWindows())
return new WindowsComDevice();
else return new LinuxComDevice();
};
};
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(platformIsWindows())
return new WindowsComDevice();
else return new LinuxComDevice();
};
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以使用工厂。然而,如果您只需要 Windows/Linux,那么 Factory 就有点大材小用了。如果您预计将来会被要求支持很多平台,那么您所做的事情就有意义了。
这部分不太明白。你想编译什么?
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.
Didn't quite understand this part. What do you want to compile?