惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - Clingingboy

冒个泡,刷刷存在感 使用文件映射和信号量来进程间通信 Xperf Basics: Recording a Trace (the easy way)(转) Xperf Basics: Recording a Trace(转) Xperf Analysis Basics(转) Android相关sdk使用 Uniscribe文字自动换行 Chrome RenderText分析(2) c++智能指针 codepage IMLangCodePages GUI 快捷键的实现思路 Menu实现逻辑 控件保持多种绘图状态的做法 2个函数宏技巧 绘图 Painter转接口封装的方式 DirectUI消息循环的简单封装 c++对象工厂 使用模板来解决接口继承问题 VC++ 使用attributes定义接口
c++以代理的方式来实现接口化编程
Clingingboy · 2013-11-05 · via 博客园 - Clingingboy

2013-11-05 14:13  Clingingboy  阅读(1054)  评论()    收藏  举报

假如你项目代码本身已经实现了很多的实体类,但并未采用接口,可以考虑以这种方式来实现接口化编程

struct ITest
{
    virtual void Test()=0;
};

class CTest
{
public:
    void Test() {}
};

class CTestProxy:public ITest
{
    void Test() 
    {  
        m_object.Test();
    }
protected:
    CTest m_object;
};

class TestObject
{
public:
    static void Demo()
    {
        ITest *pTest=new CTestProxy;
        pTest->Test();
    }
};

改过为模板化

template<class T, class I>
class CTestProxy:public I
{
    void Test() 
    {  
        m_object.Test();
    }
protected:
    T m_object;
};

再来看子类的实现

struct ISubTest:public ITest
{
    virtual void SubTest()=0;
};

class CSubTest:public CTest
{
public:
    void SubTest() {}
};

template<class T, class I>
class CSubTestProxy:public CTestProxy<T,I>
{
    void SubTest() 
    {  
        m_object.SubTest();
    }
};

此方法可以使用接口的形式重用原有实现代码

实体类与接口交互

struct ITest
{
    virtual void Test()=0;

    virtual ITest* GetProxy()=0;
    virtual void SetProxy(ITest* pTest)=0;
};

class CTest
{
public:
    CTest()
    {
        m_pTest=NULL;
    }
    void Test() {  }

    ITest* GetProxy() { return m_pTest; }
    void SetProxy(ITest* pTest) { m_pTest=pTest; }

    template<class T>
    T* Get()
    {
        ITest* pProxy=GetProxy();
        T* pT=dynamic_cast<T*>(pProxy);
        return pT;
    }

protected:
    ITest *m_pTest;
};

template<class T, class I>
class CTestProxy:public I
{
public:
    CTestProxy()
    {
        SetProxy(this);
    }

    void Test() 
    {  
        m_object.Test();
    }

    ITest* GetProxy() { return m_object.GetProxy(); }
    void SetProxy(ITest* pTest) { m_object.SetProxy(pTest); }

protected:
    T m_object;
};

添加一个指针便有了两边交互的能力

外部类继承:

需要将T m_object声明成为指针,该类并非为导出类,避免对外暴露实现