DIP的实际举例

news/2025/2/25 7:44:58

SOLID原则。

依赖倒置原则(DIP)的核心是高层模块不应该依赖于低层模块,二者都应该依赖于抽象(接口或抽象类)

例如,随着业务的发展,订单总金额的计算规则可能需要根据不同的客户类型或促销活动进行调整。如果最初没有使用接口来抽象计算逻辑,那么可能需要直接修改具体的业务代码,这会增加代码的修改风险和维护成本。而使用接口可以方便地实现不同的计算策略,通过依赖注入等方式进行切换,提高系统的灵活性和可扩展性。
接下来我们逐步优化,把他变得满足SRP和OCP、DIP原则了。

循环累加

public class Order {
    private double[] productPrices;

    public Order(double[] productPrices) {
        this.productPrices = productPrices;
    }

    // 计算订单总金额的方法
    public double calculateTotalAmount() {
        double total = 0;
        for (double price : productPrices) {
            total += price;
        }
        return total;
    }
}

当业务发展,需要根据不同客户类型(比如普通客户、VIP 客户)和促销活动(比如满减、折扣)来调整订单总金额的计算规则时,就需要直接修改 calculateTotalAmount 方法的代码。例如,VIP 客户享受 9 折优惠,满 100 减 20 的促销活动,代码修改如下:

public class Order {
    private double[] productPrices;
    private String customerType;

    public Order(double[] productPrices, String customerType) {
        this.productPrices = productPrices;
        this.customerType = customerType;
    }

    // 计算订单总金额的方法,修改后变得复杂
    public double calculateTotalAmount() {
        double total = 0;
        for (double price : productPrices) {
            total += price;
        }
        if ("VIP".equals(customerType)) {
            total *= 0.9;
        }
        if (total >= 100) {
            total -= 20;
        }
        return total;
    }
}

这种直接修改的方式,随着业务规则的不断增加和变化,代码会变得越来越复杂和难以维护,修改一处可能会影响到其他部分的逻辑,增加了出错的风险。

抽象工厂

我们发现,订单类和计算金额的修改原因、修改速率不一样,根据SRP原则,利用抽象工厂模式,把他抽出来,如下。

// 订单类
public class Order {
    private double[] productPrices;
    private String customerType;

    public Order(double[] productPrices, String customerType) {
        this.productPrices = productPrices;
        this.customerType = customerType;
    }

    // 计算总金额,依赖工厂类
    public double calculateTotalAmount() {
        return OrderTotalCalculatorFactory.calculateTotal(productPrices, customerType);
    }
}

// 订单总金额计算工厂类
public class OrderTotalCalculatorFactory {
    public static double calculateTotal(double[] productPrices, String customerType) {
        if ("Regular".equals(customerType)) {
            double total = 0;
            for (double price : productPrices) {
                total += price;
            }
            return total;
        } else if ("VIP".equals(customerType)) {
            double total = 0;
            for (double price : productPrices) {
                total += price;
            }
            total *= 0.9;
            return total;
        } else {
            double total = 0;
            for (double price : productPrices) {
                total += price;
            }
            if (total >= 100) {
                total -= 20;
            }
            return total;
        }
    }
}

引入抽象类

如果再有新的计费类型时,我们需要修改OrderTotalCalculatorFactory ,再添加一个if-else,然后在里面写代码,违反了OCP原则。所以我们不把具体代码写到OrderTotalCalculatorFactory 里,而是再抽一层,可以是接口,也可以不是。(这样不是说没有改动,但是改动点小了。是扩展本类,而不是修改)
但是为了专业(把相同的抽出来,作为上层),我们没有理由不写成接口。
此外如果再想加一个过期时间校验功能(我们的优惠、会员等都是有时间的),先校验,然后再计算金额。这个时候就体现出接口的好处来了。

这样就是所谓的DIP了:由原来的order订单依赖于金额计算,现在加了一个接口,二者都依赖于这个接口了。

// 订单总金额计算策略接口
public interface OrderTotalCalculator {
    double calculateTotal(double[] productPrices);
}

// 普通客户计算策略实现类
public class RegularCustomerCalculator implements OrderTotalCalculator {
    @Override
    public double calculateTotal(double[] productPrices) {
        double total = 0;
        for (double price : productPrices) {
            total += price;
        }
        return total;
    }
}

// VIP客户计算策略实现类
public class VIPCustomerCalculator implements OrderTotalCalculator {
    @Override
    public double calculateTotal(double[] productPrices) {
        double total = 0;
        for (double price : productPrices) {
            total += price;
        }
        total *= 0.9;
        return total;
    }
}

// 工厂类
public class OrderTotalCalculatorFactory {
    public static OrderTotalCalculator createCalculator(String customerType) {
        if ("Regular".equals(customerType)) {
            return new RegularCustomerCalculator();
        } else if ("VIP".equals(customerType)) {
            return new VIPCustomerCalculator();
        } 
        // 目前只有这两种情况,后续可扩展
        return null;
    }
}


// 组合优于继承
public class Order {
    private double[] productPrices;
    private String customerType;
    private OrderTotalCalculator calculator;

    public Order(double[] productPrices, String customerType) {
        this.productPrices = productPrices;
        this.customerType = customerType;
        this.calculator = OrderTotalCalculatorFactory.createCalculator(customerType);
    }

    public double calculateTotalAmount() {
        return calculator.calculateTotal(productPrices);
    }
}

http://www.niftyadmin.cn/n/5865194.html

相关文章

毕业离校管理系统的开发与需求分析

在当今信息化的时代背景下,高校的毕业生离校管理工作也逐渐向数字化转型。为了提高工作效率,减少人为错误,增强信息透明度,毕业离校管理系统应运而生。该系统旨在为学校提供一个高效、准确的毕业生离校管理平台,从而提…

python类型转换深浅拷贝

1.类型转换 1.1 int(x):转化为一个整数&#xff0c;只能转换由纯数字组成的字符串 float->int 浮点型强转整形会去掉小数点后面的数&#xff0c;只保留整数部分 a 1.2 print(type(a)) #<class float> b int(a) print(type(b)) #<class int>print(int…

flutter项目构建常见问题

最近在研究一个验证码转发的app&#xff0c;原理是尝试读取手机中对应应用的验证码进行自动转发。本次尝试用flutter开发&#xff0c;因为之前没有flutter开发的经验&#xff0c;遇到了诸多环境方面的问题&#xff0c;汇总一些常见的问题如下。希望帮助到入门的flutter开发者&a…

【cv】vs2022配置opencv

release下配置包含目录和库目录 E:\sdk\sdk_cuda12.3\opencv490\include E:\sdk\sdk_cuda12.3\opencv490\include\opencv2 E:\sdk\sdk_cuda12.3\opencv490\lib release下配置包含链接器输入的依附依赖项 opencv_world490.lib release编译文件夹下需手动复制opencv_world49…

如何安装vm 和centos

以下分别是VMware和CentOS的安装方法&#xff1a; 安装VMware 下载安装包&#xff1a;访问VMware官方网站&#xff0c;根据你的操作系统版本&#xff0c;下载对应的VMware Workstation安装包。 运行安装程序&#xff1a;以管理员身份运行下载好的安装包。在安装向导中&#…

WebRTC解析

一、WebRTC 协议概述 WebRTC&#xff08;Web Real-Time Communication&#xff09;是由 Google 发起并成为 W3C 标准的实时音视频通信技术&#xff0c;核心特点&#xff1a; 零插件&#xff1a;浏览器原生支持端到端加密&#xff08;SRTP DTLS&#xff09;P2P 优先架构&…

《链表》学习——链表理论基础

链表&#xff0c;一种通过指针串联在一起的线性结构&#xff0c;每一个节点由两部分组成&#xff0c;一个是数据域&#xff0c;一个是指针域&#xff08;存放指向下一个节点的指针&#xff09;&#xff0c;最后一个节点的指针域指向null&#xff08;空指针&#xff09;。 链表…

Xlua 编译 Windows、UWP、Android、iOS 平台支持库

Xlua 编译 Windows、UWP、Android、iOS 平台支持库 Windows&#xff1a; 安装 Visual Studio&#xff08;推荐 2017 或更高版本&#xff09; 安装 CMake&#xff08;https://cmake.org/&#xff09; macOS&#xff1a; 安装 Xcode 和命令行工具 安装 CMake 检查 cmake 是否安…