public abstract class AutoMessage { //收件人地址 private String to; //发件人地址 private String from; //标题 private String subject; //内容 private String body; //发送日期 private Date sendDate; public void send(){ System.out.println("收件人地址:" + to); System.out.println("发件人地址:" + from); System.out.println("标题:" + subject); System.out.println("内容:" + body); System.out.println("发送日期:" + sendDate); } }
public class WelcomeMessage extends AutoMessage { /** * 构造子 */ public WelcomeMessage(){ System.out.println("发送欢迎信息"); } }
public class GoodbyeMessage extends AutoMessage{ /** * 构造子 */ public GoodbyeMessage(){ System.out.println("发送欢送信息"); } }
public abstract class Builder { protected AutoMessage msg; //标题零件的建造方法 public abstract void buildSubject(); //内容零件的建造方法 public abstract void buildBody(); //收件人零件的建造方法 public void buildTo(String to){ msg.setTo(to); } //发件人零件的建造方法 public void buildFrom(String from){ msg.setFrom(from); } //发送时间零件的建造方法 public void buildSendDate(){ msg.setSendDate(new Date()); } /** * 邮件产品完成后,用此方法发送邮件 * 此方法相当于产品返还方法 */ public void sendMessage(){ msg.send(); } }
public class WelcomeBuilder extends Builder { public WelcomeBuilder(){ msg = new WelcomeMessage(); } @Override public void buildBody() { // TODO Auto-generated method stub msg.setBody("欢迎内容"); } @Override public void buildSubject() { // TODO Auto-generated method stub msg.setSubject("欢迎标题"); } }
public class GoodbyeBuilder extends Builder { public GoodbyeBuilder(){ msg = new GoodbyeMessage(); } @Override public void buildBody() { // TODO Auto-generated method stub msg.setBody("欢送内容"); } @Override public void buildSubject() { // TODO Auto-generated method stub msg.setSubject("欢送标题"); } }
导演者Director,这个类提供一个construct()方法,此方法调用建造者的建造方法,包括buildTo()、buildFrom()、buildSubject()、buildBody()、buildSendDate()等,从而一部分一部分地建造出产品对象,既AutoMessage对象。
public class Director { Builder builder; /** * 构造子 */ public Director(Builder builder){ this.builder = builder; } /** * 产品构造方法,负责调用各零件的建造方法 */ public void construct(String toAddress , String fromAddress){ this.builder.buildTo(toAddress); this.builder.buildFrom(fromAddress); this.builder.buildSubject(); this.builder.buildBody(); this.builder.buildSendDate(); this.builder.sendMessage(); } }
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub Builder builder = new WelcomeBuilder(); Director director = new Director(builder); director.construct("toAddress@126.com", "fromAddress@126.com"); } }
/** * 保险合同对象 */ public class InsuranceContract { //保险合同编号 private String contractId; /** * 被保险人员的名称,同一份保险合同,要么跟人员签订,要么跟公司签订 * 也就是说,“被保险人员”和“被保险公司”这两个属性,不可能同时有值 */ private String personName; //被保险公司的名称 private String companyName; //保险开始生效日期 private long beginDate; //保险失效日期,一定会大于保险开始生效日期 private long endDate; //其他数据 private String otherData; //私有构造方法 private InsuranceContract(ConcreteBuilder builder){ this.contractId = builder.contractId; this.personName = builder.personName; this.companyName = builder.companyName; this.beginDate = builder.beginDate; this.endDate = builder.endDate; this.otherData = builder.otherData; } /** * 保险合同的一些操作 */ public void someOperation(){ System.out.println("当前正在操作的保险合同编号为【"+this.contractId+"】"); } public static class ConcreteBuilder{ private String contractId; private String personName; private String companyName; private long beginDate; private long endDate; private String otherData; /** * 构造方法,传入必须要有的参数 * @param contractId 保险合同编号 * @param beginDate 保险合同开始生效日期 * @param endDate 保险合同失效日期 */ public ConcreteBuilder(String contractId,long beginDate,long endDate){ this.contractId = contractId; this.beginDate = beginDate; this.endDate = endDate; } //被保险人员的名称 public ConcreteBuilder setPersonName(String personName) { this.personName = personName; return this; } //被保险公司的名称 public ConcreteBuilder setCompanyName(String companyName) { this.companyName = companyName; return this; } //其他数据 public ConcreteBuilder setOtherData(String otherData) { this.otherData = otherData; return this; } /** * 构建真正的对象并返回 * @return 构建的保险合同对象 */ 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 class Client { public static void main(String[]args){ //创建构建器对象 InsuranceContract.ConcreteBuilder builder = new InsuranceContract.ConcreteBuilder("9527", 123L, 456L); //设置需要的数据,然后构建保险合同对象 InsuranceContract contract = builder.setPersonName("小明").setOtherData("test").build(); //操作保险合同对象的方法 contract.someOperation(); } }
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java 23种设计模式(三、建造者模式) - Python技术站