🧑💻作者:猫十二懿
🎊公众号:猫十二懿
迪米特法则(LoD),也叫最少知识原则,是指在减少系统各个组件之间的耦合度。它的核心思想是,一个对象应该对其他对象有尽可能少的了解。也就是说,对象应该仅与其直接交互的对象交互,而不是与其它对象的内部交互。
迪米特特点:
假设有一个订单处理系统,其中包含订单(Order)、客户(Customer)和商品(Product)三个类。订单类包含客户和商品的信息,客户类包含客户的姓名和地址,商品类包含商品的名称和价格。
示例:订单处理系统包含三个类: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
// 订单类
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 只保存了客户和订单项的信息,而不知道它们的具体实现方式。这样做在一定程度上降低了类之间的耦合度,使得系统更加灵活和可扩展。