接口,依赖反转,单元测试
一、IEnumerable接口使用
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace IEnumerable接口使用 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 int[] nums1 = new int[] { 1, 2, 3, 4, 5 }; 15 ArrayList nums2 = new ArrayList { 1, 2, 3, 4, 5 }; 16 Console.WriteLine(sum(nums2)); 17 Console.WriteLine(Avg(nums2)); 18 Console.WriteLine(sum(nums1)); 19 Console.WriteLine(Avg(nums1)); 20 } 21 //供方为整形数组和Arraylist 供方Array实现了IEnumerable接口,Arraylist也实现了IEnumerable这个接口,Array和Arraylist都遵守这个锲约保证自己可以被迭代 22 //需方为Sum 和 Avg这两个函数 ,需方我只要求你传进来的参数可以被迭代就行了,因为我要用foreach循环去迭代里面的每一个整数 23 24 //供方需方都遵守IEnumerable接口这个锲约 25 static int sum(IEnumerable nums) //int[] nums ;; ArrayList nums(object类型的所以要加int) 26 { 27 int sum = 0; 28 foreach (var n in nums) sum += (int )n; 29 return sum; 30 } 31 32 static double Avg(IEnumerable nums) 33 { 34 int sum = 0; 35 double count = 0; 36 foreach( var n in nums) { sum +=(int) n;count++; } 37 return sum / count; 38 } 39 40 } 41 }
二、接口(供需双方都满足的锲约)
类与类之间,对象与对象之间分工合作,面向对象世界里有一个术语叫做依赖,服务的提供者和服务的使用者之间有一个依赖关系。实现接口,必须要把接口里的所有成员全部实现。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /*========人和手机之间是解耦的============*/ 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace Interface_Example 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 // IPhone V = new NokiaPhone();实现接口的实例 16 var user = new PhoneUSER(new NokiaPhone()); //(V); 17 user.usephone(); 18 } 19 } 20 21 22 class PhoneUSER 23 { 24 private IPhone _phone; 25 public PhoneUSER (IPhone phone) 26 { 27 _phone = phone; 28 } 29 public void usephone() 30 { 31 _phone.Dial(); 32 _phone.PickUP(); 33 _phone.Send(); 34 _phone.Receive(); 35 36 } 37 38 39 } 40 41 42 43 interface IPhone 44 { 45 void Dial(); 46 void PickUP(); 47 void Send(); 48 void Receive(); 49 50 } 51 52 class NokiaPhone : IPhone 53 { 54 public void Dial() 55 { 56 Console.WriteLine("Nokia calling..."); 57 } 58 59 public void PickUP() 60 { 61 Console.WriteLine("Hello,this is tim"); 62 } 63 64 public void Receive() 65 { 66 Console.WriteLine("Nokia message ring..."); 67 } 68 69 public void Send() 70 { 71 Console.WriteLine("hello"); 72 } 73 } 74 75 class EricssonPhone : IPhone 76 { 77 public void Dial() 78 { 79 Console.WriteLine("EricssonPhone calling..."); 80 } 81 82 public void PickUP() 83 { 84 Console.WriteLine("hi,this is tim"); 85 } 86 87 public void Receive() 88 { 89 Console.WriteLine("Ericsson message ring..."); 90 } 91 92 public void Send() 93 { 94 Console.WriteLine("good evening"); 95 } 96 } 97 98 99 }
人和手机之间是解耦的。
三、依赖反转
人和手机之间是解耦的。什么是解耦?解耦在代码中的表现就是依赖反转(依赖倒置)。单元测试就是依赖反转在开发中的直接应用。
1 /*============强耦合,依赖关系,电扇依赖电源=======*/ 2 using System; 3 4 namespace InterfaceExample 5 { 6 7 class Program 8 { 9 var fan = new DeskFan(new PowerSupply()); 10 Console.Writeline(fan.Work()); 11 } 12 class PowerSupply 13 { 14 public int GetPower() 15 { 16 return 100; 17 } 18 } 19 20 class DeskFan 21 { 22 private PowerSupply _powerSupply; 23 public DeskFan(PowerSupply powerSupply) 24 { 25 _powerSupply = powerSupply; 26 } 27 public string Work() 28 { 29 int power = _powerSupply.GetPower(); 30 if (power<=0) 31 {return "won't work!!!!";} 32 else if(power<100) 33 {return "slow!!!!";} 34 else if(power<200) 35 {return "work fine!!!!!!";} 36 else 37 {return "warning!!!!!!";} 38 } 39 } 40 41 }
1 /*===================解耦==================*/ 2 using System; 3 4 namespace InterfaceExample 5 { 6 7 class Program 8 { 9 var fan = new DeskFan(new PowerSupply()); 10 Console.Writeline(fan.Work()); 11 12 } 13 14 InterFace IPowerSupply 15 { int GetPower();} 16 17 18 class PowerSupply : IPowerSupply 19 { 20 public int GetPower() 21 { 22 return 100; 23 } 24 } 25 26 class DeskFan 27 { 28 private IPowerSupply _powerSupply; 29 public DeskFan(IPowerSupply powerSupply) 30 { 31 _powerSupply = powerSupply; 32 } 33 public string Work() 34 { 35 int power = _powerSupply.GetPower(); 36 if (power<=0) 37 {return "won't work!!!!";} 38 else if(power<100) 39 {return "slow!!!!";} 40 else if(power<200) 41 {return "work fine!!!!!!";} 42 else 43 {return "warning!!!!!!";} 44 45 } 46 47 48 } 49 50 51 52 }