• Java设计模式七大原则-迪米特法则


    🧑‍💻作者:猫十二懿

    🏡账号:CSDN个人博客Github

    🎊公众号:猫十二懿

    迪米特原则

    1、迪米特原则介绍

    迪米特法则(LoD),也叫最少知识原则,是指在减少系统各个组件之间的耦合度。它的核心思想是,一个对象应该对其他对象有尽可能少的了解。也就是说,对象应该仅与其直接交互的对象交互,而不是与其它对象的内部交互。

    迪米特特点:

    1. 强调的前提是在类的结构设计上,每一个类都应当尽量降低成员的访问权限
    2. 降低类之间的耦合度:迪米特原则要求一个对象对其他对象的了解应该尽可能少,即一个类不应该直接依赖于其他对象的细节和实现。这样可以降低类之间的耦合度,降低代码的复杂性和维护成本。
    3. 增强类的可复用性:通过将细节和实现封装在类的内部,可以使得该类更加独立和可复用,与其他类的关系更加清晰明了。
    4. 提高系统的可维护性:迪米特原则使得系统的各个模块之间的关系更加松散,降低了彼此之间的影响,从而提高了系统的可维护性。

    2、具体例子

    假设有一个订单处理系统,其中包含订单(Order)、客户(Customer)和商品(Product)三个类。订单类包含客户和商品的信息,客户类包含客户的姓名和地址,商品类包含商品的名称和价格。

    2.1 不符合迪米特原则

    示例:订单处理系统包含三个类:Order、Customer 和 Product。

    其中,Order 类包含了 Customer 和 Product 两个类的信息,这违反了迪米特原则,因为 Order 类直接依赖于 Customer 和 Product 两个类的实现细节,使得耦合度过高,导致代码的复杂度和维护成本增加。

    Order类

    /**
     * @author Shier
     * CreateTime 2023/4/22 22:49
     */
    public class Order {
        private String orderId;
        private Customer customer;
        private Map<Product, Integer> productQuantities;
    
        public Order(String orderId, Customer customer) {
            this.orderId = orderId;
            this.customer = customer;
            this.productQuantities = new HashMap<>();
        }
    
        /**
         * 构造商品
         * @param product
         * @param quantity
         */
        public void addProduct(Product product, int quantity) {
            if (!productQuantities.containsKey(product)) {
                productQuantities.put(product, 0);
            }
            productQuantities.put(product, productQuantities.get(product) + quantity);
        }
    
        /**
         * 计算总价
         * @return
         */
        public double calculateTotalPrice() {
            double totalPrice = 0;
            for (Product product : productQuantities.keySet()) {
                int quantity = productQuantities.get(product);
                totalPrice += product.getPrice() * quantity;
            }
            return totalPrice;
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    Product类

    /**
     * @author Shier
     * CreateTime 2023/4/22 22:50
     */
    public class Product {
        private String name;
        private double price;
    
        public Product(String name, double price) {
            this.name = name;
            this.price = price;
        }
    
        public String getName() {
            return name;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    Customer类

    /**
     * @author Shier
     * CreateTime 2023/4/22 22:49
     */
    public class Customer {
        private String name;
        private String address;
    
        public Customer(String name, String address) {
            this.name = name;
            this.address = address;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    测试类:

    /**
     * @author Shier
     *
     */
    public class Test {
        public static void main(String[] args) {
            Customer customer = new Customer("张三", "北京市海淀区");
            Product product1 = new Product("电视机", 5000);
            Product product2 = new Product("冰箱", 3000);
    
            Order order = new Order("202304220001", customer);
            order.addProduct(product1, 2);
            order.addProduct(product2, 1);
    
            double totalPrice = order.calculateTotalPrice();
            System.out.println("订单总价:" + totalPrice);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    输出结果:订单总价:13000.0

    2.2 符合迪米特原则

    // 订单类
    public class Order {
        private String orderId;
        private Customer customer;
        private List<OrderItem> orderItems;
    
        public Order(String orderId, Customer customer) {
            this.orderId = orderId;
            this.customer = customer;
            this.orderItems = new ArrayList<>();
        }
    
        public void addOrderItem(Product product, int quantity) {
            OrderItem orderItem = new OrderItem(product, quantity);
            orderItems.add(orderItem);
        }
        
        // 其他方法
    }
    
    // 客户类
    public class Customer {
        private String name;
        private String address;
    
        public Customer(String name, String address) {
            this.name = name;
            this.address = address;
        }
    
        // 其他方法
    }
    
    // 商品类
    public class Product {
        private String name;
        private double price;
    
        public Product(String name, double price) {
            this.name = name;
            this.price = price;
        }
    
        // 其他方法
    }
    
    // 订单项类
    public class OrderItem {
        private Product product;
        private int quantity;
    
        public OrderItem(Product product, int quantity) {
            this.product = product;
            this.quantity = quantity;
        }
    
        // 其他方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    Order 类依赖于 Customer、Product 和 OrderItem 三个类,但是并不直接访问它们中的任何一个。相反,Order 只保存了客户和订单项的信息,而不知道它们的具体实现方式。这样做在一定程度上降低了类之间的耦合度,使得系统更加灵活和可扩展。

  • 相关阅读:
    Redis 内存淘汰策略
    云计算——虚拟化技术简介及Docker安装(二)
    【C++】万字详解IO流(输入输出流+文件流+字符串流)
    mysql双主主键冲突
    LLaMA 2 - 你所需要的一切资源
    LVGL V8.3 使用lvgl文件系统读取SD卡内容基于Arduino
    数据向好,分析师预测美联储GDP或将翻一番?
    代码随想录day38|DP动态规划登场|理论基础|509. 斐波那契数|70. 爬楼梯|746. 使用最小花费爬楼梯|Golang
    java并发编程:LinkedBlockingQueue详解
    c/c++查看代码片段运行时间
  • 原文地址:https://blog.csdn.net/qq_56098191/article/details/130899750