Introduction: First of all, please come with me to review the Bridge Mode in the design mode. The following is a simple example: in this design pattern, our abstract classes and implementation classes can be expanded and encapsulated separately so that they can be decoupled and many changes can be produced through combination. This... SyntaxHighlighter. all ();
Introduction:
First of all, please come with me to review the Bridge Mode in the design mode. There is not much nonsense:
In this design pattern, our abstract classes and implementation classes can be expanded and encapsulated separately so that they can be decoupled and many changes can be produced through combination. This idea also conforms to the design principle of "less inheritance and more combination. in the bridge mode, we can use the aggregate action class to bridge the ConreteImplementor and the refinedized aggregate action. But how does JavaScript implement bridging? Please follow me
1 // Validation class:
2
3
4 Validation = {
5 required: function (elem ){
6 return! $ (Elem). val (). trim (). isNullOrEmpty ();
7 },
8 email: function (elem ){
9 returnValidation. regexValidator ($ (elem). val (). trim (), Validation. Regex. email );
10 },
11 regexValidator: function (elemVal,/* string */regex ,){
12 if (! ElemVal. isNullOrEmpty ()&&! (ElemVal. match (regex, "\ g "))){
13 returnfalse;
14} else {
15 returntrue;
16 };
17 },
18 Regex :{
19 email:/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )? $/
20}
21 };
22 Validation. validateColl = {
23 'jq-validation-required': {validFunc: Validation. required, ErrMsg: 'required '},
24 'jq-validation-email ': {validFunc: Validation. email, ErrMsg: 'invalid email address '}
25 };
26 Validator class:
27
28
29 (function (){
30 varvalidator_elements_blur_selector = 'input [type = "text"] ';
31 varvalidator_elements_change_selector = 'select, input [type = "hidden"] ';
32 var validator_elements_selector = validator_elements_blur_selector + ',' + validator_elements_change_selector;
33
34 Validator = function (validateScopeSelector ){
35 this. _ validateColl = $. extend (true, {}, Validation. _ validateColl );
36 this. _ validateDom =$ (validateScopeSelector );
37 vartheValidator = this;
38 this. _ validateDom. delegate (validator_elements_blur_selector, 'blur', function (){
39 theValidator. validateInput (this, 'blur ');
40 });
41 this. _ validateDom. delegate (validator_elements_change_selector, 'change', function (){
42 varinputValidated = theValidator. validateInput (this, 'change ');
43 });
44 };
45
46 Validator. prototype = {
47 validate: function (){
48 varvalidated = true;
49 vartheValidator = this;
50 $ (validator_elements_selector, this. _ validateDom). each (function (){
51 if (! TheValidator. validateInput. call (theValidator, this )){
52 validated = false;
53 };
54 });
55 returnvalidated;
56 },
57 validateInput: function (elem, event ){
58 varinput = $ (elem );
59 varclassArr = input. attr ('class'). split ('');
60
61 varvalidated = true;
62 varinValidTable = new Hashtable ();
63 for (var I = 0; I <classArr. length; I ++ ){
64 varclassItem = classArr [I];
65 if (! ClassItem. startWith ('jq-validation ') continue;
66 varvalidateItem = this. _ validateColl [classItem];
67 if (validateItem & validateItem. validFunc ){
68 if (! ValidateItem. validFunc (input, validateItem )){
69 validated = false;
70 if (! StrPopupErr. isNullOrEmpty ()){
71 strPopupErr + = "";
72}
73 inValidTable. add (classItem, validateItem );
74}
75}
76}
77 returnvalidated;
78}
79 };
80 })();
81 // call example:
82
83
84 <input type = "text" class = "jq-validation-required"/>
85 <input type = "text" class = "jq-validation-email"/>
86
87 <input type = "button" onclick = "submit ()"/>
88
89