introduce
Facade provides a consistent interface for a set of interfaces in the subsystem. This module defines a high-level interface, which is worth more easy to use in this subsystem.
text
Appearance mode not only simplifies interfaces in the class, but also decouples interfaces from callers. Appearance pattern is often considered a must for developers, it can encapsulate some complex operations and create a simple interface for invocation.
Appearance mode is often used in JavaScript class library. Through it, it encapsulates some interfaces for compatibility with multiple browsers. Appearance mode allows us to indirectly call subsystems, thereby avoiding unnecessary errors caused by direct access to subsystems.
The advantage of the appearance mode is that it is easy to use and is also lightweight in itself. However, there are also disadvantages. When the appearance mode is continuously used by developers, it will cause certain performance problems, because the availability of functions must be detected every time it is called.
Below is a piece of unoptimized code. We use appearance mode to create a cross-browser usage method by detecting browser characteristics.
The code copy is as follows:
var addMyEvent = function (el, ev, fn) {
if (el.addEventListener) {
el.addEventListener(ev, fn, false);
} else if (el.attachEvent) {
el.attachEvent('on' + ev, fn);
} else {
el['on' + ev] = fn;
}
};
Let’s take another simple example. To put it bluntly, use one interface to encapsulate other interfaces:
The code copy is as follows:
var mobileEvent = {
// ...
stop: function (e) {
e.preventDefault();
e.stopPropagation();
}
// ...
};
Summarize
So when to use Appearance Mode? Generally speaking, there are three stages:
First, in the early stage of design, you should consciously separate the two different layers, such as the classic three-layer structure, to establish an appearance facade between the data access layer and the business logic layer, the business logic layer and the presentation layer.
Secondly, during the development stage, subsystems often become more and more complex due to continuous reconstruction and evolution. Adding appearance to Facade can provide a simple interface and reduce their dependencies.
Third, when maintaining a large legacy system, it may be difficult to maintain this system. It is also very suitable to use appearance Facade at this time. Develop an appearance Facade class for the system system to provide a clearer interface for designing rough and highly complex legacy code, allowing the new system to interact with Facade objects, and Facade interacts with legacy code.
Reference: Big Talk Design Mode