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

奋斗吧
奋斗吧
擅长邻域:未填写

标签: Java设计模式七大原则-迪米特法则

2023-05-27 18:23:27 44浏览

迪米特法则

🧑‍💻作者:猫十二懿

🏡账号: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;
    }
    
}

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;
    }
}

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;
    }
}

测试类:

/**
 * @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);
    }
}

输出结果:订单总价: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;
    }

    // 其他方法
}

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

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695