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

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

博客园 - 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声明成为指针,该类并非为导出类,避免对外暴露实现