DIY迷你郵件用戶端的開發算是告一段落,能夠從中擷取的東西需要往後實踐中去感受和踐行了。面對開始出現的問題,沒能處理好,隻有在整個過程來處理和消化掉可能帶來的更多問題。
思考DIY迷你用戶端開發手記(一)中的5中問題才是清除掉亂七八糟的工程之後的價值了。
1.業務政策程式設計
在整個應用中最核心的問題是:通過選擇收件人的郵件位址的源檔案,加載到收件人的集合中,進而發送相應的郵件。
處理這個問題自然想到了政策模式。
先定義一個接口,聲明了子類需要實作的方法,然後通過不同的需求來實作其中的功能。
編碼-0:定義了解析檔案中的聯系人郵件位址的政策接口
- public interface AnalysisStrategy {
- /**
- *
- * 解析聯系人資料源
- *
- * @return list
- */
- public List<String> analysisSrc();
- }
編碼-1:主要的兩種模式是,解析Excel檔案和XML檔案
- public class ExcelContacts implements AnalysisStrategy {
- private File excelFile;
- public ExcelContacts(File f) {
- this.excelFile = f;
- }
- /**
- * 聯系人Excel表
- *
- * @help 參見規定的Excel格式
- *
- * @return list
- */
- @Override
- public List<String> analysisSrc() {
- Workbook wb = null;
- List<String> contactsList = null;
- try {
- wb = Workbook.getWorkbook(excelFile);
- Sheet[] sheets = wb.getSheets();
- contactsList = new ArrayList<String>();
- for (int i = 0, j = sheets.length; i < j; i++) {
- Cell[] cells = sheets[i].getColumn(2);
- for (int row = 1, rows = cells.length; row < rows; row++) {
- String str=cells[row].getContents().trim();
- if(MiniMailTool.checkEmail(str)){
- contactsList.add(str);
- }
- }
- }
- } catch (IOException ex) {
- Logger.getLogger(ExcelContacts.class.getName()).log(Level.SEVERE, null, ex);
- } catch (BiffException ex) {
- Logger.getLogger(ExcelContacts.class.getName()).log(Level.SEVERE, null, ex);
- }
- return contactsList;
- }
- }
- //-----------------------------------------------------//
- public class XMLContacts implements AnalysisStrategy {
- private File xmlFile;
- public XMLContacts(File f) {
- this.xmlFile=f;
- }
- /**
- * 聯系人XML表
- *
- * @help 參見規定的XML格式
- *
- * @param xmlFile
- * @return list
- */
- @Override
- public List<String> analysisSrc() {
- List<String> contactsList=null;
- try {
- SAXBuilder sb =new SAXBuilder();
- Document doc=sb.build(xmlFile);
- Element root=doc.getRootElement();
- List<Element> userList=root.getChildren("user");
- contactsList=new ArrayList<String>();
- for(int i=0, j=userList.size(); i<j; i++){
- Element children=userList.get(i);
- String str=children.getChildText("email").trim();
- if(MiniMailTool.checkEmail(str)){
- contactsList.add(str);
- }
- }
- } catch (JDOMException ex) {
- Logger.getLogger(XMLContacts.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(XMLContacts.class.getName()).log(Level.SEVERE, null, ex);
- }
- return contactsList;
- }
- }
通過這樣的方式就可以将不同的檔案的解析聯系人的過程封裝掉,對于調用者來講是相同的,隻需要面向接口程式設計就可以了。
這裡給出源檔案的格式:
EXCEL表的格式
XML格式:
這裡的格式可以按照已經約定的方式書寫,當然亦可以自定義格式,這樣就應該編寫實作
AnalysisStrategy(聯系人解析)接口。
2.處理一些關于字元串,郵件位址驗證,擷取收件人位址資訊
這樣的問題一般在程式設計開始的時候會有所考慮,倒是帶來的後續問題不大,如何有效的編寫更加通用的代碼,才是問題的關鍵。
這裡有個關于工具類的命名問題,比如:Utils,Tools,Helper等類的命名都是非常不可取的方式,命名要簡明思議,并且能夠傳單一定的資訊,像這個工具類處理哪方面的問題,都應該能夠很好的放映出來。
工具類有可能涉及到:字元串處理方面;檔案解析,檔案過濾方面;圖形使用者界面程式設計中的元件資訊處理方面;特定的多出使用的方法,變量等;類的構造和管理等方面。
這個應用程式設計的相關方法不是很多,較好的處理掉了。
編碼-2:郵件用戶端程式的工具類
- /**
- *
- * 郵件用戶端程式工具類
- *
- * @author aiilive
- */
- public final class MiniMailTool {
- /**
- * 擷取架構在Windows中顯示的中心點
- *
- * @param jf
- * @return 中心點
- */
- public static Point getCenter(JFrame jf) {
- Point p = new Point();
- Dimension dim = jf.getSize();
- int width = dim.width;
- int height = dim.height;
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- p.setLocation((screenSize.width - width) / 2, ((screenSize.height - height) / 2));
- return p;
- }
- /**
- * 通過收件人資訊擷取收件人
- *
- * @param contactsString
- * @return List
- */
- public static List getToContacts(String contactsText) {
- String[] contacts = contactsText.split(";");
- return Arrays.asList(contacts);
- }
- /**
- * 驗證輸入的聯系人的電子郵箱的有效性
- *
- * @param contactsText
- * @return bo
- */
- public static boolean checkContacts(String contactsText){
- String[] contacts = contactsText.split(";");
- boolean bo=true;
- for (int i = 0; i < contacts.length; i++) {
- contacts[i] = contacts[i].trim();
- if(!checkEmail(contacts[i])){
- bo=false;
- break;
- }
- }
- return bo;
- }
- /**
- * 中文字元轉換
- *
- * @param str
- * @return strEncode
- */
- public static String chineseEncode(String str) {
- String strEncode = str;
- try {
- byte[] bts = str.getBytes("ISO-8859-1");
- strEncode = new String(bts, "GB2312");
- } catch (UnsupportedEncodingException ex) {
- Logger.getLogger(MiniMailTool.class.getName()).log(Level.SEVERE, null, ex);
- }
- return strEncode;
- }
- /**
- * 将字元數組char [] 轉化為字元串String
- *
- * @param ch []
- * @return String
- */
- public static String arrayToString(char[] ch) {
- if (ch == null) {
- return "null";
- }
- StringBuilder b = new StringBuilder();
- for (int i = 0; i < ch.length; i++) {
- b.append(ch[i]);
- }
- return b.toString();
- }
- /**
- * 驗證電子郵件位址是否有效
- *
- * @param email 電子郵件位址字元串
- * @return 布爾值
- */
- public static boolean checkEmail(String email) {
- Pattern pattern=Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");
- Matcher matcher=pattern.matcher(email);
- return matcher.matches();
- }
- }
上面的代碼涉及到了使用者界面元件的工具類方法,字元串處理,常用的驗證等。對于一個較大的工程,或者是涉及相關的操作非常多和複雜的時候,就得考慮方法的組織,重構,提取,建立更好的類的層次關系這樣才符合面向對象程式設計基本思想,才能夠獲得可以水準和垂直擴充的機會。同時使得代碼的耦合度降到最低,想要達到如此美好的境況,又得回到設計群組織方面去。