设计模式之Facade外观模式

1. 子系统角色

  • SubSystem(子系统角色):主板、内存、CPU、硬盘、操作系统

    //SubSystem(子系统角色)
    //主板
    class Motherboard
    {
    public:
    	Motherboard()
    	{
    		std::cout << "Motherboard 构造函数" << std::endl;
    	}
    	~Motherboard()
    	{
    		std::cout << "~Motherboard 析构函数" << std::endl;
    	}
    	void powerOnMotherboard()
    	{
    		std::cout << "------主板上电------" << std::endl;
    	}
    
    private:
    
    };
    //内存
    class Memory
    {
    public:
    	Memory()
    	{
    		std::cout << "Memory 构造函数" << std::endl;
    	}
    	~Memory()
    	{
    		std::cout << "~Memory 析构函数" << std::endl;
    	}
    	void selfCheckMemory()
    	{
    		std::cout << "------内存自检电------" << std::endl;
    	}
    
    private:
    
    };
    //CPU
    class CPU
    {
    public:
    	CPU()
    	{
    		std::cout << "CPU 构造函数" << std::endl;
    	}
    	~CPU()
    	{
    		std::cout << "~CPU 析构函数" << std::endl;
    	}
    	void runCPU()
    	{
    		std::cout << "------CPU运行------" << std::endl;
    	}
    
    private:
    
    };
    
    //HardDisk
    class HardDisk
    {
    public:
    	HardDisk() {
    		std::cout << "HardDisk 构造函数" << std::endl;
    	}
    	~HardDisk() {
    		std::cout << "~HardDisk 析构函数" << std::endl;
    	}
    	void readHardDisk() {
    		std::cout << "------读取硬盘------" << std::endl;
    	}
    };
    
    //OS
    class OS
    {
    public:
    	OS()
    	{
    		std::cout << "OS 构造函数" << std::endl;
    	}
    	~OS()
    	{
    		std::cout << "~OS 析构函数" << std::endl;
    	}
    	void loadOS()
    	{
    		std::cout << "------加载操作系统------" << std::endl;
    	}
    
    private:
    
    };

    2. 外观角色

  • Facade外观模式

    //Facade(外观模式)
    class PowerOnButton
    {
    public:
    	PowerOnButton()
    	{
    		std::cout << "PowerOnButton 构造函数" << std::endl;
    
    		m_pMotherboard = new Motherboard;
    		m_pMemory = new Memory;
    		m_pCPU = new CPU;
    		m_pHardDisk = new HardDisk;
    		m_pOS = new OS;
    	}
    	~PowerOnButton() 
    	{
    		std::cout << "~PowerOnButton 析构函数函数" << std::endl;
    		if (nullptr != m_pMotherboard)
    		{
    			delete m_pMotherboard;
    			m_pMotherboard = nullptr;
    		}
    
    		if (nullptr != m_pMemory)
    		{
    			delete m_pMemory;
    			m_pMemory = nullptr;
    		}
    
    		if (nullptr != m_pCPU)
    		{
    			delete m_pCPU;
    			m_pCPU = nullptr;
    		}
    
    		if (nullptr != m_pHardDisk)
    		{
    			delete m_pHardDisk;
    			m_pHardDisk = nullptr;
    		}
    
    		if (nullptr != m_pOS)
    		{
    			delete m_pOS;
    			m_pOS = nullptr;
    		}	
    	}
    
    	void pressButton()
    	{
    		std::cout << std::endl;
    
    		std::cout << "点击电源按钮..." << std::endl;
    		std::cout << "正在开机..." << std::endl;
    		std::cout << "...." << std::endl;
    		std::cout << ".." << std::endl;
    		m_pMotherboard->powerOnMotherboard();
    		m_pMemory->selfCheckMemory();
    		m_pCPU->runCPU();
    		m_pHardDisk->readHardDisk();
    		m_pOS->loadOS();
    		printf("开机完成!\n");
    	
    		std::cout << std::endl;
    	}
    
    private:
    	Motherboard *m_pMotherboard;
    	Memory *m_pMemory;
    	CPU *m_pCPU;
    	HardDisk *m_pHardDisk;
    	OS *m_pOS;
    };

    3. 主函数

    #include <string>
    #include <sstream> 
    using namespace std;
    
    void main()
    {
    	auto *powerOnButton = new PowerOnButton();
    	powerOnButton->pressButton();
    	delete powerOnButton;
    	system("pause");
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胖七少爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值