Facade mode of design mode (C++)

Author: Zhai Tianbao Steven
Copyright statement: The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source

1. What is the facade mode?

       Facade mode is a structural software design mode, also called appearance mode, which provides a unified interface to access the interfaces of multiple subsystems. For example, there are many roles in a restaurant, each role is a subsystem, and the restaurant is the overall system. When customers come to the restaurant, they only need to order food as required, and they don't need to care about how the restaurant operates.

       Advantages of the facade pattern:

  1. Simple and easy to use. Provides a simple interface for complex modules and systems to simplify operation.
  2. Subsystem independence is guaranteed. The independence between subsystems is good, and they are generally not affected by each other. How to use them is determined by the facade.
  3. System stability is guaranteed. When the subsystem is directly used, unpredictable exceptions may occur, the facade mode can regulate the call of the subsystem interface through the high-level interface, and effectively block the interaction between the subsystem and the client, thereby enhancing the robustness of the system.
  4. Good privacy. The facade encapsulates the specific details of the subsystem.

      Disadvantages of the facade pattern:

  1. Does not comply with the open-closed principle. Adding new systems requires modifications to the facade.
  2. High demands on developers. Developers need to understand the business logic relationship between subsystems, so as to ensure that the encapsulated high-level interface is effective and stable.

2. Facade mode

2.1 Structure diagram

       The client is the main function, and the facade is a total system, which manages multiple subsystems.

2.2 Code example

       Scene description: Go to a restaurant for dinner.

//Facade.h
/****************************************************/
#pragma once
#include <iostream>
#include <list>
#include <vector>
#include <string>

using namespace std;

// 厨师
class Cook
{
public:
	// 炒菜
	void cookMeal(vector<string> menu) {
		for (int i = 0; i < menu.size(); ++i) {
			cout << "正在炒:" << menu[i] << endl;
		}
	}
};

// 服务员
class Waiter
{
public:
	// 点菜
	void orderDishes(vector<string> menu) {
		for (int i = 0; i < menu.size(); ++i) {
			cout << "点菜:" << menu[i] << endl;
		}
	}

	// 收拾
	void clear() {
		cout << "打扫卫生。" << endl;
	}

};

// 前台
class Reception
{
public:
	// 欢迎
	void welcome() {
		cout << "欢迎光临!" << endl;
	}

	// 买单
	void bill() {
		cout << "买单完成,欢迎下次再来!" << endl;
	}

};

// 门面
class Facade
{
public:
	// 构造函数
	Facade() {
		m_cook = new Cook();
		m_waiter = new Waiter();
		m_reception = new Reception();
	}

	// 析构函数
	~Facade() {
		if (m_cook != nullptr) {
			delete m_cook;
			m_cook = nullptr;
		}
		if (m_waiter != nullptr) {
			delete m_waiter;
			m_waiter = nullptr;
		}
		if (m_reception != nullptr) {
			delete m_reception;
			m_reception = nullptr;
		}
	}

	// 经营
	void manage(vector<string> menu) {
		// 欢迎
		m_reception->welcome();

		// 服务员点菜
		m_waiter->orderDishes(menu);

		// 厨师炒菜
		m_cook->cookMeal(menu);

		// 客人用餐
		cout << "客人用餐中。" << endl;

		// 买单
		m_reception->bill();

		// 打扫卫生
		m_waiter->clear();
	}

private:
	Cook *m_cook;
	Waiter *m_waiter;
	Reception *m_reception;
};
//main.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "Facade.h"

using namespace std;

int main()
{
	Facade *facade = new Facade();
	// 餐馆运营
	vector<string> menu = { "红烧肉","土豆丝","酸菜鱼" };
	facade->manage(menu);
	delete facade;
	facade = nullptr;
	return 0;
}

       The program results are as follows.

       The facade mode is a widely used design mode. For example, in some industries that require professional technology, the entire technical route is huge, complex and delicate. A set of technical routes covers multiple sub-technologies. In such scenarios, use The facade mode can well realize the encapsulation of the algorithm library.

       Like in my work, because the algorithm library designed by our company often needs to cover dozens or even more functional classes, the combination of each class can realize some engineering technologies. If it is not combined and packaged, the algorithm library will be called of software engineers (without a professional technical background) don't know how to use it. However, complex functions can be encapsulated through the facade mode, and only a high-level interface is provided to the software department. Software engineers only need to call the interface according to the requirements given by the algorithm.

3. Summary

       I try my best to express my understanding of the Façade pattern with more common words and intuitive code routines. There may be some things that are not thoughtful. If you have different opinions, welcome to communicate in the comment area! I hope my examples will help you understand the Facade pattern better.

       If the article helps you, you can give me a like to let me know, I will be very happy ~ come on!

Guess you like

Origin blog.csdn.net/zhaitianbao/article/details/129878173