我需要编写应用程序,因为我有这样的计算:
在此,我需要配置告诉我们是否包含销售税和服务税。 最后,我需要明确项目作为基本费用,注册费,销售税和服务税。
我必须使用什么设计模式来实现这一目标?
我对装饰师和责任链感到困惑。在每件事情上,我必须分别存储费用。在末尾。需要列表为
Desc Basic Reg Sales Service Total
------------------------------------------
Item 1 100 25 22 13 160
Item 2 80 15 12 8 115
------------------------------------------
Total 180 40 34 25 275
答案 0 :(得分:1)
我相信decorator pattern应符合您的要求。
答案 1 :(得分:0)
一些例子。在这里,我假设您必须缴纳销售税和服务税的销售税的注册费。
interface Payable {
public float getAmount();
}
abstract class PayableDecorator implements Payable {
private Payable base;
public PayableDecorator(Payable base) {
this.base = base;
}
public Payable getBase() {
return base;
}
}
class Fee implements Payable {
private float value;
public Fee(float value) {
this.value = value;
}
public float getAmount() {
return value;
}
}
class RegistrationFee extends PayableDecorator {
private float registrationPercentage;
public RegistrationFee(Payable fee, float pct) {
super(fee);
registrationPercentage = pct;
}
public float getRegistrationPercentage() {
return registrationPercentage();
}
public float getAmount() {
return getBase() * (1 + registrationPercentage);
}
}
class SaleTax extends PayableDecorator {
private float salePercentage;
public SaleTax(RegistrationFee registration, float pct) {
super(registration);
salePercentabe = pct;
}
public float getAmount() {
return getBase() * (1 + salePercentage);
}
}
class SericeTax extends PayableDecorator {
private float servicePercentage;
public SaleTax(SaleTax registration, float pct) {
super(registration);
salePercentabe = pct;
}
public float getAmount() {
return getBase() * (1 + servicePercentage);
}
}
使用:
Payable totalTax = new ServiceTax(new SaleTax(new RegistrationFee(new Fee(100), .1), .03), .01);