策略模式:它定义算法家族,分别封装起来,让他们之间互相替换,此模式让算法的变化,不会影响使用算法的客户。
1 /// <summary>
2 /// 策略模式
3 /// </summary>
4 public interface IidCheck
5 {
6 bool Check(string id);
7 }
8
9 public class Id18Check : IidCheck
10 {
11 public bool Check(string cid)
12 {
13 string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃", "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null, null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null, null, null, null, "国外" };
14 double iSum = 0;
15 System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|X|x)$");
16 System.Text.RegularExpressions.Match mc = rg.Match(cid);
17 if (!mc.Success)
18 {
19 //Log.WriteLog("身份证验证失败:" + cid);
20 return false;
21 }
22 cid = cid.ToLower();
23 cid = cid.Replace("x", "a");
24 if (aCity[int.Parse(cid.Substring(0, 2))] == null)
25 {
26 //Log.WriteLog("身份证验证失败:" + cid);
27 //非法地区
28 return false;
29 }
30 try
31 {
32 DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
33 }
34 catch
35 {
36 //Log.WriteLog("身份证验证失败:" + cid);
37 return false;//非法生日
38 }
39 for (int i = 17; i > 0; i--)
40 {
41 int w = (int)(System.Math.Pow(2, i) % 11);
42 int a = int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
43
44 iSum += w * a;
45 }
46
47 Dictionary<string, string> dic = new Dictionary<string, string>();
48 dic.Add("0", "1");
49 dic.Add("1", "0");
50 dic.Add("2", "X");
51 dic.Add("3", "9");
52 dic.Add("4", "8");
53 dic.Add("5", "7");
54 dic.Add("6", "6");
55 dic.Add("7", "5");
56 dic.Add("8", "4");
57 dic.Add("9", "3");
58 dic.Add("10", "2");
59
60 if (dic[(iSum % 11).ToString()] == cid.Substring(cid.Length - 1, 1))
61 {
62 return true;
63 }
64 return false;
65 }
66 }
67
68 public class Id15Check : IidCheck
69 {
70 public bool Check(string cid)
71 {
72 string[] aCity15 = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃", "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null, null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null, null, null, null, "国外" };
73
74 System.Text.RegularExpressions.Regex rg15 = new System.Text.RegularExpressions.Regex(@"^\d{15}$");
75 System.Text.RegularExpressions.Match mc15 = rg15.Match(cid);
76 if (!mc15.Success)
77 {
78 return false;
79 }
80 cid = cid.ToLower();
81 cid = cid.Replace("x", "a");
82 if (int.Parse(cid.Substring(0, 2)) > aCity15.Length)
83 {
84 //Log.WriteLog("身份证验证失败:" + cid);
85 return false;//非法地区
86 }
87 if (aCity15[int.Parse(cid.Substring(0, 2))] == null)
88 {
89 //Log.WriteLog("身份证验证失败:" + cid);
90 return false;//非法地区
91 }
92 try
93 {
94 DateTime.Parse("19" + cid.Substring(6, 2) + "-" + cid.Substring(8, 2) + "-" + cid.Substring(10, 2));
95 }
96 catch
97 {
98 //Log.WriteLog("身份证验证失败:" + cid);
99 return false;//非法生日
100 }
101 return true;
102 }
103 }
104
105 /// <summary>
106 /// 工厂模式
107 /// </summary>
108 public class IdCheckFactory
109 {
private IdCheckFactory(){}
110 /// <summary>
111 /// 单例模式
112 /// </summary>
113 public static readonly IdCheckFactory Instance = new IdCheckFactory();
114
115 public bool Check(string id)
116 {
117 if (string.IsNullOrEmpty(id))
118 {
119 return false;
120 }
121
122 switch (id.Length)
123 {
124 case 15:
125 IidCheck model = new Id15Check();
126 return model.Check(id);
127 case 18:
128 IidCheck model18 = new Id18Check();
129 return model18.Check(id);
130 default:
131 return false;
132 }
133 }
134 }
调用控制台代码:
1 /// <summary>
2 /// client端
3 /// </summary>
4 class Program
5 {
6 static void Main(string[] args)
7 {
8 Console.WriteLine("请输入要验证的身份证号");
9 string id = Console.ReadLine();
10 while (id != "#")
11 {
12 bool succ = IdCheckFactory.Instance.Check(id);
13
14 if (succ)
15 {
16 Console.WriteLine("身份证输入正确");
17 }
18 else
19 {
20 Console.WriteLine("身份证输入错误");
21 }
22 Console.WriteLine("请输入要验证的身份证号");
23 id = Console.ReadLine();
24 }
25
26 }
27 }