开发者

What design pattern to use in serial communication on different platform?

开发者 https://www.devze.com 2023-02-15 06:09 出处:网络
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?fo

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消