Preface: Design patterns are very important in our programming! Design patterns represent best practices that are commonly adopted by experienced object-oriented software developers. Design patterns are solutions to common problems faced by software developers during the software development process. These solutions are the result of trial and error by numerous software developers over a considerable period of time. I'm currently learning design patterns, would you guys like to join me? 1. What is a design pattern?In software design, a concise and elegant solution to a specific problem. Summarizing previous experience and applying it reasonably to a certain scenario can solve practical problems. 2. Five design principles of design pattern (SOLID)S-Single Responsibility Principle
O-Open Closed Principle
L-Liskov substitution principle
I-Interface independence principle
D-Dependency leads to principle
3. Why do we need design patterns?Readability
Scalability
Reusability
reliability
4. Singleton PatternDefinition: Unique & globally accessible. Ensure that a class has only one instance and provide a global access point to it.
Application scenario: content that can be cached, such as a login pop-up window.
Let’s look at this pseudocode: const creatLoginLayer = () => { const div = document.createElement("div"); div.innerHtml = "Login floating window"; div.style.display = "none"; document.body.appendChild(div); return div; }; document.getElementById("loginBtn").onclick = () => { const loginLayer = creatLoginLayer(); loginLayer.style.display = "block"; }; The function of There is nothing wrong with this logic, but think about it, these codes will be executed every time the login button is clicked. What if there are many places in a project that need this? The lines we have above are just a few short lines. What if there are hundreds, thousands or even tens of thousands? Isn't it a great loss of performance? At this time, our singleton mode comes in handy. After using the singleton pattern: const getSingle = (fn) => { let result; return (...rest) => { return result || (result = fn.apply(this.rest)); }; }; const creatLoginLayer = () => { const div = document.createElement("div"); div.innerHtml = "Login floating window"; div.style.display = "none"; document.body.appendChild(div); return div; }; const createSingleLoginLayer = getSingle(createLoginLayer); document.getElementById("loginBtn").onclick = () => { const loginLayer = createSingleLoginLayer(); loginLayer.style.display = "block"; }; As you can see, a This concludes this article about the singleton pattern design pattern in You may also be interested in:
|
<<: Solution to slow response of Tomcat server
>>: Detailed explanation of the binlog log analysis tool for monitoring MySQL: Canal
About JS, CSS CSS: Stylesheet at the top Avoid CS...
Table of contents 1. The origin of fork 2. Early ...
Being an operation and maintenance engineer is a ...
The filter attribute defines the visual effect of...
1. Add a hard disk 2. Check the partition status:...
TABLE> <TR> <TD> <TH> <CA...
Table of contents 1. Software Package 2. Install ...
In the previous article [Detailed explanation of ...
After the worker process is started, it will firs...
VMware Preparation CentOS preparation, here is Ce...
Table of contents Working principle: What does th...
Table of contents 1. Introduction 2. Create a Vit...
1. Cause: The effect after the subbox is set to f...
1. Delete file command: find the corresponding di...
Check the Python version python -V If it is below...