下面对各个用例进行详细分析
增加雇员
增加雇员采用命令模式(适用于数据库事务操作)
增加雇员将雇员的支付薪水时间表和他们的支付薪水类别关联起来
现在采用测试优先的方法去编写代码:由于是增量迭代的过程,所以代码只是部分片段,完整代码后期会上传至github以及csdn上
package salary;
import implement.classification.CommissionedClassification;
import implement.classification.HourlyClassification;
import implement.classification.SalariedClassification;
import implement.database.PayrollDatabase;
import implement.transaction.AddCommissionedEmployee;
import implement.transaction.AddHourlyEmployee;
import implement.entity.Employee;
import implement.method.HoldMethod;
import implement.schedule.MonthlySchedule;
import implement.transaction.AddSalariedEmployee;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* 1.增加雇员测试
* Created by ZD on 2017/10/24.
*/
public class AddEmployTransactionTest {
PayrollDatabase payrollDatabase = PayrollDatabase.getPayrollDatabase();
/*
*增加正常雇员
*/
@Test
public void testaddSalariedEmployee(){
int empId = 1;
String name = "Bob";
String address = "Bob.home";
double monthlyPay = 1000.0;
AddSalariedEmployee addSalariedEmployee = new AddSalariedEmployee(empId,name,address,monthlyPay);
addSalariedEmployee.execute();
Employee e = payrollDatabase.getEmployeeById(empId);
SalariedClassification salariedClassification = (SalariedClassification) e.getPaymentClassification();
MonthlySchedule monthlySchedule = (MonthlySchedule) e.getPaymentSchedule();
System.out.println(monthlySchedule.toString());
HoldMethod holdMethod = (HoldMethod) e.getPaymentMethod();
assertNotNull(e);
}
/*
*增加钟点工
*/
@Test
public void testaddHourlyEmployee(){
long id = 2;
String name = "Bobh";
String address = "Bobh.home";
double hourPay = 150;
AddHourlyEmployee addHourlyEmployee = new AddHourlyEmployee(id,name,address,hourPay);
addHourlyEmployee.execute();
Employee e = payrollDatabase.getEmployeeById(id);
assertEquals(e.getName(),name);
HourlyClassification classification = (HourlyClassification) e.getPaymentClassification();
assertEquals(hourPay,classification.getHourlyPay(),0.01);
}
/*
*增加带薪雇员
*/
@Test
public void testAddCommissionedEmployee(){
long id = 3;
String name = "Bob3";
String address = "Bob3.home";
double monthPay = 200;
double commissionRate = 5;
AddCommissionedEmployee addCommissionedEmployee = new AddCommissionedEmployee(id,name,address,monthPay,commissionRate);
addCommissionedEmployee.execute();
Employee e = payrollDatabase.getEmployeeById(id);
assertEquals(e.getName(),name);
CommissionedClassification commissionedClassification = (CommissionedClassification) e.getPaymentClassification();
}
}
在上述测试用例中可知,有一个名为PayrollDatabase的类,该类用于存放所有Employee对象和所有Member对象,该类采用FACADE模式(为一组复杂而且全面的接口对象提供一个简单且特定的接口)
package implement.transaction;
/**
* 使用command模式定义一个Transaction基类来代表操作
* Created by ZD on 2017/10/24.
*/
public interface Transaction {
public void execute();
}
package implement.database;
import implement.entity.Employee;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* 利用外观模式
* 以empId为键,Employee为值
* 以memberId为键,empId为值
* Created by ZD on 2017/10/24.
*/
public class PayrollDatabase {
private static PayrollDatabase payrollDatabase = new PayrollDatabase();
private ConcurrentHashMap<Long,Employee> employees = new ConcurrentHashMap<Long, Employee>();
public static PayrollDatabase getPayrollDatabase(){
return payrollDatabase;
}
public Employee getEmployeeById(long id){
return employees.get(id);
}
public void addEmployee(int empId,Employee e){
employees.put((long) empId,e);
}
}
使用template模式来增加雇员
下面为增加雇员的动态模型:(这里默认的支付方式是从出纳手里获取支票)
下面给出部分代码:
package implement.transaction;
import implement.classification.PaymentClassification;
import implement.database.PayrollDatabase;
import implement.entity.Employee;
import implement.method.HoldMethod;
import implement.method.PaymentMethod;
import implement.schedule.PaymentSchedule;
/**
*采用模板模式实现
* Created by ZD on 2017/10/24.
*/
public abstract class AddEmployeeTransaction implements Transaction {
private long id;
private String name;
private String address;
public AddEmployeeTransaction(){}
public AddEmployeeTransaction(long empid,String name,String address){
this.id = empid;
this.name = name;
this.address = address;
}
public void execute(){
PaymentClassification pc = getClassification();
PaymentSchedule ps = getSchdule();
PaymentMethod pm = getMethod();
Employee e = new Employee(id,name,address);
e.setClassification(pc);
e.setSchedule(ps);
e.setMethod(pm);
PayrollDatabase.getPayrollDatabase().addEmployee((int) id,e);
}
protected PaymentMethod getMethod(){
return new HoldMethod();
}
protected abstract PaymentSchedule getSchdule();
protected abstract PaymentClassification getClassification();
}
下面展示其中AddHourlyEmployee代码:
package implement.transaction;
import implement.classification.HourlyClassification;
import implement.classification.PaymentClassification;
import implement.schedule.PaymentSchedule;
import implement.schedule.WeeklySchedule;
/**
* Created by ZD on 2017/10/24.
*/
public class AddHourlyEmployee extends AddEmployeeTransaction {
private double hourlyPay;
public AddHourlyEmployee(long id,String name,String address,double hourlyPay){
super(id,name,address);
this.hourlyPay = hourlyPay;
}
protected PaymentSchedule getSchdule() {
return new WeeklySchedule();
}
protected PaymentClassification getClassification() {
return new HourlyClassification(hourlyPay);
}
}