Design Patterns Proxy Pattern, Adapter Pattern and Facade Pattern

  Writing a wrapper interface based on another set of classes is a common API design task, for example, your job might be to maintain a large legacy codebase, and you'd rather audit a new, more Concise API to hide all the underlying legacy code; or you may have written a C++ API that later needs to provide a C interface to specific clients; or your API uses a third-party dependency library and you want clients to directly Use this library, but don't want to expose this library directly to clients.

  A potential side effect of creating a wrapper API is performance impact, mainly due to the extra level of indirection and the overhead of storing the state of the wrapper hierarchy. But for the situations mentioned above, doing so can create a higher quality, more targeted API, and it's worth the money. Next, the proxy pattern, adapter pattern, and facade pattern will be summarized in order of increasing degree of difference between the wrapper layer and the original interface.

1. Proxy mode

1.1 Class Diagram

  

  On the basis of the above, add the virtual interface shared by the original API of the proxy. This is to better preserve the synchronization of the two APIs. The premise of this is that you can modify the original API.

  

1.2 Code Implementation

#pragma once 

#include <iostream>
using namespace std;

class IOriginal
{
public:
    IOriginal()
    {
        cout << "IOriginal Sturuct" << endl;
    }

    virtual ~ IOriginal()
    {
        cout << "IOriginal Destruct" << endl;
    }

    virtual void DoSomething() = 0;
};

class COriginal : public IOriginal
{
public:
    COoriginal()
    {
        cout << "COriginal Sturuct" << endl;
    }

    ~ COoriginal()
    {
        cout << "COriginal Destruct" << endl;
    }

    void DoSomething()
    {
        cout << "COriginal DOSomething" << endl;
    }
}

class CProxy : public IOriginal
{
public:
    CProxy ()
    {
        m_pOriginal = new COoriginal();
        cout << "CProxy Sturuct" << endl;
    }

    ~ CProxy ()
    {
        if (m_pOriginal != NULL)
        {
            delete COoriginal;
            m_pOriginal = NULL;
        }
        cout << "CProxy Destruct" << endl;
    }

    void DoSomething()
    {
        m_pOriginal->DoSomething();
        cout << "CProxy DOSomething" << endl;
    }

private:
    COoriginal * m_pOriginal;
}

2. Adapter Mode

  The adapter design pattern converts the interface of a class into a compatible but different interface. Similar to the proxy pattern, the adapter design pattern is also a single component wrapper, but the interface of the adapter class and Yuan Shilei can be different.

2.1 Class Diagram

  

2.2 Code Implementation

#pragma once 

#include <iostream>
using namespace std;

class COriginal
{
public:
    COoriginal()
    {
        cout << "COriginal Sturuct" << endl;
    }

    ~ COoriginal()
    {
        cout << "COriginal Destruct" << endl;
    }

    void DoSomethingA()
    {
        cout << "COriginal DOSomethingA" << endl;
    }
}

class CAdapter
{
public:
    CProxy ()
    {
        m_pOriginal = new COoriginal();
        cout << "CProxy Sturuct" << endl;
    }

    ~ CProxy ()
    {
        if (m_pOriginal != NULL)
        {
            delete COoriginal;
            m_pOriginal = NULL;
        }
        cout << "CProxy Destruct" << endl;
    }

    void fun()
    {
        //"......"
        m_pOriginal->DoSomethingA();
        cout << "CProxy DOSomething" << endl;
    }

private:
    COoriginal * m_pOriginal;
}
View Code

3. Appearance Mode

  The Facade pattern provides a simplified interface for a set of classes. It actually defines a higher-level interface to make the underlying subsystem easier to use.

3.1 Class Diagram

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325412461&siteId=291194637