当前位置:网站首页>Appearance mode (facade)

Appearance mode (facade)

2022-06-26 13:06:00 baboon_ chen

Reference resources :

Design pattern ( Facade mode ) (refactoringguru.cn)

[ Appearance mode (Facade Pattern ) Detailed explanation (biancheng.net)](http://c.biancheng.net/view/1366.html)

design-patterns-cpp/facade at master · JakubVojvoda/design-patterns-cpp · GitHub

One 、 What is the appearance mode ?

Definition : Provide a consistent interface for multiple complex subsystems , This makes these subsystems more accessible , It's also called Facade mode .

It is equivalent to inserting an intermediate layer between the caller and the interface provider , Packaging common logic , Provide API Interface . So that users can only care about how to use , Don't worry about how complicated the process is .

such as , During telephone shopping , The customer only needs to place an order by telephone , It is not necessary for him to understand the process from shipment to receipt . This simplifies the shopping process for all customers , It also makes shopping more efficient .

 Insert picture description here

Two 、 Realization

appearance (Facade) The pattern includes the following main characters :

  1. appearance (Facade): Provide a common interface for multiple subsystems .
  2. Subsystem (Sub System): Realize some functions of the system , Customers can access it through the facade role .
  3. Customer (Client): Access the functions of each subsystem through a look and feel role .

 Insert picture description here

Facade.cpp

/*
 * C++ Design Patterns: Facade
 * Author: Jakub Vojvoda [github.com/JakubVojvoda]
 * 2016
 *
 * Source code is licensed under MIT License
 * (for more details see LICENSE)
 *
 */

#include <iostream>

/*
 * Subsystems
 * implement more complex subsystem functionality
 * and have no knowledge of the facade
 */
class SubsystemA
{
public:
  void suboperation()
  {
    std::cout << "Subsystem A method" << std::endl;
    // ...
  }
  // ...
};

class SubsystemB
{
public:
  void suboperation()
  {
    std::cout << "Subsystem B method" << std::endl;
    // ...
  }
  // ...
};

class SubsystemC
{
public:
  void suboperation()
  {
    std::cout << "Subsystem C method" << std::endl;
    // ...
  }
  // ...
};

/*
 * Facade
 * delegates client requests to appropriate subsystem object
 * and unified interface that is easier to use
 */
class Facade
{
public:
  Facade() : subsystemA(), subsystemB(), subsystemC() {}
  
  void operation1()
  {
    subsystemA->suboperation();
    subsystemB->suboperation();
    // ...
  }
  
  void operation2()
  {
    subsystemC->suboperation();
    // ...
  }
  // ...
  
private:
  SubsystemA *subsystemA;
  SubsystemB *subsystemB;
  SubsystemC *subsystemC;
  // ...
};


int main()
{
  Facade *facade = new Facade();
  
  facade->operation1();
  facade->operation2();
  delete facade;
  
  return 0;
}

3、 ... and 、 Advantages and disadvantages , Applicable scenario

advantage

  • Reduce the coupling between the subsystem and the client , So that changes to the subsystem do not affect the client class that invokes it .
  • Shielding subsystem components from customers , It reduces the number of objects handled by customers , And make it easier to use the subsystem .

shortcoming

  • Adding a new subsystem may need to modify the appearance class or the client's source code , Against the principle of opening and closing .

原网站

版权声明
本文为[baboon_ chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261224552072.html