-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLawDemeter_counter.java
64 lines (54 loc) · 1.96 KB
/
LawDemeter_counter.java
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
59
60
61
62
63
64
package src;
import java.util.ArrayList;
import java.util.List;
/**
* 这个例子违反了迪米特原则,为了方面理解,把全部类放在了一起。
* 1. 对象职责不清晰,不单一。顾客类下单购物,还实现了价格计算逻辑。
* 2. 对象依赖了朋友的朋友。顾客类依赖了购买朋友的朋友商品。
*/
public class LawDemeter_counter {
public LawDemeter_counter() {
return;
}
// 定义一个顾客类,里面直接依赖了购买商品,且包含商品统计与价格计算逻辑
public class Customer {
private String name;
private List<Product> products;
public Customer(String name) {
this.name = name;
this.products = new ArrayList<>();
}
public void buy(Product product) {
// 违反迪米特法则:顾客类只跟购物车打交道,而不应该直接与商品类交互
if (product.getPrice() > 1000) {
System.out.println(product.getName() + "'s price exceeds range:" + product.getPrice());
} else {
products.add(product);
double totalPrice = calculateTotalPrice();
System.out.println(name + " purchased " + product.getName() + " for " + product.getPrice());
System.out.println("Total price: $" + totalPrice);
}
}
private double calculateTotalPrice() {
double totalPrice = 0.0;
for (Product product : products) {
totalPrice += product.getPrice();
}
return totalPrice;
}
}
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;
}
}
}