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!