策略模式 用其它方法改进
2010-12-20 19:44
393 查看
某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,
属性:员工的姓名和生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,
如果该月员工过生日,则公司会额外奖励100元。
SalariedEmployee:Employee的子类,拿固定工资的员工。
属性:月薪
HourlyEmployee:Employee的子类,按小时拿工资的员工,
每月工作超出160小时的部分按照1.5倍工资发放
属性:每小时的工资、每月工作的小时数
SalesEmployee:Employee的子类,销售人员,工资由月销
售额和提成率决定
属性:月销售额、提成率
1-10万之间,提成率 2%,10-50万,提成率 1.5%;
50-100万之间,提成率 1.2%
BasePlusSalesEmployee:SalesEmployee的子类,有固定底
薪的销售人员,工资由底薪加上销售提成部分
属性:底薪。
了解:Java 设计模式 “策略模式”
/**
* 所有员工的父类
*
*/
public class Employee {
private String name;
private Month month; // 枚举类型代表生日月份
private CaculSalary cs; // 定义算法族对象
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public void setCs(CaculSalary cs) {
this.cs = cs;
}
public double getSalary(Month birMonth) {
return cs.caculEmployeeSalary(this, this.month)
+ this.getBirthdaySaler(birMonth);
}
public double getBirthdaySaler(Month birthdayMonth) {
double birthdaySaler = 0.0;
if (this.month == birthdayMonth) {
birthdaySaler += 100.0;
}
return birthdaySaler;
}
}
public class BasePlusSalesEmployee extends Employee {
private double basicSalary;
private double commision;
private double totalMoney;
public double getBasicSalary() {
return basicSalary;
}
public void setBasicSalary(double basicSalary) {
this.basicSalary = basicSalary;
}
public double getCommision() {
return commision;
}
public void setCommision(double commision) {
this.commision = commision;
}
public double getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(double totalMoney) {
this.totalMoney = totalMoney;
}
}
public class HourlyEmployee extends Employee {
private int hours;
private double hourSalary;
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public double getHourSalary() {
return hourSalary;
}
public void setHourSalary(double hourSalary) {
this.hourSalary = hourSalary;
}
}
public class SalesEmployee extends Employee {
private double totalMoney;
private double commision1;
private double commision2;
private double commision3;
private double commision4;
public double getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(double totalMoney) {
this.totalMoney = totalMoney;
}
public double getCommision1() {
return commision1;
}
public void setCommision1(double commision1) {
this.commision1 = commision1;
}
public double getCommision2() {
return commision2;
}
public void setCommision2(double commision2) {
this.commision2 = commision2;
}
public double getCommision3() {
return commision3;
}
public void setCommision3(double commision3) {
this.commision3 = commision3;
}
public double getCommision4() {
return commision4;
}
public void setCommision4(double commision4) {
this.commision4 = commision4;
}
}
public class SalariedEmployee extends Employee{
private double basePay;
public double getBasePay() {
return basePay;
}
public void setBasePay(double basePay) {
this.basePay = basePay;
}
}
public interface CaculSalary {
/**
* 根据员工类型,当前月份计算该员工工资
*
* @param employee
* @param month
* @return
*/
public double caculEmployeeSalary(Employee employee, Month month);
}
public class CaculBasePlusSalesEmployee implements CaculSalary {
@Override
public double caculEmployeeSalary(Employee employee, Month month) {
double salary = 0.0;
//参数非空检查
if(employee == null || month == null){return salary;}
//参数类型检查
if(employee instanceof BasePlusSalesEmployee){
BasePlusSalesEmployee bpsemp = (BasePlusSalesEmployee)employee;
salary = bpsemp.getBasicSalary() + bpsemp.getTotalMoney() * bpsemp.getCommision();
}
return salary;
}
}
public class CaculHourlyEmployee implements CaculSalary {
public double caculEmployeeSalary(Employee employee, Month month) {
double salary = 0.0;
// 参数非空检查
if (employee == null || month == null) {
return salary;
}
// 参数类型检查
if (employee instanceof HourlyEmployee) {
HourlyEmployee hemp = (HourlyEmployee) employee;
// 判断工作时间
int time = hemp.getHours();
if (time > 160) {
int temp = time - 160;
salary = 160 * hemp.getHourSalary() + temp
* (hemp.getHourSalary() * 1.5);
} else if (time <= 160) {
salary = time * hemp.getHourSalary();
}
}
return salary;
}
}
public class CaculSalariedEmployee implements CaculSalary {
public double caculEmployeeSalary(Employee employee, Month month) {
double salary = 0.0;
// 参数非空检查
if (employee == null || month == null) {
return salary;
}
// 参数类型检查
if (employee instanceof SalariedEmployee) {
SalariedEmployee Sdemp = (SalariedEmployee) employee;
salary = Sdemp.getBasePay();
}
return salary;
}
}
public class CaculSalesEmployee implements CaculSalary {
public double caculEmployeeSalary(Employee employee, Month month) {
double salary = 0.0;
if (employee == null || month == null) {
return salary;
}
if (employee instanceof SalesEmployee) {
SalesEmployee semp = (SalesEmployee) employee;
double total = semp.getTotalMoney();
if (total < 100000.0)
salary = total * semp.getCommision1();
else if (total >= 100000.0 && total < 500000)
salary = 100000 * semp.getCommision1() + (total - 100000)
* semp.getCommision2();
else if (total >= 500000.0 && total < 1000000)
salary = 100000 * semp.getCommision1() + (500000 - 100000)
* semp.getCommision2() + (total - 500000)
* semp.getCommision3();
else
salary = 100000 * semp.getCommision1() + (500000 - 100000)
* semp.getCommision2() + (1000000 - 500000)
* semp.getCommision3() + (total - 1000000)
* semp.getCommision4();
}
return salary;
}
}
/**
* 自定义枚举类型,描述 12 个月份 月份 英文名称 1月 January 2月 February 3月 March 4月 April 5月 May 6月
* June 7月 July 8月 Aguest 9月 September 10月 October 11月 November 12月 December
* 调用,类似于常量
*/
public enum Month {
January, February, March, April, May, June, July, Aguest, September, October, November, December
}
public class TestEmployee {
/**
* @param args
*/
public static void main(String[] args) {
// 新建员工对象
HourlyEmployee emp = new HourlyEmployee();
emp.setName("li");
emp.setMonth(Month.April);
emp.setHours(180);
emp.setHourSalary(50);
// 定义一个算法族对象
CaculSalary cs = new CaculHourlyEmployee();
emp.setCs(cs);
// 计算该员工工资
double salary = emp.getSalary(Month.July);
System.out.println("该员工 " + emp.getName() + " " + "July" + " 月工资 :"
+ salary);
SalariedEmployee emp2 = new SalariedEmployee();
emp2.setName("wang");
emp2.setBasePay(2500);
emp2.setMonth(Month.July);
CaculSalary cs2 = new CaculSalariedEmployee();
emp2.setCs(cs2);
double salary2 = emp2.getSalary(Month.July);
System.out.println("该员工 " + emp2.getName() + " " + "July" + " 月工资 :"
+ salary2);
BasePlusSalesEmployee emp3 = new BasePlusSalesEmployee();
emp3.setName("you");
emp3.setBasicSalary(1500);
emp3.setMonth(Month.January);
CaculSalary cs3 = new CaculBasePlusSalesEmployee();
emp3.setCs(cs3);
emp3.setCommision(0.1);
emp3.setTotalMoney(50000);
double salary3 = emp3.getSalary(Month.July);
System.out.println("该员工 " + emp3.getName() + " " + "July" + " 月工资 :"
+ salary3);
SalesEmployee emp4 = new SalesEmployee();
emp4.setName("yin");
emp4.setMonth(Month.July);
CaculSalary cs4 = new CaculSalesEmployee();
emp4.setCs(cs4);
emp4.setTotalMoney(510000);
emp4.setCommision1(0.05);
emp4.setCommision2(0.1);
emp4.setCommision3(0.15);
emp4.setCommision4(0.2);
double salary4 = emp4.getSalary(Month.July);
System.out.println("该员工 " + emp4.getName() + " " + "July" + " 月工资 :"
+ salary4);
}
}
Employee:这是所有员工总的父类,
属性:员工的姓名和生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,
如果该月员工过生日,则公司会额外奖励100元。
SalariedEmployee:Employee的子类,拿固定工资的员工。
属性:月薪
HourlyEmployee:Employee的子类,按小时拿工资的员工,
每月工作超出160小时的部分按照1.5倍工资发放
属性:每小时的工资、每月工作的小时数
SalesEmployee:Employee的子类,销售人员,工资由月销
售额和提成率决定
属性:月销售额、提成率
1-10万之间,提成率 2%,10-50万,提成率 1.5%;
50-100万之间,提成率 1.2%
BasePlusSalesEmployee:SalesEmployee的子类,有固定底
薪的销售人员,工资由底薪加上销售提成部分
属性:底薪。
了解:Java 设计模式 “策略模式”
/**
* 所有员工的父类
*
*/
public class Employee {
private String name;
private Month month; // 枚举类型代表生日月份
private CaculSalary cs; // 定义算法族对象
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public void setCs(CaculSalary cs) {
this.cs = cs;
}
public double getSalary(Month birMonth) {
return cs.caculEmployeeSalary(this, this.month)
+ this.getBirthdaySaler(birMonth);
}
public double getBirthdaySaler(Month birthdayMonth) {
double birthdaySaler = 0.0;
if (this.month == birthdayMonth) {
birthdaySaler += 100.0;
}
return birthdaySaler;
}
}
public class BasePlusSalesEmployee extends Employee {
private double basicSalary;
private double commision;
private double totalMoney;
public double getBasicSalary() {
return basicSalary;
}
public void setBasicSalary(double basicSalary) {
this.basicSalary = basicSalary;
}
public double getCommision() {
return commision;
}
public void setCommision(double commision) {
this.commision = commision;
}
public double getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(double totalMoney) {
this.totalMoney = totalMoney;
}
}
public class HourlyEmployee extends Employee {
private int hours;
private double hourSalary;
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public double getHourSalary() {
return hourSalary;
}
public void setHourSalary(double hourSalary) {
this.hourSalary = hourSalary;
}
}
public class SalesEmployee extends Employee {
private double totalMoney;
private double commision1;
private double commision2;
private double commision3;
private double commision4;
public double getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(double totalMoney) {
this.totalMoney = totalMoney;
}
public double getCommision1() {
return commision1;
}
public void setCommision1(double commision1) {
this.commision1 = commision1;
}
public double getCommision2() {
return commision2;
}
public void setCommision2(double commision2) {
this.commision2 = commision2;
}
public double getCommision3() {
return commision3;
}
public void setCommision3(double commision3) {
this.commision3 = commision3;
}
public double getCommision4() {
return commision4;
}
public void setCommision4(double commision4) {
this.commision4 = commision4;
}
}
public class SalariedEmployee extends Employee{
private double basePay;
public double getBasePay() {
return basePay;
}
public void setBasePay(double basePay) {
this.basePay = basePay;
}
}
public interface CaculSalary {
/**
* 根据员工类型,当前月份计算该员工工资
*
* @param employee
* @param month
* @return
*/
public double caculEmployeeSalary(Employee employee, Month month);
}
public class CaculBasePlusSalesEmployee implements CaculSalary {
@Override
public double caculEmployeeSalary(Employee employee, Month month) {
double salary = 0.0;
//参数非空检查
if(employee == null || month == null){return salary;}
//参数类型检查
if(employee instanceof BasePlusSalesEmployee){
BasePlusSalesEmployee bpsemp = (BasePlusSalesEmployee)employee;
salary = bpsemp.getBasicSalary() + bpsemp.getTotalMoney() * bpsemp.getCommision();
}
return salary;
}
}
public class CaculHourlyEmployee implements CaculSalary {
public double caculEmployeeSalary(Employee employee, Month month) {
double salary = 0.0;
// 参数非空检查
if (employee == null || month == null) {
return salary;
}
// 参数类型检查
if (employee instanceof HourlyEmployee) {
HourlyEmployee hemp = (HourlyEmployee) employee;
// 判断工作时间
int time = hemp.getHours();
if (time > 160) {
int temp = time - 160;
salary = 160 * hemp.getHourSalary() + temp
* (hemp.getHourSalary() * 1.5);
} else if (time <= 160) {
salary = time * hemp.getHourSalary();
}
}
return salary;
}
}
public class CaculSalariedEmployee implements CaculSalary {
public double caculEmployeeSalary(Employee employee, Month month) {
double salary = 0.0;
// 参数非空检查
if (employee == null || month == null) {
return salary;
}
// 参数类型检查
if (employee instanceof SalariedEmployee) {
SalariedEmployee Sdemp = (SalariedEmployee) employee;
salary = Sdemp.getBasePay();
}
return salary;
}
}
public class CaculSalesEmployee implements CaculSalary {
public double caculEmployeeSalary(Employee employee, Month month) {
double salary = 0.0;
if (employee == null || month == null) {
return salary;
}
if (employee instanceof SalesEmployee) {
SalesEmployee semp = (SalesEmployee) employee;
double total = semp.getTotalMoney();
if (total < 100000.0)
salary = total * semp.getCommision1();
else if (total >= 100000.0 && total < 500000)
salary = 100000 * semp.getCommision1() + (total - 100000)
* semp.getCommision2();
else if (total >= 500000.0 && total < 1000000)
salary = 100000 * semp.getCommision1() + (500000 - 100000)
* semp.getCommision2() + (total - 500000)
* semp.getCommision3();
else
salary = 100000 * semp.getCommision1() + (500000 - 100000)
* semp.getCommision2() + (1000000 - 500000)
* semp.getCommision3() + (total - 1000000)
* semp.getCommision4();
}
return salary;
}
}
/**
* 自定义枚举类型,描述 12 个月份 月份 英文名称 1月 January 2月 February 3月 March 4月 April 5月 May 6月
* June 7月 July 8月 Aguest 9月 September 10月 October 11月 November 12月 December
* 调用,类似于常量
*/
public enum Month {
January, February, March, April, May, June, July, Aguest, September, October, November, December
}
public class TestEmployee {
/**
* @param args
*/
public static void main(String[] args) {
// 新建员工对象
HourlyEmployee emp = new HourlyEmployee();
emp.setName("li");
emp.setMonth(Month.April);
emp.setHours(180);
emp.setHourSalary(50);
// 定义一个算法族对象
CaculSalary cs = new CaculHourlyEmployee();
emp.setCs(cs);
// 计算该员工工资
double salary = emp.getSalary(Month.July);
System.out.println("该员工 " + emp.getName() + " " + "July" + " 月工资 :"
+ salary);
SalariedEmployee emp2 = new SalariedEmployee();
emp2.setName("wang");
emp2.setBasePay(2500);
emp2.setMonth(Month.July);
CaculSalary cs2 = new CaculSalariedEmployee();
emp2.setCs(cs2);
double salary2 = emp2.getSalary(Month.July);
System.out.println("该员工 " + emp2.getName() + " " + "July" + " 月工资 :"
+ salary2);
BasePlusSalesEmployee emp3 = new BasePlusSalesEmployee();
emp3.setName("you");
emp3.setBasicSalary(1500);
emp3.setMonth(Month.January);
CaculSalary cs3 = new CaculBasePlusSalesEmployee();
emp3.setCs(cs3);
emp3.setCommision(0.1);
emp3.setTotalMoney(50000);
double salary3 = emp3.getSalary(Month.July);
System.out.println("该员工 " + emp3.getName() + " " + "July" + " 月工资 :"
+ salary3);
SalesEmployee emp4 = new SalesEmployee();
emp4.setName("yin");
emp4.setMonth(Month.July);
CaculSalary cs4 = new CaculSalesEmployee();
emp4.setCs(cs4);
emp4.setTotalMoney(510000);
emp4.setCommision1(0.05);
emp4.setCommision2(0.1);
emp4.setCommision3(0.15);
emp4.setCommision4(0.2);
double salary4 = emp4.getSalary(Month.July);
System.out.println("该员工 " + emp4.getName() + " " + "July" + " 月工资 :"
+ salary4);
}
}
相关文章推荐
- 损失函数改进方法之A-softmax loss
- Nodejs安装方法及npm安装其它组件问题:npm err
- C++, Qt Windows关机的时候,阻止其关机或这注销 做其它业务的解决方法
- c#实现魔兽(warIII)中显血和改键功能 (附源码)(Hook中start方法已改进)
- 如何 阻止员工 访问 找工作网站 变态的方法 也适合禁用其它网站
- 统计文件种类数+获取子shell返回值的其它方法
- VC2010 中 MFC 的改进导致 XTP 停靠窗体无法显示的解决方法
- jquery getScript动态加载JS方法改进详解
- VB6.0中,在一个TextBox里面只输入数字,不能输入其它,比如汉字,符号,英文的方法
- 链表(篇6)Floyd破圈法以及其改进方法检查删除链表中的环
- c# 扩展方法奇思妙用高级篇一:改进 Scottgu 的 "In" 扩展
- 谈谈预览文档和其它多种格式文件的实现方法
- 斐波那契数列的时间复杂度详释与改进方法
- 改进 XSLT 编码的五种方法
- 简谈 GET和POST以及其它请求方法
- 改进网页可读性的7个方法
- 一种基于RBAC模型的动态访问控制改进方法
- JDBC连接数据库(适当的封装查询、新增修改删除)的方法改进
- 关于获得MFC窗口其它类指针的方法
- Js文件中调用其它Js函数的方法