Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 建议设计模式以添加多个费用和项目列表_Design Patterns - Fatal编程技术网

Design patterns 建议设计模式以添加多个费用和项目列表

Design patterns 建议设计模式以添加多个费用和项目列表,design-patterns,Design Patterns,我需要编写应用程序,因为我的计算如下: 基本费用 注册费;注册费%(基本费用)将因财产而异 销售税;(基本费+注册费)%,根据地点的不同而有所不同 服务税;(基本费+注册费+营业税)% 在这里,我需要配置,告诉我们是否包括销售税和服务税。 最后,我需要逐项列出基本费用、注册费、销售税和服务税 interface Payable { public float getAmount(); } abstract class PayableDecorator implements Payable

我需要编写应用程序,因为我的计算如下:

  • 基本费用
  • 注册费;注册费%(基本费用)将因财产而异
  • 销售税;(基本费+注册费)%,根据地点的不同而有所不同
  • 服务税;(基本费+注册费+营业税)%
  • 在这里,我需要配置,告诉我们是否包括销售税和服务税。 最后,我需要逐项列出基本费用、注册费、销售税和服务税

    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);
        }
    }
    
    我必须使用什么设计模式来实现这一点

    我对装饰师和责任链感到困惑。在每件事情上我都要存储相应的费用。最后。需要列表为

    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
    

    我认为应该符合你的要求。

    举个例子。在这里,我假设你必须缴纳销售税的注册费和服务税的销售税

    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);
    

    谢谢,我对装饰师和责任链感到困惑。在每件事情上我都要存储相应的费用。最后。需要列表作为描述基本注册销售服务总计项目1100 25 22 13 160项目280 15 12 8 115 180 40 34 25 275您能解释一下您的困惑吗?我会尽量说得更具体:)