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
Load one or more features <template> <di...
This article uses examples to explain the concept...
Forms in HTML can be used to collect various type...
Written in front In today's Internet field, N...
Preface Normal business needs: upload pictures, E...
Generally, we rarely meet HR, but once we do, it c...
Based on the Vue image magnifier component packag...
One purpose Select a local folder on the Html pag...
1. When the mobile terminal processes the list sl...
You can call it directly in the Linux shell envir...
What? What star coat? Well, let’s look at the pic...
Table of contents Logical Layering Separate busin...
I've been learning about stacking contexts re...
Table of contents jQuery's $.ajax The beginni...
This article shares the specific code of JavaScri...