Detailed explanation of JavaScript facade pattern
External communication with a subsystem must be carried out through a facade object of a system. This is the facade pattern. This article mainly introduces the relevant information of JavaScript facade mode in detail. It has certain reference value. Interested friends can refer to it. I hope it can help you.
The facade mode has the following two roles:
1. Facade role
The client can call this role method, and there are subsystems in this role application (knowing the functions and responsibilities of the relevant subsystem(s)). This role will delegate all requests sent from the client to the corresponding subsystem.
2. Subsystem role
Can have one or more subsystems at the same time. Each subsystem is not a separate class, but a collection of classes. Each subsystem can be called directly by the client (this will increase the client code), or called by the facade role. The subsystem does not know the existence of the facade. To the subsystem, the facade is just another client.
Let’s take a look at a simple requirement that can cash out the facade model: the corresponding pet adoption certificate that the owner has applied for for his pet dog
From this simple requirement we can roughly analyze our Required: Some relevant information about the owner Person class and the pet dog Dog class
In the following example, the verification of the interface will be involved. Now post the code first
//(定义一个静态方法来实现接口与实现类的直接检验 //静态方法不要写出Interface.prototype ,因为这是写到接口的原型链上的 //我们要把静态的函数直接写到类层次上 //定义一个接口类 var Interface=function (name,methods) {//name:接口名字 if(arguments.length<2){ alert("必须是两个参数") } this.name=name; this.methods=[];//定义一个空数组装载函数名 for(var i=0;i<methods.length;i++){ if(typeof methods[i]!="string"){ alert("函数名必须是字符串类型"); }else { this.methods.push( methods[i]); } } }; Interface.ensureImplement=function (object) { if(arguments.length<2){ throw new Error("参数必须不少于2个") return false; } for(var i=1;i<arguments.length;i++){ var inter=arguments[i]; //如果是接口就必须是Interface类型 if(inter.constructor!=Interface){ throw new Error("如果是接口类的话,就必须是Interface类型"); } //判断接口中的方法是否全部实现 //遍历函数集合 for(var j=0;j<inter.methods.length;j++){ var method=inter.methods[j];//接口中所有函数 //object[method]传入的函数 //最终是判断传入的函数是否与接口中所用函数匹配 if(!object[method]||typeof object[method]!="function" ){ //实现类中必须有方法名字与接口中所用方法名相同 throw new Error("实现类中没有完全实现接口中的所有方法") } } } }
(1) Master class (Person class)
function Person() { this.name="测试"; this.address="居住在中国"; this.getInfo=function () { return "名字"+this.name+" 地址"+this.address; }; this.learn=function () { alert("学习的方法"); } this.marray=function () { alert("marray"); } //验证接口 Interface.ensureImplement(this,PersonDao);//验证该类是否全部实现接口中的方法 }
(2) Pet dog (Dog class)
var DogDao=new Interface("DogDao",["getInfo","call","run"]); var Dog=function () { this.name="gg"; this.getInfo=function () { return "狗狗的名字"+this.name; }; this.call=function () { }; this.run=function () {}; Interface.ensureImplement(this,DogDao);//验证接口 }
(3) Now owners can apply for pet adoption certificates for their pet dogs -----Client code
First method: No need The client code of the facade method is as follows
function action(person,dog) { var r="GG"+new Date().getDay()+Math.floor(Math.random()*11); var str="办证成功:编号"+r +"<br/>主人信息"+person.getInfo() +"<br>狗狗的信息:"+dog.getInfo(); return str; }document.write(action(new Person(),new Dog()));
The second method: use the facade mode-----leave the complex things to the facade, and the customer End pressure can be reduced
#1: The following processing is performed in the facade
function facade(person,dog) { var r="GG"+new Date().getDay()+Math.floor(Math.random()*11); var str="办证成功:编号"+r +"<br/>主人信息"+person.getInfo() +"<br>狗狗的信息:"+dog.getInfo(); this.action=function () {//相当于实例的方法 return str; }; }
##2: The code the client is responsible for using is
function action2(person,dog) { document.write(new facade(person,dog).action()); } action2(new Person(),new Dog());
To summarize, we can see that clients that are not suitable for the facade mode need to handle more complex businesses. After using the facade, complex things are processed in the facade. The client only needs to make a simple call.
A simple diagram structure to understand the facade pattern:
Related recommendations:
Design patterns in php - Facade Mode_PHP Tutorial
Laravel Detailed Explanation of Inversion of Control and Facade Mode Concepts
Learn PHP Design Pattern PHP Implements Facade Mode ( Facade)_PHP
The above is the detailed content of Detailed explanation of JavaScript facade pattern. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a
