适配器,桥接,代理,享元模式示例代码

适配器模式:

// 适配器模式 将一个类的接口转换成客户希望的另外一个接口
// 接口层
class OutInterface {
public:
    virtual ~OutInterface() = default;
    // 客户希望实现的接口 例如: 用户希望一次性打印姓名 年龄 成绩
    virtual void ShowInfomation() = 0;
};

// 驱动层
class Driver {
public:
    // 内部原本的接口 姓名,年龄,成绩分别在不同的接口中实现
    void ShowName(string name) { cout << "Name: " << name  << " " << endl;};
    void ShowAge(int age) { cout << "Age: " << age  << " "  << endl;};
    void ShowScore(int score) { cout << "score: " << score << " "  << endl;};
};

// 适配层
class Adapter : public OutInterface {
public:
    virtual void ShowInfomation() override {
        Driver dr;
        dr.ShowName("张三");
        dr.ShowAge(24);
        dr.ShowScore(100);
    }
};

TEST(Adapter, Adapter1)
{
    OutInterface* inc = new Adapter();
    inc->ShowInfomation();
    delete inc;
}

 

桥接模式:

// 抽象部分, 外部接口,抽象化产品
// 当一个类存在两个独立变化的维度,且这两个维度都需要进行扩展时。
// 当一个系统不希望使用继承或因为多层次继承导致系统类的个数急剧增加时。
// 当一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性时。
// 例子1: 画图形 正方形, 圆形,长方形 颜色:红色,黑色, 白色

// 颜色
class Color {
public:
    virtual void FillColor(string produce) = 0;
    virtual ~Color() {};
};

// 形状
class Shape {
protected:
    Color* fillcolor;
public:
    Shape(Color* userColor): fillcolor(userColor){};
    virtual void DrawShape() = 0;
};

// 具体的形状类
class Rectangle : public Shape {
public:
    virtual void DrawShape() override
    {
        fillcolor->FillColor("长方形");
    }
    Rectangle(Color* userColor) : Shape(userColor) {}
};

class Square : public Shape {
public:
    virtual void DrawShape() override
    {
        fillcolor->FillColor("正方形");
    }
    Square(Color* userColor) : Shape(userColor) {}
};

class White : public Color {
public:
    virtual void FillColor(string produce) override
    {
        cout << "entry White " << __func__ << endl;
        cout << "白色的" + produce << endl;
    }
};

class Black : public Color {
public:
    virtual void FillColor(string produce) override
    {
        cout << "entry Black " << __func__ << endl;
        cout << "黑色的" + produce << endl;
    }
};

TEST(bridge_module, bridge1)
{
    Color* w = new White();
    Color* blk = new Black();
    Rectangle rg(w);
    Square sq(blk);
    sq.DrawShape();
    rg.DrawShape();
}

 

代理模式:

class Service {
public:
    int QueryData() {
        // 服务端,查询数据,订阅数据,修改数据等
        cout << "this is service data" << endl;
        return 0;
    }
};
// 代理模式
class CProxy {
private:
    CProxy() {
    }
    static CProxy* Instance;
    Service m_server;
public:
    static CProxy* GetInstance() {
        if (Instance == nullptr) {
            Instance = new CProxy();
        }
        return Instance;
    }
    static CProxy& GetProxy() {
        static CProxy single;
        return single;
    }

    int QueryData() {
        // 代理访问服务端
        m_server.QueryData();
        return 0;
    }
};

CProxy* CProxy::Instance = nullptr;

class Client {
public:
    void ShowData()
    {
        CProxy* px = CProxy::GetInstance();
        px->QueryData();
        CProxy prx = CProxy::GetProxy();
        prx.QueryData();
    }
};


TEST(Proxy, Proxy1)
{
    Client cl;
    cl.ShowData();
}

 

享元模式

// 享元模式 共享数据
class SharedOption {
public:
    virtual void Option() = 0;
};

class SharedOptionA : public SharedOption{
public:
    virtual void Option() override {
        cout << "Opention A key:" + m_key << endl;
    }
    SharedOptionA(string optKey) : m_key(optKey) {}
private:
    string m_key;
};

class ShareFactory {
public:
    SharedOption* GetShareOpention(string opentionkey) {
        map<string, SharedOption*>::iterator item = m_OptionMap.find(opentionkey);
        if (item == m_OptionMap.end()) {
            SharedOption* opt = new SharedOptionA(opentionkey);
            m_OptionMap.insert(make_pair(opentionkey, opt));
            return opt;
        } else {
            cout << item->first << " " << "find" << endl;
            return item->second;
        }
    }

private:
    map<string, SharedOption*> m_OptionMap;
};

TEST(Shareddata, main)
{
    ShareFactory sf;
    SharedOption* opt1 = sf.GetShareOpention("first");
    opt1->Option();
    SharedOption* opt2 = sf.GetShareOpention("two");
    opt2->Option();
    SharedOption* opt3 = sf.GetShareOpention("three");
    opt3->Option();
    SharedOption* opt4 = sf.GetShareOpention("first");
    opt4->Option();
    SharedOption* opt5 = sf.GetShareOpention("two");
    opt5->Option();
}

 

posted on 2022-05-19 14:57  蜀山菜鸟  阅读(40)  评论(0)    收藏  举报