生成器模式

阅读《研磨设计模式》笔记。之后若有所感,再补充。
生成器模式目的:将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
示例:
/**
 * @description 描述输出到文件头的对象
 * @author liuwei
 * @date 2014-4-17
 * @version 1.0
 */
public class ExportHeaderModel {
	/**分公司*/
	private String depId;
	/**导出数据的日期*/
	private String exportDate;
	
	public String getDepId() {
		return depId;
	}
	public void setDepId(String depId) {
		this.depId = depId;
	}
	public String getExportDate() {
		return exportDate;
	}
	public void setExportDate(String exportDate) {
		this.exportDate = exportDate;
	}
}

/**
 * @description 描述输出数据的对象
 * @author liuwei
 * @date 2014-4-17
 * @version 1.0
 */
public class ExportDataModel {
	/**产品编号*/
	private String productId;
	/**销售价格*/
	private String price;
	/**销售数量*/
	private String amount;
	
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public String getAmount() {
		return amount;
	}
	public void setAmount(String amount) {
		this.amount = amount;
	}
}

/**
 * @description描述输出到文件尾的内容的对象
 * @author liuwei
 * @date 2014-4-17
 * @version 1.0
 */
public class ExportFooterModel {
	/**输出人*/
	private String exportUser;

	public String getExportUser() {
		return exportUser;
	}
	public void setExportUser(String exportUser) {
		this.exportUser = exportUser;
	}
}

/**
 * @description 生成器接口,定义创建一个输出文件对象所需的各个部件的操作
 * @author liuwei
 * @date 2014-4-17
 * @version 1.0
 */
public interface Builder {
	/**
	 * 构建输出文件的header部分
	 * @param ehm 文件头的内容
	 */
	public void buildHeader(ExportHeaderModel ehm);
	/**
	 * 构建输出文件的body部分
	 * @param mapData 要输出的数据部分
	 */
	public void buildBody(Map<String, Collection<ExportDataModel>> mapData);
	/**
	 * 构建输出文件的footer部分
	 * @param efm 文件尾的内容
	 */
	public void buildFooter(ExportFooterModel efm);

}

/**
 * @description 实现导出数据到文本文件的生成器对象
 * @author liuwei
 * @date 2014-4-17
 * @version 1.0
 */
public class TxtBuilder implements Builder{
	/**
	 * 用来记录构建的文件内容,相当于产品
	 */
	private StringBuffer buffer = new StringBuffer();
	/* (non-Javadoc)
	 * @see builder.Builder#buildHeader(builder.ExportHeaderModel)
	 */
	@Override
	public void buildHeader(ExportHeaderModel ehm) {
		buffer.append(ehm.getDepId() + "," + ehm.getExportDate() + "\n");
	}
	/* (non-Javadoc)
	 * @see builder.Builder#buildBody(java.util.Map)
	 */
	@Override
	public void buildBody(Map<String, Collection<ExportDataModel>> mapData) {
		for(String tblName : mapData.keySet()){
			//先拼表名
			buffer.append(tblName + "\n");
			//然后循环拼接具体数据
			for(ExportDataModel edm : mapData.get(tblName)){
				buffer.append(edm.getProductId() + "," + edm.getPrice() + "," + edm.getAmount() + "\n");
			}
		}
	}
	/* (non-Javadoc)
	 * @see builder.Builder#buildFooter(builder.ExportFooterModel)
	 */
	@Override
	public void buildFooter(ExportFooterModel efm) {
		buffer.append(efm.getExportUser());
	}
	
	public StringBuffer getResult(){
		return buffer;
	}
}

/**
 * @description实现导出数据到xml文件的生成器对象
 * @author liuwei
 * @date 2014-4-17
 * @version 1.0
 */
public class XmlBuilder implements Builder{
	/**
	 * 用来记录构建的文件内容,相当于产品
	 */
	private StringBuffer buffer = new StringBuffer();
	/* (non-Javadoc)
	 * @see builder.Builder#buildHeader(builder.ExportHeaderModel)
	 */
	@Override
	public void buildHeader(ExportHeaderModel ehm) {
		buffer.append("<?xml version='1.0' encoding='GBK'?>\n");
		buffer.append("<Report>\n");
		buffer.append("  <Header>\n");
		buffer.append("    <DepId>" + ehm.getDepId() + "</DepId>\n");
		buffer.append("    <ExportDate>" + ehm.getExportDate() + "</ExportDate>\n");
		buffer.append("  </Header>\n");
	}
	/* (non-Javadoc)
	 * @see builder.Builder#buildBody(java.util.Map)
	 */
	@Override
	public void buildBody(Map<String, Collection<ExportDataModel>> mapData) {
		buffer.append("  <Body>\n");
	       for(String tblName : mapData.keySet()){
	           //先拼接表名称
	           buffer.append("    <Datas TableName=\""+tblName+"\">\n");
	           //然后循环拼接具体数据
	           for(ExportDataModel edm : mapData.get(tblName)){
	              buffer.append("      <Data>\n");
	              buffer.append("        <ProductId>"+edm.getProductId()+"</ProductId>\n");
	              buffer.append("        <Price>"+edm.getPrice()+"</Price>\n");
	              buffer.append("        <Amount>"+edm.getAmount()+"</Amount>\n");
	              buffer.append("      </Data>\n");
	           }
	           buffer.append("    </Datas>\n");
	       }
	    buffer.append("  </Body>\n");
	}
	/* (non-Javadoc)
	 * @see builder.Builder#buildFooter(builder.ExportFooterModel)
	 */
	@Override
	public void buildFooter(ExportFooterModel efm) {
	    buffer.append("  <Footer>\n");
        buffer.append("    <ExportUser>"+efm.getExportUser()+"</ExportUser>\n");
        buffer.append("  </Footer>\n");
        buffer.append("</Report>\n");
	}
	
	public StringBuffer getResult(){
		return buffer;
	}
}

/**
 * @description 指导者,指导使用生成器的接口来构建输出的文件的对象
 * @author liuwei
 * @date 2014-4-17
 * @version 1.0
 */
public class Director {
	/**指定的生成器*/
	private Builder builder;
	/**
	 * 传入生成器
	 * @param builder
	 */
	public Director(Builder builder){
		this.builder = builder;
	}
	/**
	 * 指导生产器构建最终的文件输出对象
	 * @param ehm
	 * @param mapData
	 * @param efm
	 */
	public void construct(ExportHeaderModel ehm, Map<String, Collection<ExportDataModel>> mapData, ExportFooterModel efm){
		//1:先构建header
		builder.buildHeader(ehm);
		//2:再构建body
		builder.buildBody(mapData);
		//3:再构建footer
		builder.buildFooter(efm);
	}
}

/**
 * @description 生成器模式:将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
 * @author liuwei
 * @date 2014-4-17
 * @version 1.0
 */
public class Client {

	public static void main(String[] args) {
		 //准备测试数据
	       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("产品001号");
	       edm1.setPrice("100");
	       edm1.setAmount("80");
	 
	       ExportDataModel edm2 = new ExportDataModel();
	       edm2.setProductId("产品002号");
	       edm2.setPrice("99");
	       edm2.setAmount("55");     
	       //把数据组装起来
	       col.add(edm1);
	       col.add(edm2);      
	       mapData.put("销售记录表", col);
	 
	       ExportFooterModel efm = new ExportFooterModel();
	       efm.setExportUser("张三");
	       
	       //测试输出到文本文件
	       TxtBuilder txtBuilder = new TxtBuilder();
	       //创建指导者对象
	       Director director = new Director(txtBuilder);
	       director.construct(ehm, mapData, efm);
	       //输出文本文件内容
	       System.out.println("输出文本文件的内容:\n" + txtBuilder.getResult());
	       
	       //测试输出到xml文件
	       XmlBuilder xmlBuilder = new XmlBuilder();
	       Director director2 = new Director(xmlBuilder);
	       director.construct(ehm, mapData, efm);
	       //输出xml文件内容
	       System.out.println("输出xml文件的内容:\n" + xmlBuilder.getResult());
	}
}

你可能感兴趣的:(模式)