The appearance pattern provides a unified interface for accessing a group of interfaces of a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use. The appearance mode makes the interface simple and simplifies the interface of the subsystem. The appearance pattern is simple, in short, to simplify the interface of your class, encapsulate a series of complex processes internally, and provide only the simplest interfaces externally.
Structure Chart:
Applicable scenario:
when you want to provide a simple interface for a complex subsystem. Subsystems tend to become more complex as they evolve. Most patterns will produce more and smaller classes when used. This makes subsystems more reusable and easier to customize for subsystems, but it also brings some usability difficulties for users who do not need a custom subsystem. The façade can provide a simple default view, which is sufficient for most users, and those who need more customization can cross the façade layer.
There is a large dependency between the client program and the implementation part of the abstract class. The introduction of a façade separates the subsystem from the customer and other subsystems, which can improve the subsystem's independence and portability.
When you need to build a hierarchical subsystem, use the façade pattern to define the entry points for each layer in the subsystem. If subsystems are interdependent, you can simplify their dependencies by allowing them to communicate only through a façade.
Realize:
Code Snippets 1:facade.h
Facade.h
#ifndef _facade_h_
#define _FACADE_H_
class subsystem1{public
:
Subsystem1 ();
~subsystem1 ();
void Operation ();
Protected:
private:
};
Class subsystem2{public
:
Subsystem2 ();
~SUBSYSTEM2 ();
void Operation ();
Protected:
private:
};
Class facade{public
:
façade ();
~facade ();
void Operationwrapper ();
Protected:
private:
subsystem1* _subs1;
subsystem2* _subs2;
};
#endif//~_facade_h_
Code Snippets 2:facade.cpp
Facade.cpp
#include "Facade.h"
#include <iostream>
using namespace std;
Subsystem1::subsystem1 () {
}
subsystem1::~subsystem1 () {
}
void Subsystem1::operation () {
cout << "Subsystem2 operation ..." <<endl;
}
Subsystem2::subsystem2 () {
}
subsystem2::~subsystem2 () {
}
void Subsystem2::operation () {
cout<< "Subsystem2 operation ..." <<endl;
}
Facade::facade () {
this->_subs1 = new Subsystem1 ();
THIS->_SUBS2 = new Subsystem2 ();
}
Facade::~facade () {
delete _subs1;
Delete _subs2;
}
void Facade::operationwrapper () {
this->_subs1->operation ();
This->_subs2->operation ();
}
Code Snippets 3:main.cpp
Main.cpp
#include "Facade.h"
#include <iostream>
using namespace std;
int main (int argc,char* argv[]) {
facade* f = new façade ();
F->operationwrapper ();
return 0;
}
Let's look at one more example: