栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

设计模式之Facade外观模式

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设计模式之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 
    #include  
    using namespace std;
    
    void main()
    {
    	auto *poweronButton = new PoweronButton();
    	powerOnButton->pressButton();
    	delete powerOnButton;
    	system("pause");
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号