• 设计模式——12.外观模式


    外观模式(Facade)

    外观模式(Facade)简介:

    外观模式为子系统中的一组接口提供一个已知的界面,即定义一个高层接口,使子系统更加容易使用

    外观模式体现了依赖倒转原则迪米特法则的思想,是常用的设计模式。

    外观模式结构:

    Facade

    C++代码

    子系统类:

    //file: SubSystem.h
    #pragma once
    class SubSystemA
    {
    public:
    	SubSystemA();
    	virtual ~SubSystemA();
    	
    	void featureA();
    };
    
    class SubSystemB
    {
    public:
    	SubSystemB();
    	virtual ~SubSystemB();
    
    	void featureB();
    };
    
    class SubSystemC
    {
    public:
    	SubSystemC();
    	virtual ~SubSystemC();
    
    	void featureC();
    };
    
    //file: SubSystem.cpp
    #include "pch.h"
    #include "SubSystem.h"
    #include <iostream>
    
    ///SubSystemA
    SubSystemA::SubSystemA() {}
    
    SubSystemA::~SubSystemA() {}
    
    void SubSystemA::featureA()
    {
    	std::cout << "SubSystemA's Feature A ." << std::endl;
    }
    
    ///SubSystemB
    SubSystemB::SubSystemB() {}
    
    SubSystemB::~SubSystemB() {}
    
    void SubSystemB::featureB()
    {
    	std::cout << "SubSystemB's Feature B ." << std::endl;
    }
    
    ///SubSystemC
    SubSystemC::SubSystemC() {}
    
    SubSystemC::~SubSystemC() {}
    
    void SubSystemC::featureC()
    {
    	std::cout << "SubSystemC's Feature C ." << std::endl;
    }
    

    外观类:

    //file: Facade.h
    #pragma once
    #include "SubSystem.h"
    
    class Facade
    {
    public:
    	Facade();
    	virtual ~Facade();
    
    	void Operation();
    
    private:
    	SubSystemA *m_SA;
    	SubSystemB *m_SB;
    	SubSystemC *m_SC;
    };
    
    //file: Facade.cpp
    #include "pch.h"
    #include "Facade.h"
    #include <iostream>
    
    Facade::Facade()
    {
    	m_SA = new SubSystemA();
    	m_SB = new SubSystemB();
    	m_SC = new SubSystemC();
    }
    
    Facade::~Facade()
    {
    	delete m_SA;
    	delete m_SB;
    	delete m_SC;
    }
    
    void Facade::Operation()
    {
    	m_SA->featureA();
    	m_SB->featureB();
    	m_SC->featureC();
    }
    

    客户端调用:

    //file: FacadePattern.cpp : This file contains the 'main' function. Program execution begins and ends there.
    #include "pch.h"
    #include "Facade.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	Facade facade;
    	facade.Operation();
    
    	return 0;
    }
    

    C#代码

    子系统类:

    public class SubSystemA
    {
    
    	public void Feature1()
    	{
    		Console.WriteLine("SubSystemA's Feature 1 .");
    	}
    
    	public void Feature2()
    	{
    		Console.WriteLine("SubSystemA's Feature 2 .");
    	}
    }
    

    外观类:

    class Facade
    {
    	private SubSystemA subA = null;
    	private SubSystemB subB = null;
    	private SubSystemC subC = null;
    
    	public Facade()
    	{
    		subA = new SubSystemA();
    		subB = new SubSystemB();
    		subC = new SubSystemC();
    	}
    
    	public void Func1()
    	{
    
    		subA.Feature1();
    		subB.Feature1();
    		subC.Feature1();
    	}
    
    	public void Func2()
    	{
    		subA.Feature2();
    		subB.Feature2();
    		subC.Feature2();
    	}
    }
    

    客户端测试:

    class Program
    {
    	static void Main(string[] args)
    	{
    		Facade facade = new Facade();
    
    		facade.Func1();
    
    		Console.WriteLine("__________");
    
    		facade.Func2();
    
    		Console.ReadKey();
    	}
    }
    

    外观模式的优点及缺点

    优点:

    使用外观模式之后,外观类可以给客户端提供单一操作界面,减少了不必要的类引用以及功能的整合。

    缺点:

    因为是将所有子系统都集中在Facade类中,当子系统过多时,,则会导致Facade类过于庞大而较难维护,此时,可以对Facade进行重构,将功能相近的子系统进行整合,减少内部的依赖性。

    REF

    书籍:

    设计模式与游戏开发、大话设计模式

    GitHub:

    https://github.com/me115/design_patterns

  • 相关阅读:
    移动SI[转]
    什么是云计算
    ToString()格式和用法大全[转]
    SQLJOIN之完全用法[转]
    DIV样式汇总
    CSS+DIV之强化background属性
    SQLserver中用convert函数转换日期格式
    session丢失与解决办法的资料
    div滤镜结合ajax,实现登录
    什么是ajax
  • 原文地址:https://www.cnblogs.com/sylvan/p/10127548.html
Copyright © 2020-2023  润新知