public static void main(String[] args){//準(zhǔn)備測試數(shù)據(jù)ExportHeaderModel ehm = new ExportHeaderModel();ehm.setDepId("一分公司");ehm.setExportDate("2010-05-18");Map<String,Collection<ExportDataModel>> mapData = new HashMap<String,Collection<ExportDataModel>>();Collection<ExportDataModel> col = new ArrayList<ExportDataModel>();ExportDataModel edm1 = new ExportDataModel();edm1.setProductId("產(chǎn)品001號");edm1.setPrice(100);edm1.setAmount(80);ExportDataModel edm2 = new ExportDataModel();edm2.setProductId("產(chǎn)品002號");edm2.setPrice(99);edm2.setAmount(55); //把數(shù)據(jù)組裝起來col.add(edm1);col.add(edm2); mapData.put("銷售記錄表", col);ExportFooterModel efm = new ExportFooterModel();efm.setExportUser("張三");//測試輸出到文本文件ExportToTxt toTxt = new ExportToTxt();toTxt.export(ehm, mapData, efm);//測試輸出到xml文件ExportToXml toXml = new ExportToXml();toXml.export(ehm, mapData, efm);}
public class ConcreteBuilder{//構(gòu)建真正的對象并返回 public InsuranceContract build(){if(contractId==null || contractId.trim().length()==0){throw new IllegalArgumentException("合同編號不能為空");}boolean signPerson = personName!=null && personName.trim().length()>0;boolean signCompany = companyName!=null && companyName.trim().length()>0;if(signPerson && signCompany){throw new IllegalArgumentException("一份保險合同不能同時與人和公司簽訂");}if(signPerson==false && signCompany==false){throw new IllegalArgumentException("一份保險合同不能沒有簽訂對象");}if(beginDate<=0){throw new IllegalArgumentException("合同必須有保險開始生效的日期");}if(endDate<=0){throw new IllegalArgumentException("合同必須有保險失效的日期");}if(endDate<=beginDate){throw new IllegalArgumentException("保險失效的日期必須大于保險生效日期");} return new InsuranceContract(this);}public static void main(String[] args){//創(chuàng)建構(gòu)建器ConcreteBuilder builder = new ConcreteBuilder("001",12345L,67890L);//設(shè)置需要的數(shù)據(jù),然后構(gòu)建保險合同對象InsuranceContract contract = builder.setPersonName("張三").setOtherData("test").build();//操作保險合同對象的方法contract.someOperation();}
public class InsuranceContract{...public static class ConcreteBuilder{..}}public static void main(String[] args){//創(chuàng)建構(gòu)建器InsuranceContract.ConcreteBuilder builder = new InsuranceContract.ConcreteBuilder("001",12345L,67890L);//設(shè)置需要的數(shù)據(jù),然后構(gòu)建保險合同對象InsuranceContract contract = builder.setPersonName("張三").setOtherData("test").build();//操作保險合同對象的方法contract.someOperation();}