Facade: provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes the subsystem easier to use.
Mode description:
SubSystem (SubSystem)
Responsible for handling complex logic processing. Do not deal with customers directly.
Facade)
Interacts with subsystems and provides easy-to-use functions or interfaces to customers.
Client)
Interact with the subsystem through the facade, without the need to care about the details of the subsystem.
UML class diagram:
Basic code for facade mode:
[Cpp]
/*************************************** *****************************
Filename: Facade. h
Created: 2012-09-28
Author: firehood
Purpose: The Design Pattern of firehood-Facade Pattern
**************************************** *****************************/
# Pragma once
# Include <iostream>
Using namespace std;
Class sub‑ema
{
Public:
Subscribe EMA (void ){}
~ Subscribe EMA (void ){}
Void Operate1 (void)
{
Cout <"subreceivema: Operation 1" <endl;
}
Void Operate2 (void)
{
Cout <"subreceivema: Operation 2" <endl;
}
};
Class SubSystemB
{
Public:
SubSystemB (void ){}
~ SubSystemB (void ){}
Void Operate1 (void)
{
Cout <"SubSystemB: Operation 1" <endl;
}
Void Operate2 (void)
{
Cout <"SubSystemB: Operation 2" <endl;
}
};
Class SubSystemC
{
Public:
Subscribe EMC (void ){}
~ Subscribe EMC (void ){}
Void Operate1 (void)
{
Cout <"sub‑emc: Operation 1" <endl;
}
Void Operate2 (void)
{
Cout <"subreceivemc: Operation 2" <endl;
}
};
Class Facade
{
Public:
Facade (void ){}
Virtual ~ Facade (void ){}
Void MethodA (void)
{
Cout <"method A:" <endl;
M_SubSystemA.Operate1 ();
M_SubSystemB.Operate2 ();
M_SubSystemC.Operate2 ();
}
Void MethodB (void)
{
Cout <"method B:" <endl;
M_SubSystemA.Operate1 ();
M_SubSystemB.Operate1 ();
M_SubSystemC.Operate1 ();
}
Private:
Sub‑ema m_sub‑ema;
SubSystemB m_SubSystemB;
Subscribe EMC m_subscribe EMC;
};
Client call code:
[Cpp]
# Include "Facade. h"
# Include <iostream>
Using namespace std;
Int main (int argc, char * argv [])
{
Cout <"************************************ * "<endl;
Cout <"firehood learning design model-facade model" <endl;
Cout <"************************************ * "<endl;
Facade facade;
Facade. MethodA ();
Facade. MethodB ();
System ("pause ");
Return 0;
}
Running result:
*************************************
Firehood learning design model-facade Model
*************************************
Method:
Sub‑ema: Operation 1
SubSystemB: Operation 2
Subscribe EMC: Operation 2
Method B:
Sub‑ema: Operation 1
SubSystemB: Operation 1
Subscribe EMC: Operation 1