【发布时间】:2017-10-16 15:04:20
【问题描述】:
闲散的代码解释了松散耦合的概念。我想实现添加项目的主要方法(价格和数量)并计算总价和销售税。我如何实现主要方法。
class ShopingCartEntry {
public float price;
public int quantity;
public float getTotalPrice() {
return price * quantity;
}
}
class ShopingCartContents {
public ShopingCartEntry[] items;
public float getTotalPrice() {
float totalPrice = 0;
for (ShopingCartEntry item : items) {
totalPrice += item.getTotalPrice();
}
return totalPrice;
}
}
class Order {
private ShopingCartContents cart;
private float salesTax;
public Order(ShopingCartContents cart, float salesTax) {
this.cart = cart;
this.salesTax = salesTax;
}
public float orderTotalPrice() {
return cart.getTotalPrice() * (1.0f + salesTax);
}
}
public class LooseCoupling {
public static void main(String[] args) {
}
}
【问题讨论】:
-
我想实现添加项目的主要方法(价格和数量)并计算总价和销售税这不是要求
-
您可能想问松耦合概念在这个业务场景和数据模型中是如何工作的。尝试了解这里的数据建模。
-
fo放纵,你的意思肯定是跟随,而不是放纵。
标签: java loose-coupling