- 154
- 0
- 约1.21万字
- 约 16页
- 2017-01-29 发布于重庆
- 举报
设计模式大题
5.2.某电影院售票系统为不同类型的用户提供了不同的电影票(CinemaTicket)打折方式(Discount),学生凭学生证可享受8折优惠(StudentDiscount),儿童可享受减免10元的优惠(ChildrenDiscount), VIP用户除享受半价优惠外还可以进行积分(VIPDiscount)。选择一种合适的设计模式来设计该系统。(策略模式)
//电影票类:环境类
class MovieTicket
{
private double price;
private Discount discount;
public void setPrice(double price)
{
this.price = price;
}
public void setDiscount(Discount discount)
{
this.discount = discount;
}
public double getPrice()
{
return discount.calculate(this.price);
}
}
//折扣类:抽象策略类
interface Discount
{
public double calculate(double price);
}
//学生折扣类:具体策略类
class StudentDiscount imp
原创力文档

文档评论(0)