版权声明:copyright@xxdk2017 https://blog.csdn.net/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: [email protected]
> 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: [email protected]
> 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: [email protected]
> 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: [email protected]
> 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