协会成员
采用NULL OBJECT模式设计(消除对null对象的检查,有助于简化代码),在Employee对象包含的一组Affiliation对象中搜索UnionAffiliation对象,然后将ServiceCharge增加到搜寻到的UnionAffiliation对象中。
下面是静态图以及对应的时序图:
静态图:
时序图:
下面给出部分代码片段,依然是从测试代码开始:
测试代码中简单创建一个雇员并向其增加一个UnionAffiliation对象
ServiceChargeTransactionTest
package salary;
import implement.affiliation.UnionAffiliation;
import implement.database.PayrollDatabase;
import implement.entity.Employee;
import implement.entity.ServiceCharge;
import implement.transaction.AddHourlyEmployee;
import implement.transaction.AddServiceChargeTransaction;
import org.junit.Test;
import java.util.Date;
/**
* 会费扣除测试
* Created by ZD on 2017/10/24.
*/
public class ServiceChargeTransactionTest {
PayrollDatabase payrollDatabase = PayrollDatabase.getPayrollDatabase();
@Test
public void testAddServiceChargeTransaction(){
int empId = 7;
String name = "Bob7";
String address = "Bob7.home";
double hourlyPay = 25;
int memberId = 1;
double amount = 12.5;
Date date = new Date();
AddHourlyEmployee addHourlyEmployee = new AddHourlyEmployee(empId,name,address,hourlyPay);
addHourlyEmployee.execute();
Employee e = payrollDatabase.getEmployeeById(empId);
UnionAffiliation af = new UnionAffiliation(memberId,amount);
e.setAffiliation(af);
payrollDatabase.addUnionMember(memberId,e);
AddServiceChargeTransaction serviceChargeTransaction = new AddServiceChargeTransaction(memberId,date,amount);
serviceChargeTransaction.execute();
ServiceCharge serviceCharge = af.getServiceCharge(date);
System.out.println(serviceCharge.getAmount());
}
}
ServiceCharge
package implement.entity;
import java.util.Date;
/**
*会费
* Created by ZD on 2017/10/24.
*/
public class ServiceCharge {
private Date date;
private double amount;
public ServiceCharge(){}
public ServiceCharge(Date date,double amount){
this.amount = amount;
this.date = date;
}
public Date getDate() {
return date;
}
public double getAmount() {
return amount;
}
}
AddServiceChargeTransaction
package implement.transaction;
import implement.affiliation.Affiliation;
import implement.affiliation.UnionAffiliation;
import implement.database.PayrollDatabase;
import implement.entity.Employee;
import implement.entity.ServiceCharge;
import java.util.Date;
/**
* Created by ZD on 2017/10/24.
*/
public class AddServiceChargeTransaction implements Transaction {
private long memberId;
private Date date;
private double amount;
public AddServiceChargeTransaction(){}
public AddServiceChargeTransaction(long memberId, Date date, double amount){
this.memberId = memberId;
this.date = date;
this.amount = amount;
}
public void execute() {
Employee e = PayrollDatabase.getPayrollDatabase().getEmployeeByMemberId(memberId);
ServiceCharge charge = new ServiceCharge(date,amount);
Affiliation affiliation = e.getAffiliation();
if (affiliation instanceof UnionAffiliation){
((UnionAffiliation)affiliation).addServiceCharge(new ServiceCharge(date,amount));
}
}
}