C++封装SDK的一种方法(接口与实现分离)

https://blog.csdn.net/u011583798/article/details/79615756

 

1. 假设一个module包含三个sub module

    SubModuleA,SubModuleB,SubModuleC

2. 现在要将该模块暴露sdk给客户,我们不想直接将模块实现的头文件暴露出去

3. 可以考虑接口与实现分离的设计模式

    ├── module
    │   ├── module_implement.cpp
    │   ├── module_implement.hpp
    │   ├── module_interface.cpp
    │   ├── module_interface.hpp
    │   ├── sub_modules.cpp
    │   └── sub_modules.hpp

    └── test_client.cpp

其中module_implement和sub_modules是模块的实现文件,module_interface是模块的接口文件,

最终我们的目的是将module_interface.hpp 和libmodule.so 给客户,而不会泄露任何模块实现信息

sub_modules.hpp

    /*************************************************************************
        > File Name: sub_modules.hpp
        > Author: XXDK
        > Email: v.manstein@qq.com
        > Created Time: Mon 19 Mar 2018 11:40:40 AM CST
     ************************************************************************/
     
    #ifndef _SUB_MODULES_HPP
    #define _SUB_MODULES_HPP
     
    class SubModuleA
    {
    public:
        SubModuleA();
        virtual ~SubModuleA();
        void init();
        void run();
        void stop();
    };
     
    class SubModuleB
    {
    public:
        SubModuleB();
        virtual ~SubModuleB();
        void init();
        void run();
        void stop();
    };
     
    class SubModuleC
    {
    public:
        SubModuleC();
        virtual ~SubModuleC();
        void init();
        void run();
        void stop();
    };
    #endif // _SUB_MODULES_HPP

sub_modules.cpp

    /*************************************************************************
        > File Name: sub_modules.cpp
        > Author: XXDK
        > Email: v.manstein@qq.com
        > Created Time: Mon 19 Mar 2018 11:42:59 AM CST
     ************************************************************************/
     
    #include<iostream>
    #include"sub_modules.hpp"
     
    using namespace std;
     
    SubModuleA::SubModuleA()
    {
        ;    
    }
    SubModuleB::SubModuleB()
    {
        ;    
    }
    SubModuleC::SubModuleC()
    {
        ;    
    }
     
    SubModuleA::~SubModuleA()
    {
        ;
    }
     
    SubModuleB::~SubModuleB()
    {
        ;
    }
    SubModuleC::~SubModuleC()
    {
        ;
    }
    void SubModuleA::init()
    {
        std::cout<< "    -sub module A init!"<< std::endl;
    }
    void SubModuleB::init()
    {
        std::cout<< "    -sub module B init!"<< std::endl;
    }
    void SubModuleC::init()
    {
        std::cout<< "    -sub module C init!"<< std::endl;
    }
     
    void SubModuleA::run()
    {
        std::cout<< "    -sub module A run!"<< std::endl;
    }
    void SubModuleB::run()
    {
        std::cout<< "    -sub module B run!"<< std::endl;
    }
    void SubModuleC::run()
    {
        std::cout<< "    -sub module C run!"<< std::endl;
    }
    void SubModuleA::stop()
    {
        std::cout<< "    -sub module A stop!"<< std::endl;
    }
    void SubModuleB::stop()
    {
        std::cout<< "    -sub module B stop!"<< std::endl;
    }
    void SubModuleC::stop()
    {
        std::cout<< "    -sub module C stop!"<< std::endl;
    }

module_implement.hpp

    /*************************************************************************
        > File Name: module_implement.hpp
        > Author: XXDK
        > Email: v.manstein@qq.com
        > Created Time: Mon 19 Mar 2018 11:58:23 AM CST
     ************************************************************************/
     
    #ifndef _MODULEIMPLEMENT_HPP
    #define _MODULEIMPLEMENT_HPP
     
    #include"sub_modules.hpp"
     
    class ModuleImplement
    {
    public:
        ModuleImplement();
        ~ModuleImplement();
        void init();
        void run();
        void stop();
    private:
        SubModuleA* m_pSubModuleA;
        SubModuleB* m_pSubModuleB;
        SubModuleC* m_pSubModuleC;
        void allSubMoudleInit();
        void allSubMoudleRun();
        void allSubMoudleStop();
    };
     
    #endif // _MODULEIMPLEMENT_HPP

module_implement.cpp

    /*************************************************************************
        > File Name: module_implement.cpp
        > Author: XXDK
        > Email: v.manstein@qq.com
        > Created Time: Mon 19 Mar 2018 06:35:23 PM CST
     ************************************************************************/
     
    #include<iostream>
    #include"module_implement.hpp"
     
    using namespace std;
    ModuleImplement::ModuleImplement()
    {
        m_pSubModuleA = new SubModuleA;
        m_pSubModuleB = new SubModuleB;
        m_pSubModuleC = new SubModuleC;
    }
     
    ModuleImplement::~ModuleImplement()
    {
        if(m_pSubModuleA)
            delete m_pSubModuleA;
        if(m_pSubModuleB)
            delete m_pSubModuleB;
        if(m_pSubModuleC)
            delete m_pSubModuleC;
    }
     
    void ModuleImplement::init()
    {
        std::cout << "+module init!" << std::endl;
        allSubMoudleInit();
    }
    void ModuleImplement::allSubMoudleInit()
    {
        m_pSubModuleA->init();
        m_pSubModuleB->init();
        m_pSubModuleC->init();
    }
    void ModuleImplement::run()
    {
        std::cout << "+module run!" << std::endl;
        allSubMoudleRun();
    }
    void ModuleImplement::allSubMoudleRun()
    {
        m_pSubModuleA->run();
        m_pSubModuleB->run();
        m_pSubModuleC->run();
    }
    void ModuleImplement::stop()
    {
        std::cout << "+module stop!" << std::endl;
        allSubMoudleStop();
    }
     
    void ModuleImplement::allSubMoudleStop()
    {
        m_pSubModuleA->stop();
        m_pSubModuleB->stop();
        m_pSubModuleC->stop();
    }

moudle_interface.hpp

    /*************************************************************************
        > File Name: module_interface.hpp
        > Author: XXDK
        > Email: v.manstein@qq.com
        > Created Time: Mon 19 Mar 2018 11:50:49 AM CST
     ************************************************************************/
     
    #ifndef _MODULE_INTERFACE_HPP
    #define _MODULE_INTERFACE_HPP
     
    class ModuleImplement;
     
    class ModuleInterface
    {
    public:
        ModuleInterface();
        virtual ~ModuleInterface();
        void init();
        void run();
        void stop();
     
    private:
        ModuleImplement* m_pImpl;
    };
     
    #endif // _MODULE_INTERFACE_HPP

moudle_interface.cpp

    /*************************************************************************
        > File Name: moudule_interface.cpp
        > Author: XXDK
        > Email: v.manstein@qq.com
        > Created Time: Mon 19 Mar 2018 11:53:35 AM CST
     ************************************************************************/
     
    #include<iostream>
    #include"module_interface.hpp"
    #include"module_implement.hpp"
     
    using namespace std;
     
    ModuleInterface::ModuleInterface()
    {
        m_pImpl = new ModuleImplement;
    }
    ModuleInterface::~ModuleInterface()
    {
        if(m_pImpl)
            delete m_pImpl;
    }
     
    void ModuleInterface::init()
    {
        m_pImpl->init();
    }
     
    void ModuleInterface::run()
    {
        m_pImpl->run();
    }
     
    void ModuleInterface::stop()
    {
        m_pImpl->stop();
    }

4. 在module目录下编译共享库:

g++ -shared -fpic -o libmodule.so module_implement.cpp module_interface.cpp sub_modules.cpp

5. 编写测试用例test_client.cpp:

    /*************************************************************************
        > File Name: test_client.cpp
        > Author: XXDK
        > Email: v.manstein@qq.com
        > Created Time: Mon 19 Mar 2018 01:53:31 PM CST
     ************************************************************************/
     
    #include<iostream>
    #include"./module/module_interface.hpp"
    using namespace std;
     
    int main()
    {
        ModuleInterface module;
        module.init();
        module.run();
        module.stop();
     
        return 0;
    }

6. 编译连接test_client.cpp

g++ test_client.cpp -L./module -lmodule -o test -Wl,-rpath,./module

7. enjoy:

+module init!
-sub module A init!
-sub module B init!
-sub module C init!
+module run!
-sub module A run!
-sub module B run!
-sub module C run!
+module stop!
-sub module A stop!
-sub module B stop!
-sub module C stop!

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用C++代码封装的win32操作类, 与MFC相似,对于学习SDKC++是巨好的参考 Tutorials Menu of tutorials Tutorial 1: The Simplest Window Tutorial 2: Using Classes and Inheritance Tutorial 3: Using Messages to Create a Scribble Window Tutorial 4: Repainting the Window Tutorial 5: Wrapping a Frame around our Scribble Window Tutorial 6: Customising Window Creation Tutorial 7: Customising the Toolbar Tutorial 8: Loading and Saving Files Tutorial 9: Printing Tutorial 10: Finishing Touches Tutorial 1: The Simplest Window The following code uses Win32++ to create a window. This is all the code you need (in combination with Win32++) to create and display a simple window. Note that in order to add the Win32++ code to our program, we use an #include statement as shown below. #include "../Win32++/Wincore.h" INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { //Start Win32++ CWinApp MyApp; //Create a CWnd object CWnd MyWindow; //Create (and display) the window MyWindow.Create(); //Run the application return MyApp.Run(); } This program has four key steps: Start Win32++. We do this here by creating a CWinApp object called MyApp. Create a CWnd object called MyWindow. Create a default window by calling the Create function. Start the message loop, by calling the Run function. If you compile and run this program, you'll find that the application doesn't end when the window is closed. This is behaviour is normal. An illustration of how to use messages to control the windows behaviour (including closing the application) will be left until tutorial 3.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值