In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to master the JavaScript strategy model, in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
The use of Policy Mode in JavaScript
What is a design pattern?
Imagine an electronics enthusiast who has no formal training, but over time has designed and manufactured many useful electronic devices: amateur radios, Geiger counters, alarms, and so on. One day the enthusiast decided to go back to school to study for an electronics degree so that his talents could be formally recognized. As the course unfolded, the enthusiast suddenly found that the contents of the course were all familiar. Deja vu is not the term or the way of expression, but the concept behind it. This enthusiast keeps learning some names and principles, which he doesn't know, but in fact he has been using them for many years. The whole process is just one epiphany after another.
Meditation on Design patterns, John Vlissides, Chapter 1, Section 1.2
We must have encountered a lot of similar scenarios when we were writing code. With the increase of experience, we are more and more adept at dealing with these common scenes, and even summed up targeted "routines". The next time we encounter such problems, we will directly use "tricks" to solve them, saving both worry and effort. These "routines" that gradually accumulate in the process of software development are design patterns.
One of the goals of the design pattern is to improve the reusability, scalability and maintainability of the code. Because of this, although sometimes we do not know a certain design pattern, but after reading the relevant books or articles, there will be a "ah, this is the design pattern" suddenly clear.
If you feel the same way after reading this article, congratulations, you've been on the road to being an effective programmer.
What is a strategic model?
Strategy pattern is a simple but commonly used design pattern, which has a wide range of applications. Let's first understand the concept of policy pattern, and then use code examples to understand it more clearly.
The policy pattern consists of two parts: one is a policy group that encapsulates different policies, and the other is Context. Combine and delegate to give Context the ability to enforce policies, making it reusable, extensible, and maintainable, and avoiding a lot of copy-and-paste work.
The typical application scenario of policy pattern is the encapsulation of verification rules in form verification. Let's take a look at it in detail through a simple example:
Rough form check
A common login form code is as follows:
Login var loginForm = document.getElementById ('login-form'); loginForm.onsubmit = function (e) {e.preventDefault (); var account= document.getElementById ("account"). Value; var pwd = document.getElementById ("password"). Value; if (account===null | | account==='') {alert (' Mobile number cannot be empty') Return false;} if (pwd===null | | pwd==='') {alert ('password cannot be empty'); return false;} if (! / (^ 1 [3 | 4 | 5 | 7 | 8] [0-9] {9} $) / .test (account) {alert ('incorrect mobile number format') Return false;} if (pwd.length
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.