#include <iostream>
#include <memory>
using namespace std;
// 组件
struct ComBase {
// 展开UI面板
virtual void show() = 0;
};
///////////////////////////////////////////////////////////////////////////
struct ComA : public ComBase {
virtual void show() { std::cout << "I'm ComA" << endl; }
// 特有事务
void do_somethingA() { std::cout << "Do A" << endl; }
};
struct ComB : public ComBase {
virtual void show() { std::cout << "I'm ComB" << endl; }
// 特有事务
void do_somethingB() { std::cout << "Do B" << endl; }
};
template <typename Type>
auto GetComPtr() -> std::shared_ptr<Type> {
return std::make_shared<Type>();
};
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
class MyTool {
using ComPtr = std::shared_ptr<ComBase>;
public:
MyTool(ComPtr com) : m_com(com) {}
void ShowWindow() { m_com->show(); }
private:
ComPtr m_com;
};
using MyToolPtr = std::shared_ptr<MyTool>;
///////////////////////////////////////////////////////////////////////////
int main()
{
auto comA = GetComPtr<ComA>();
auto tool = std::make_shared<MyTool>(comA);
tool->ShowWindow();
comA->do_somethingA();
return 0;
}