【C#语言入门】22. 接口、依赖反转、单元测试

【C#语言入门】22. 接口、依赖反转、单元测试

一、接口与单元测试

  • 接口的产生:自底向上(重构),自顶向下(设计)
  • C#中接口的实现(隐式,显式,多接口)
  • 语言对面向对象设计的内建支持:依赖反转,接口隔离,开/闭原则......
csharp 复制代码
//不使用接口,会很麻烦
class Program
{
    static void Main(string[] args)
    {
        int[] nums1 = new int[] { 1, 2, 3, 4, 5 };
        ArrayList nums2 = new ArrayList() { 1, 2, 3, 4, 5};
        Console.WriteLine(Sum(num1));
        Console.WriteLine(Avg(num1));
        Console.WriteLine(Sum(num2));
        Console.WriteLine(Avg(num2));
    }

    static int Sum(int[] nums)
    {
        int sum = 0;
        foreach (int x in nums) {
        sum += x;}
        return sum;
    }

    static double Avg(int[] nums)
    {
        int sum = 0;
        double count = 0;
        foreach(int x in nums)
        {
            sum += x;
            count++;
        }
        return sum/count;
    }

    static int Sum(ArrayList nums)
    {
        int sum = 0;
        foreach(var x in nums)
        {
            sum += (int)x;
        }
        return sum;
    }

    static double Avg(ArrayList nums)
    {
        int sum = 0;
        double count = 0;
        foreach(var x in nums)
        {
            sum += (int)x;
            count++;
        }
        return sum/count;
    }
}
csharp 复制代码
//使用了IEnumerable接口
class Program
{
    static void Main(string[] args)
    {
        int[] nums1 = new int[] { 1, 2, 3, 4, 5 };
        ArrayList nums2 = new ArrayList() { 1, 2, 3, 4, 5};

        Console.WriteLine(Sum(nums1));
        Console.WriteLine(Avg(nums1));
        Console.WriteLine(Sum(nums2));
        Console.WriteLine(Avg(nums2));
    }

    static int Sum(IEnumerable nums)
    {
        int sum = 0;
        foreach (int x in nums) {
        sum += x;}
        return sum;
    }

    static double Avg(IEnumerable nums)
    {
        int sum = 0;
        double count = 0;
        foreach(int x in nums)
        {
            sum += x;
            count++;
        }
        return sum/count;
    }

}

二、依赖

在现实世界中,由于自身能力有限,合作是一个非常常见的行为,而面向对象是对现实世界的抽象,因此也存在着类与类、对象与对象的合作,即"依赖"。依赖的出现伴随着耦合,依赖越直接,耦合度越高。

csharp 复制代码
class Program
{
    static void Main(string[] args)
    {
        var user = new PhoneUser(new NokiaPhone());
        //var user = new PhoneUser(new EricssonPhone());超级无敌松耦合,只要改这里就能实现手机切换
        user.UsePhone();
    }

}

class PhoneUser
{
    private IPhone _phone;
    public PhoneUser(IPhone phone)
    {
        _phone = phone;
    }

    public void UsePhone()
    {
        _phone.Dail();
        _phone.PickUp();
        _phone.Send();
        _phone.Receive();
    }
}

interface IPhone
{
    void Dail();
    void PickUp();
    void Send();
    void Receive();
}

class NokiaPhone:IPhone
{
    public void Dail()
    {
        Console.WriteLine("Nokia calling...");
    }

    public void PickUp()
    {
        Console.WriteLine("Hello!This is Tom");
    }

    public void Receive()
    {
        Console.WriteLine("Nokia message ring...");
    }

    public void Send()
    {
        Console.WriteLine("Hello!");
    }
}

class EricssonPhone : IPhone
{
    public void Dail()
    {
        Console.WriteLine("Ericsson calling...");
    }

    public void PickUp()
    {
        Console.WriteLine("Hello!This is Tom");
    }

    public void Receive()
    {
        Console.WriteLine("Ericsson message ring...");
    }

    public void Send()
    {
        Console.WriteLine("Hello!");
    }
}

注意:在代码中如果有可以替换的地方,就一定有接口的存在,接口就是为了解耦/松耦合而生,他最大的好处就是可以让功能的提供方可以方便替换,减少紧耦合时提供方可能带来的风险。

  • 依赖反转原则
相关推荐
小oo呆几秒前
【学习心得】Python好库推荐——pipx
linux·开发语言·python
CoderYanger4 分钟前
C.滑动窗口-求子数组个数-越短越合法——3258. 统计满足 K 约束的子字符串数量 I
java·开发语言·算法·leetcode·1024程序员节
2301_8079973810 分钟前
代码随想录-day56
算法
AI科技星17 分钟前
时空运动的几何约束:张祥前统一场论中圆柱螺旋运动光速不变性的严格数学证明与物理诠释
服务器·数据结构·人工智能·python·科技·算法·生活
幽络源小助理18 分钟前
《已调试》SpringBoot景区寄存管理系统源码 - 免费JavaWeb项目下载 | 幽络源
java·开发语言·spring boot
豆沙沙包?22 分钟前
2025年--Lc302-415. 字符串相加--java版
java·开发语言
JIngJaneIL23 分钟前
基于Java民宿管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
杰克尼23 分钟前
蓝桥云课-13. 定时任务
java·开发语言·算法
m0_7269659826 分钟前
RAG小实战
开发语言·python
Q1808095127 分钟前
手撕BP与CNN:不依赖外源库,探寻神经网络原理
c#