java 策略枚举


public enum PayrollDay {
	MONDAY,	TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY;
	private static final int HOURS_PER_SHIFT=8;
	
	double pay(double hoursWorked,double payRate){
		double basePay = hoursWorked * payRate;
		
		double overtimePay;
		
		switch (this) {
		case SATURDAY: case SUNDAY:
			overtimePay =hoursWorked* payRate/2;
		default:
			overtimePay = hoursWorked <=HOURS_PER_SHIFT? 0 :(hoursWorked - HOURS_PER_SHIFT)*payRate/2;
			break;
		}
		return basePay+ overtimePay;
	}
}


enum PayrollDay2 {
	MONDAY(PayType.WEEKDAY),
	TUESDAY(PayType.WEEKDAY),
	WEDNESDAY(PayType.WEEKDAY),
	THURSDAY(PayType.WEEKDAY),
	FRIDAY(PayType.WEEKDAY),
	SATURDAY(PayType.WEEKEND),
	SUNDAY(PayType.WEEKEND);
	
	private final PayType payType;

	private PayrollDay2(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:(hours-HOURS_PER_SHIFT)*payRate/2;
			}
		},
		WEEKEND{
			double overtimePay(double hours,double payRate){
				return hours*payRate/2;
			}
		};
		
		private static final int HOURS_PER_SHIFT=8;
		
		abstract double overtimePay(double hrs,double payRate);
		
		double pay(double hoursWorked,double payRate){
			double basePay=hoursWorked*payRate;
			return basePay+overtimePay(hoursWorked, payRate);
		}
	}
}


 
 

你可能感兴趣的:(java,设计模式)