作为一项任务,我必须为一家商店制定一个股票系统。每种产品都有两个价格变量,一个用于普通客户,另一个用于重要客户。我无法决定一个简单的设计问题。到目前为止,我的课程如下所示:(设计)商店的库存经理:应该在哪里包含getPrice()方法?
class Customer {
...
public Boolean isImportant() { return important; }
}
class Article {
...
float importantCustomerPrice;
float normalCustomerPrice;
}
因此,现在我想为特定客户获得文章的价格。我应该:
1)将用getPrice()方法中的物品,例如:
class Article {
float getPrice(Customer customer) {
if (customer.isImportant() == true) {
return importantCustomerPrice;
} else return normalCustomerPrice;
}
}
2)使客户这样的两个子类为
class ImportantCustomer extends Customer {
float getPrice (Article article) { return article.getImportantCustomerPrice(); }
}
class NormalCustomer extends Customer {
float getPrice (Article article) { return article.getNormalCustomerPrice(); }
}
3)包含一个类管理它这两个类:
class Seller {
float getPrice(Customer customer, Article article) {
if (customer.isImportant() == true) {
return article.getImportantCustomerPrice();
} else return article.getNormalCustomerPrice();
}
到目前为止,我认为,最安全和最灵活的选择是第二个,但它听起来我错了,客户端类的东西决定unre如文章的价格。此外,我的团队合作伙伴不喜欢为这样简单的事情添加更多班级的想法
您推荐哪一个?有更好的选择吗?
谢谢
如果是作业,则应添加“家庭作业”标签。 – Uffe