依赖倒置原则(DIP)小案例

本文通过对比正常代码和使用依赖倒置原则重构后的代码,阐述了如何通过抽象和接口来减少类之间的耦合,提高代码的稳定性和可读性,特别是在Unity游戏开发中的实践应用。
摘要由CSDN通过智能技术生成

高层模块不应该依赖低层模块,它们都应该依赖抽象。

抽象不应该依赖于细节,细节应该依赖于抽象。

减少类与类之间的耦合性,提高系统的稳定性和可读性。


 小案例代码(正常写,不使用依赖倒置原则

// 界面层
public interface ICalculatorForm
{
    void Show();
}

// 业务逻辑层
public class CalculatorBLL
{
    private Adder adder; // 加法类
    private Subtractor subtractor; // 减法类
    private Multiplier multiplier; // 乘法类
    private Divider divider; // 除法类

    public CalculatorBLL()
    {
        adder = new Adder();
        subtractor = new Subtractor();
        multiplier = new Multiplier();
        divider = new Divider();
    }

    public double Add(double a, double b)
    {
        return adder.Calculate(a, b);
    }

    public double Subtract(double a, double b)
    {
        return subtractor.Calculate(a, b);
    }

    public double Multiply(double a, double b)
    {
        return multiplier.Calculate(a, b);
    }

    public double Divide(double a, double b)
    {
        return divider.Calculate(a, b);
    }
}

小案例代码(使用依赖倒置原则重构后 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 依赖倒置原则(DIP)——案例
/// 高层模块不应该依赖低层模块,它们都应该依赖抽象。抽象不应该依赖于细节,细节应该依赖于抽象。
/// </summary>
public class OperationController : MonoBehaviour
{
    private void Start()
    {
        Operation_Add operation_Add = new Operation_Add();
        MyOperation myOperation_Add = new MyOperation(operation_Add);
        Debug.Log(myOperation_Add.UnifyOperation(10, 20)); 

        Operation_Sub operation_Sub = new Operation_Sub();
        MyOperation myOperation_Sub = new MyOperation(operation_Sub);
        Debug.Log(myOperation_Sub.UnifyOperation(10, 20));

        Operation_Mul operation_Mul = new Operation_Mul();
        MyOperation myOperation_Mul = new MyOperation(operation_Mul);
        Debug.Log(myOperation_Mul.UnifyOperation(10, 20));

        Operation_Div operation_Div = new Operation_Div();
        MyOperation myOperation_Div = new MyOperation(operation_Div);
        Debug.Log(myOperation_Div.UnifyOperation(10, 20));
    }
}
/// <summary>
/// 运算逻辑层
/// </summary>
public class MyOperation
{
    IOperation operation;//统一接口

    public MyOperation(IOperation operation)
    {
        this.operation = operation;
    }
    public double UnifyOperation(double a,double b)
    {
        return operation.Calculate(a,b);
    }
}
/// <summary>
/// 数据访问层
/// </summary>
public interface IOperation
{
    double Calculate(double a, double b);
}
public class Operation_Add : IOperation
{
    public double Calculate(double a, double b)
    {
        return a + b;
    }
}
public class Operation_Sub : IOperation
{
    public double Calculate(double a, double b)
    {
        return a - b;
    }
}
public class Operation_Mul : IOperation
{
    public double Calculate(double a, double b)
    {
        return a * b;
    }
}
public class Operation_Div : IOperation
{
    public double Calculate(double a, double b)
    {
        return a / b;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值