策略枚举

public enum PayrollDay {
    MONDAY(PayType.WEEKDAY),
    TUESDAY(PayType.WEEKDAY),
    WEDNESDAY(PayType.WEEKDAY),
    THURSDAY(PayType.WEEKDAY),
    FRIDAY(PayType.WEEKDAY),
    SATURDAY(PayType.WEEKEND),
    SUNDAY(PayType.WEEKEND);

    private PayType payType;

    PayrollDay(PayType payType)
    {
        this.payType = payType;
    }

    double pay(double hoursWorked/*工作时间*/,double payRate/*每小时工钱,加班时间减半*/)
    {
        return payType.Pay(hoursWorked, payRate);
    }

    private enum PayType{
        WEEKDAY{
            double overtimePay(double hours,double payRate)
            {
                return hours <= HOURS_PER_SHIFT ? 0 : payRate*(HOURS_PER_SHIFT-hours)/2;
            }
        },
        WEEKEND{
            double overtimePay(double hours,double payRate)
            {
                return payRate*(HOURS_PER_SHIFT-hours)/2;
            }
        };

        private static final int HOURS_PER_SHIFT = 8;

        abstract double overtimePay(double hours,double payRate);

        double Pay(double hoursWorked,double payRate)
        {
            double basePay = HOURS_PER_SHIFT * payRate;
            return basePay + overtimePay(hoursWorked,payRate);
        }
    }
}

对于枚举PayrollDay 的每个实例,必须指派一个策略枚举PayType的一个实例,计算工钱的过程封装在了策略枚举中。

你可能感兴趣的:(策略枚举)