C++25设计模式之外观模式

C++25设计模式之外观模式,第1张


图片引用于百度

简介:隐藏系统的复杂性,向外部提供一个可以访问的接口,即单实例类

优点:降低了客户端与子系统的耦合关系,让子类更加易用,外部只需要和外观类交互

缺点:增加子类可能会修改外观类,不符合开闭原则,而且不能很好的限制外部使用子类

例子背景:现在有人想要买一台主机,借用上一个代理模式的代码,用外观类实现人购买电脑的 *** 作

外观模式代码:

  • 外观类:
#pragma once
#include "Producti7Mainframe.h"
#include "../Proxy/Proxy.h"

class Facade
{
private:
        shared_ptr<Proxy> m_proxy;
        shared_ptr<Producti7Mainframe> m_pimf;
private:
        Facade() {}
        ~Facade() = default;
        Facade(const Facade&) = delete;
        Facade(Facade&&) = delete;
        Facade& operator=(const Facade&) = delete;
        Facade& operator=(Facade&&) = delete;
public:
        void BuyI7Mainframe(People* people)
        {
                m_proxy->BuyMainFrame(people, m_pimf->CreateProduct());
        }
public:
        static Facade* GetInstance()
        {
                static Facade facade;
                return &facade;
        }
};
  • 人类:
#pragma once
#include "../Proxy/People.h"
#include 

class Peoples
{
private:
        Peoples()
        {
                m_peoples.insert(std::make_pair("张三", new People));
                m_peoples.insert(std::make_pair("李四", new People));
                m_peoples.insert(std::make_pair("王五", new People));
                auto zhangsan = m_peoples.at("张三");
                zhangsan->SetMoney(8000);
                zhangsan->SetName("张三");

                auto lisi = m_peoples.at("李四");
                lisi->SetMoney(20000);
                lisi->SetName("李四");

                auto wangwu = m_peoples.at("王五");
                wangwu->SetMoney(20000);
                wangwu->SetName("王五");
                wangwu->SetVip(true);
        }
        ~Peoples()
        {
                for (auto iter : m_peoples)
                {
                        delete iter.second;
                }
        }
        Peoples(const Peoples&) = delete;
        Peoples(Peoples&&) = delete;
        Peoples& operator=(const Peoples&) = delete;
        Peoples& operator=(Peoples&&) = delete;
private:
        unordered_map<std::string, People*> m_peoples;
public:
        People* GetPeople(const string& name) { return m_peoples.at(name); }
public:
        static Peoples* GetInstance()
        {
                static Peoples facade;
                return &facade;
        }
};
  • 产品类:
#pragma once
#include "../Decorator/i7Mainframe.h"
#include "../Decorator/AccessoriesHardDisk.h"
#include "../Decorator/AccessoriesMemoryBank.h"

class Producti7Mainframe
{
public:
        Producti7Mainframe() {}
        Mainframe* CreateProduct()
        {
                Mainframe* i7 = new i7Mainframe;
                Mainframe* ahd = new AccessoriesHardDisk(i7);
                Mainframe* amb = new AccessoriesMemoryBank(ahd);
                return amb;
        }
};
  • 引用:
#include "Facade.h"
#include "Peoples.h"

int main()
{
        Facade* fd = Facade::GetInstance();
        fd->BuyI7Mainframe(Peoples::GetInstance()->GetPeople("王五"));

        getchar();
        return 0;
}

总结:
外观模式(Facade):又叫作门面模式,是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式。

该模式对外有一个统一接口,外部应用程序不用关心内部子系统的具体细节,这样会大大降低应用程序的复杂度,提高了程序的可维护性

作者:丶梦爱
博客:https://blog.csdn.net/u014732297(转载请说明出处)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/674212.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存