C++封装SDK的另一种方法(多态和工厂方法)

本文介绍了一种使用多态工具和工厂设计模式封装SDK的方法,通过对外暴露接口头文件和动态链接库,隐藏了模块的具体实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/u011583798/article/details/79696242

 

 

1. 假设一个module 名为ModuleImplement

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

3. 多态工具+工厂设计模式

├── client_test.cpp
├── module
│   ├── libmodule.so
│   ├── ModuleImplement.cpp
│   ├── ModuleImplement.hpp
│   └── ModuleInterface.hpp

├── ModuleInterface.hpp

 

其中ModuleImplement是模块的实现文件,ModuleInterface是模块的接口文件,

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

1. ModuleInterface.hpp

/*************************************************************************
    > File Name: ModuleInterface.hpp
    > Author: XXDK
    > Email: v.manstein@qq.com
    > Created Time: Mon 26 Mar 2018 09:11:48 AM CST
 ************************************************************************/
 
#ifndef _MODULEINTERFACE_HPP
#define _MODULEINTERFACE_HPP
 
#include<memory>
 
class ModuleInterface{
public:
    virtual ~ModuleInterface(){}
    virtual void init() = 0;
    virtual void run() = 0;
    virtual void stop() = 0;
 
    static std::shared_ptr<ModuleInterface> Create(std::string& m_name); // static factory method.
};
 
#endif //  _MODULEINTERFACE_HPP

 

2. ModuleImplement.hpp

/*************************************************************************
    > File Name: ModuleImplement.hpp
    > Author: XXDK
    > Email: v.manstein@qq.com
    > Created Time: Mon 26 Mar 2018 09:14:35 AM CST
 ************************************************************************/
 
#ifndef _MODULEIMPLEMENT_HPP
#define _MODULEIMPLEMENT_HPP
 
#include"ModuleInterface.hpp"
#include<iostream>
#include<string>
 
class ModuleImplement: public ModuleInterface  
{
public:
    ModuleImplement(std::string& m_name);
    ~ModuleImplement();
    virtual void init() override;
    virtual void run()  override;
    virtual void stop() override;
 
private:
    std::string m_module_name;
};
 
#endif // _MODULEIMPLEMENT_HPP

 

2. ModuleImplement.cpp

/*************************************************************************
    > File Name: ModuleImplement.cpp
    > Author: XXDK
    > Email: v.manstein@qq.com
    > Created Time: Mon 26 Mar 2018 09:19:13 AM CST
 ************************************************************************/
 
#include<iostream>
#include"ModuleImplement.hpp"
 
using namespace std;
 
std::shared_ptr<ModuleInterface> ModuleInterface::Create(std::string& m_name)
{
    return std::shared_ptr<ModuleInterface>(new ModuleImplement(m_name));
}
 
ModuleImplement::ModuleImplement(std::string& m_name)
    :m_module_name(m_name)
{
    ;
}
 
ModuleImplement::~ModuleImplement()
{
    ;
}
 
void ModuleImplement::init()
{
    std::cout <<"1. "<< __func__ << ": module init" << std::endl;    
}
 
void ModuleImplement::run()
{
    std::cout <<"2. "<< __func__ << ":  module run->"<< m_module_name << std::endl;    
}
 
void ModuleImplement::stop()
{
    std::cout <<"3. "<<  __func__ << ": module stop" << std::endl;    
}

 

将模块实现文件ModuleImplement.cpp打包为动态库:

g++ -fpic -shared libmodule.so ModuleImplement.cpp -std=c++11

4. 用户测试用例 client_test.cpp

/*************************************************************************
    > File Name: test.cpp
    > Author: XXDK
    > Email: v.manstein@qq.com
    > Created Time: Mon 26 Mar 2018 10:10:11 AM CST
 ************************************************************************/
 
#include<iostream>
#include<string>
 
#include"ModuleInterface.hpp"
 
using namespace std;
 
int main()
{
    std::string x("xxdk")    ;
    std::shared_ptr<ModuleInterface> pmd = ModuleInterface::Create(x);
    pmd->init();
    pmd->run();
    pmd->stop();
    
    return 0;
}

5. 测试: g++ client_test.cpp -L./module -lmodule -o test -Wl,-rpath,./module -std=c++11

使用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、付费专栏及课程。

余额充值