代碼整潔-函數重構

一:需求如下

生成一批word文檔(分不同類型),每個word插入一些文字和一個唯一的二維碼;比如100個人會生成100個單獨的word,

目前客戶需求修改爲同一類(ABCDE五類)只生成一個word文檔

導出word功能原代碼如下(重構前):

/**
	 * 創建word文檔,將二維碼圖片插入
	 * @param contextString 
	 */
	public static String exportDoc(String realPath, Integer index, String typeLable, String contextString)
			throws DocumentException, IOException {
		// 設置紙張大小
		Document document = new Document(PageSize.A4);
		// 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中
		// ByteArrayOutputStream baos = new ByteArrayOutputStream();
		// 創建文件路徑分二維碼類型ABDCE類
		File file = new File(realPath + File.separator + typeLable + "類" + File.separator);
		if (!file.exists()) {
			file.mkdir();
		}
		// 創建word文件
		File fileWrod = new File(realPath + File.separator + typeLable + "類" + File.separator + "中交考評" + typeLable + "類"
				+ (index + 1) + ".doc");
		RtfWriter2.getInstance(document, new FileOutputStream(fileWrod));
		document.open();
		// 設置中文字體
		BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
		// 標題字體風格
		Font titleFont = new Font(bfChinese, 36, Font.BOLD);
		// // 正文字體風格
		Font contextFont = new Font(bfChinese, 17, Font.NORMAL);
		Paragraph title = new Paragraph("績效考評" + typeLable + "類");
		// 設置標題格式對齊方式
		title.setAlignment(Element.ALIGN_CENTER);
		title.setFont(titleFont);
		document.add(title);
		//設置內容
		Paragraph context = new Paragraph(contextString);
		String contextString1 = "開始你的測評";
		Paragraph context1 = new Paragraph(contextString1);
		context1.setAlignment(Element.ALIGN_CENTER);
		context1.setFont(contextFont);
		// 正文格式左對齊
		context.setAlignment(Element.ALIGN_LEFT);
		context.setFont(contextFont);
		// 離上一段落(標題)空的行數
		context.setSpacingBefore(10);
		// 設置第一行空的列數
		context.setFirstLineIndent(50);
		document.add(context);
		document.add(context1);
		// // 利用類FontFactory結合Font和Color可以設置各種各樣字體樣式
		// Paragraph underline = new Paragraph("下劃線的實現", FontFactory.getFont(
		// FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE,
		// new Color(0, 0, 255)));
		// document.add(underline);
		// // 添加圖片 Image.getInstance即可以放路徑又可以放二進制字節流
		Image img = Image.getInstance(realPath + "考評二維碼_" + typeLable + index + ".png");
		img.setAbsolutePosition(0, 0);
		img.setAlignment(Image.ALIGN_CENTER);// 設置圖片顯示位置
		img.scaleAbsolute(140, 140);// 直接設定顯示尺寸
		// // img.scalePercent(50);//表示顯示的大小爲原尺寸的50%
		// // img.scalePercent(25, 12);//圖像高寬的顯示比例
		// // img.setRotation(30);//圖像旋轉一定角度
		document.add(img);
		document.close();
		// 得到輸入流
		// wordFile = new ByteArrayInputStream(baos.toByteArray());
		// baos.close();
		return "";
	}

二:函數問題

1.傳入參數命名不明確

2.註釋的代碼太多

3.生成word過程不明顯

三:重構

將word中各種不同類型的內容進行封裝,標題createTitle,主體內容createMainContext,測試內容createTestContext,圖片createImage,控制分頁createNextPageContext

/**
	 * 創建word文檔,將二維碼圖片插入
	 * 
	 * @param contextString
	 * @param typeValue 
	 */
	public static String exportDoc(String realPath, String wordType, String contextStr, Integer wordTypeCnt)
			throws DocumentException, IOException {
		// 創建文件路徑分二維碼類型ABDCE類
		File file = new File(realPath + File.separator + wordType + "類" + File.separator);
		if (!file.exists()) {
			file.mkdir();
		}
		// 創建word文件
		File fileWrod = new File(realPath + File.separator + wordType + "類" + File.separator + "XX考評" + wordType + "類"
				 + ".doc");

		Document document = new Document(PageSize.A4);

		RtfWriter2.getInstance(document, new FileOutputStream(fileWrod));
		document.open();
		
		BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
		Font titleFont = new Font(bfChinese, 36, Font.BOLD);
		Font contextFont = new Font(bfChinese, 17, Font.NORMAL);
		
		Paragraph mainContext = createMainContext(contextStr, contextFont);
		Paragraph startTestContext = createTestContext(contextFont);
		Paragraph nextPageContext = createNextPageContext();
		
		for (int j = 0; j < wordTypeCnt; j++) {
			Paragraph title = createTitle(wordType, titleFont,j);
			document.add(title);
			document.add(mainContext);
			document.add(startTestContext);
			Image img = createImage(realPath, wordType, j);
			document.add(img);
			document.add(nextPageContext);
		}

		document.close();
		return "";
	}

	private static Paragraph createNextPageContext() {
		Paragraph title = new Paragraph("\n\n\n");
		return title;
	}

	private static Paragraph createTitle(String typeLable, Font titleFont,int index) {
		Paragraph title = new Paragraph("xx考評" + typeLable + "類"+(index+1));
		title.setAlignment(Element.ALIGN_CENTER);
		title.setFont(titleFont);
		return title;
	}

	private static Paragraph createTestContext(Font contextFont) {
		Paragraph startTestContext = new Paragraph("開始你的測評");
		startTestContext.setAlignment(Element.ALIGN_CENTER);
		startTestContext.setFont(contextFont);
		return startTestContext;
	}

	private static Paragraph createMainContext(String contextString, Font contextFont) {
		Paragraph context = new Paragraph(contextString);
		context.setAlignment(Element.ALIGN_LEFT);
		context.setFont(contextFont);
		context.setSpacingBefore(10);
		context.setFirstLineIndent(50);
		return context;
	}

	private static Image createImage(String realPath, String typeLable, Integer index)
			throws BadElementException, MalformedURLException, IOException {
		Image img = Image.getInstance(realPath + "考評二維碼_" + typeLable + index + ".png");
		img.setAbsolutePosition(0, 0);
		img.setAlignment(Image.ALIGN_CENTER);// 設置圖片顯示位置
		img.scaleAbsolute(140, 140);// 直接設定顯示尺寸
		// // img.scalePercent(50);//表示顯示的大小爲原尺寸的50%
		// // img.scalePercent(25, 12);//圖像高寬的顯示比例
		// // img.setRotation(30);//圖像旋轉一定角度
		return img;
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章