当前位置: 移动技术网 > IT编程>开发语言>.net > C#委托的价值,结合业务场景分析

C#委托的价值,结合业务场景分析

2020年07月23日  | 移动技术网IT编程  | 我要评论

1、业务背景

技术为业务而生,技术为解决业务问题而存在,技术脱离业务就变得没有价值,我们在探讨某一个技术带来的价值时,都需要有一定的业务背景作为前提。我们先来看如下需求背景:

定义一个学生类,属性包含学生姓名、学号、年龄、创建时间,行为包含学生可以使用正确的方式对不同国家的人打招呼,如对中国人打招呼为:张三,你好!对美国人打招呼为jack  hello!

2、解决方式1,使用传统的分支逻辑判断

示例代码及调用方式:

 1     public class student
 2     { 
10         public string stuname { get; set; }
11 
12         public string stunum { get; set; }
13 
14         public int age { get; set; }
15 
16         public datetime createtime { get; set; }
17 
18         /// <summary>
19         /// 对不同国家的人说hello,每个国家的表达方式不一样
20         /// </summary>
21         /// <param name="name"></param>
22         /// <param name="peopletype"></param>
23         public void sayhello(string name, peopletype peopletype) {
25             console.writeline($"{this.stuname }开始打招呼");
27             switch (peopletype)
28             {
29                 case peopletype.chinese:
30                     console.writeline($"{name},你好!");
31                     break;
32                 case peopletype.american:
33                     console.writeline($"{name},hello");
34                     break;
35                 default:
36                     throw new exception("enum  peopletype exception");
37             }
38         }
39     }
       public enum peopletype
       {
            chinese = 1,
            american = 2
       }
 1     {
 2         student student = new student() {
 3             stuname = "wjl",
 4             stunum = "14216600010",
 5             age = 18,
 6             createtime = datetime.now
 7         };
 8         student.sayhello("张三", peopletype.chinese);
 9         student.sayhello("jack", peopletype.american);
10     }

该种方式的优缺点:

假如业务有变动,需要增加一个对马来西亚国家的人打招呼的功能,就得在枚举中增加马来西亚的枚举类型,在sayhello方法中增加一个对马来西亚类型的分支逻辑判断,这种方式导致的问题就是任意分支变化都得修改方法,代码很不稳定,如果业务逻辑更复杂变得难以维护。但这种方式增加公共逻辑方便,如:console.writeline($"{this.stuname }开始打招呼");

3、解决方式2,针对对不同类型国家的人,定义不同的方法

示例代码及调用方式:

 1     public class student
 2     {
 4         public enum peopletype
 5         {
 6             chinese = 1,
 7             american = 2
 8         }
 9 
10         public string stuname { get; set; }
11 
12         public string stunum { get; set; }
13 
14         public int age { get; set; }
15 
16         public datetime createtime { get; set; }
17 
18         /// <summary>
19         /// 对中国人说你好
20         /// </summary>
21         /// <param name="name"></param>
22         public void sayhellochinese(string name)
23         {
24             console.writeline($"{this.stuname }开始打招呼");
25             console.writeline($"{name},你好!");
26         }
27 
28         /// <summary>
29         /// 对美国人说你好
30         /// </summary>
31         /// <param name="name"></param>
32         public void sayhelloamerican(string name)
33         {
34             console.writeline($"{this.stuname }开始打招呼");
35             console.writeline($"{name},hello");
36         }
37     }
 1     {
 2          console.writeline("调用说你好的功能,方式2");
 3          student student = new student()
 4          {
 5              stuname = "wjl",
 6              stunum = "14216600010",
 7              age = 18,
 8              createtime = datetime.now
 9          };
10          student.sayhellochinese("张三");
11          student.sayhelloamerican("jack");
12     }

该种方式的优缺点:

假如业务变动,需要增加一个对马来西亚国家的人打招呼的功能,就要增加一个方法,不影响别的方法。但这种方式增加公共逻辑会导致多个方法有很多重复代码,如上述代码中的console.writeline($"{this.stuname }开始打招呼");,这种方式不利于代码复用,如果要修改这些公共逻辑或者增加更多的公共逻辑,需要修改的地方较多,不利于维护。

4、解决方式3,利用委托,将不同的业务逻辑分离出去,相同的业务逻辑提取出来

示例代码及调用方式:

 1     public class student
 2     {
 3         public delegate void sayhellodelegate(string name);
 4         public string stuname { get; set; }
 5         public string stunum { get; set; }
 6         public int age { get; set; }
 7         public datetime createtime { get; set; }
 8         public void sayhelloperfect(string name, sayhellodelegate sayhello ) {
 9             console.writeline($"{this.stuname }开始打招呼");
10             sayhello.invoke(name);
11         }
12         #region 方式2
13         /// <summary>
14         /// 对中国人说你好
15         /// </summary>
16         /// <param name="name"></param>
17         public void sayhellochinese(string name)
18         {
19             console.writeline($"{name},你好!");
20         }
21         /// <summary>
22         /// 对美国人说你好
23         /// </summary>
24         /// <param name="name"></param>
25         public void sayhelloamerican(string name)
26         {
27             console.writeline($"{name},hello");
28         }
29         #endregion
30     }
 1     {
 2         student student = new student()
 3         {
 4            stuname = "wjl",
 5            stunum = "14216600010",
 6            age = 18,
 7            createtime = datetime.now
 8         };
9 sayhellodelegate method1 = student.sayhellochinese; 10 student.sayhelloperfect("张三", method1); 13 sayhellodelegate method2 = student.sayhelloamerican; 14 student.sayhelloperfect("jack", method2); 15 }

该种方式的优缺点:

这种处理方式将逻辑作为参数传递,将不同的业务逻辑分离出去,交给调用者传递,保证了现有方法的稳定,增加公共逻辑(console.writeline($"{this.stuname }开始打招呼");)方便,实现了代码重用,对不同的逻辑分离维护简单,实现逻辑解耦,鱼和熊掌兼得,方便维护升级。相同的东西用一个方法实现,不同的各自去写,然后通过委托组合,加方法满足不同的场景,如果业务逻辑或者说方法特别复杂,就推荐用这种方式去处理。这就是委托对程序设计带来的价值。

推荐阅读:c#委托进阶,事件和委托,一次就看明白

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网