天天看點

DIY迷你郵件用戶端開發手記(二)

     DIY迷你郵件用戶端的開發算是告一段落,能夠從中擷取的東西需要往後實踐中去感受和踐行了。面對開始出現的問題,沒能處理好,隻有在整個過程來處理和消化掉可能帶來的更多問題。

    思考DIY迷你用戶端開發手記(一)中的5中問題才是清除掉亂七八糟的工程之後的價值了。

  1.業務政策程式設計

       在整個應用中最核心的問題是:通過選擇收件人的郵件位址的源檔案,加載到收件人的集合中,進而發送相應的郵件。

      處理這個問題自然想到了政策模式。

     先定義一個接口,聲明了子類需要實作的方法,然後通過不同的需求來實作其中的功能。

     編碼-0:定義了解析檔案中的聯系人郵件位址的政策接口

  1. public interface AnalysisStrategy {  
  2.     /**  
  3.      *   
  4.      * 解析聯系人資料源  
  5.      *   
  6.      * @return list  
  7.      */ 
  8.     public List<String> analysisSrc();  

     編碼-1:主要的兩種模式是,解析Excel檔案和XML檔案

  1. public class ExcelContacts implements AnalysisStrategy {  
  2.     private File excelFile;  
  3.     public ExcelContacts(File f) {  
  4.         this.excelFile = f;  
  5.     }  
  6.     /**  
  7.      * 聯系人Excel表  
  8.      *  
  9.      * @help 參見規定的Excel格式  
  10.      *  
  11.      * @return list  
  12.      */ 
  13.     @Override 
  14.     public List<String> analysisSrc() {  
  15.         Workbook wb = null;  
  16.         List<String> contactsList = null;  
  17.         try {  
  18.             wb = Workbook.getWorkbook(excelFile);  
  19.             Sheet[] sheets = wb.getSheets();  
  20.             contactsList = new ArrayList<String>();  
  21.             for (int i = 0, j = sheets.length; i < j; i++) {  
  22.                 Cell[] cells = sheets[i].getColumn(2);  
  23.                 for (int row = 1, rows = cells.length; row < rows; row++) {  
  24.                     String str=cells[row].getContents().trim();  
  25.                     if(MiniMailTool.checkEmail(str)){  
  26.                         contactsList.add(str);  
  27.                     }  
  28.                 }  
  29.             }  
  30.         } catch (IOException ex) {  
  31.             Logger.getLogger(ExcelContacts.class.getName()).log(Level.SEVERE, null, ex);  
  32.         } catch (BiffException ex) {  
  33.             Logger.getLogger(ExcelContacts.class.getName()).log(Level.SEVERE, null, ex);  
  34.         }  
  35.         return contactsList;  
  36.     }  
  37. }  
  38. //-----------------------------------------------------//  
  39. public class XMLContacts implements AnalysisStrategy {  
  40.     private File xmlFile;  
  41.     public XMLContacts(File f) {  
  42.         this.xmlFile=f;  
  43.     }  
  44.     /**  
  45.      * 聯系人XML表   
  46.      *   
  47.      * @help 參見規定的XML格式  
  48.      *  
  49.      * @param xmlFile  
  50.      * @return list  
  51.      */ 
  52.     @Override 
  53.     public List<String> analysisSrc() {  
  54.         List<String> contactsList=null;  
  55.         try {  
  56.             SAXBuilder sb =new SAXBuilder();  
  57.             Document doc=sb.build(xmlFile);  
  58.             Element root=doc.getRootElement();  
  59.             List<Element> userList=root.getChildren("user");  
  60.             contactsList=new ArrayList<String>();  
  61.             for(int i=0, j=userList.size(); i<j; i++){  
  62.                 Element children=userList.get(i);  
  63.                 String str=children.getChildText("email").trim();  
  64.                 if(MiniMailTool.checkEmail(str)){  
  65.                     contactsList.add(str);  
  66.                 }  
  67.             }  
  68.         } catch (JDOMException ex) {  
  69.             Logger.getLogger(XMLContacts.class.getName()).log(Level.SEVERE, null, ex);  
  70.         } catch (IOException ex) {  
  71.             Logger.getLogger(XMLContacts.class.getName()).log(Level.SEVERE, null, ex);  
  72.         }  
  73.         return contactsList;  
  74.     }  

    通過這樣的方式就可以将不同的檔案的解析聯系人的過程封裝掉,對于調用者來講是相同的,隻需要面向接口程式設計就可以了。

   這裡給出源檔案的格式:

   EXCEL表的格式

DIY迷你郵件用戶端開發手記(二)

   XML格式:

DIY迷你郵件用戶端開發手記(二)

     這裡的格式可以按照已經約定的方式書寫,當然亦可以自定義格式,這樣就應該編寫實作

AnalysisStrategy(聯系人解析)接口。

2.處理一些關于字元串,郵件位址驗證,擷取收件人位址資訊

       這樣的問題一般在程式設計開始的時候會有所考慮,倒是帶來的後續問題不大,如何有效的編寫更加通用的代碼,才是問題的關鍵。

       這裡有個關于工具類的命名問題,比如:Utils,Tools,Helper等類的命名都是非常不可取的方式,命名要簡明思議,并且能夠傳單一定的資訊,像這個工具類處理哪方面的問題,都應該能夠很好的放映出來。

       工具類有可能涉及到:字元串處理方面;檔案解析,檔案過濾方面;圖形使用者界面程式設計中的元件資訊處理方面;特定的多出使用的方法,變量等;類的構造和管理等方面。

       這個應用程式設計的相關方法不是很多,較好的處理掉了。

      編碼-2:郵件用戶端程式的工具類 

  1. /**  
  2.  *  
  3.  * 郵件用戶端程式工具類  
  4.  *  
  5.  * @author aiilive   
  6.  */ 
  7. public final class MiniMailTool {  
  8.     /**  
  9.      * 擷取架構在Windows中顯示的中心點  
  10.      *  
  11.      * @param jf  
  12.      * @return 中心點  
  13.      */ 
  14.     public static Point getCenter(JFrame jf) {  
  15.         Point p = new Point();  
  16.         Dimension dim = jf.getSize();  
  17.         int width = dim.width;  
  18.         int height = dim.height;  
  19.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  20.         p.setLocation((screenSize.width - width) / 2, ((screenSize.height - height) / 2));  
  21.         return p;  
  22.     }  
  23.     /**  
  24.      * 通過收件人資訊擷取收件人  
  25.      *  
  26.      * @param contactsString  
  27.      * @return List  
  28.      */ 
  29.     public static List getToContacts(String contactsText) {  
  30.        String[] contacts = contactsText.split(";");   
  31.         return Arrays.asList(contacts);  
  32.     }  
  33.     /**  
  34.      * 驗證輸入的聯系人的電子郵箱的有效性  
  35.      *   
  36.      * @param contactsText  
  37.      * @return bo  
  38.      */ 
  39.     public static boolean checkContacts(String contactsText){  
  40.         String[] contacts = contactsText.split(";");  
  41.         boolean bo=true;  
  42.         for (int i = 0; i < contacts.length; i++) {  
  43.             contacts[i] = contacts[i].trim();  
  44.             if(!checkEmail(contacts[i])){  
  45.                 bo=false;  
  46.                 break;  
  47.             }  
  48.         }  
  49.         return bo;  
  50.     }  
  51.     /**  
  52.      * 中文字元轉換  
  53.      *  
  54.      * @param str  
  55.      * @return strEncode  
  56.      */ 
  57.     public static String chineseEncode(String str) {  
  58.         String strEncode = str;  
  59.         try {  
  60.             byte[] bts = str.getBytes("ISO-8859-1");  
  61.             strEncode = new String(bts, "GB2312");  
  62.         } catch (UnsupportedEncodingException ex) {  
  63.             Logger.getLogger(MiniMailTool.class.getName()).log(Level.SEVERE, null, ex);  
  64.         }  
  65.         return strEncode;  
  66.     }  
  67.     /**  
  68.      * 将字元數組char [] 轉化為字元串String  
  69.      *  
  70.      * @param ch []  
  71.      * @return String  
  72.      */ 
  73.     public static String arrayToString(char[] ch) {  
  74.         if (ch == null) {  
  75.             return "null";  
  76.         }  
  77.         StringBuilder b = new StringBuilder();  
  78.         for (int i = 0; i < ch.length; i++) {  
  79.             b.append(ch[i]);  
  80.         }  
  81.         return b.toString();  
  82.     }  
  83.     /**  
  84.      * 驗證電子郵件位址是否有效  
  85.      *  
  86.      * @param email 電子郵件位址字元串  
  87.      * @return 布爾值  
  88.      */ 
  89.     public static boolean checkEmail(String email) {  
  90.         Pattern pattern=Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");  
  91.         Matcher matcher=pattern.matcher(email);  
  92.         return matcher.matches();  
  93.     }  

      上面的代碼涉及到了使用者界面元件的工具類方法,字元串處理,常用的驗證等。對于一個較大的工程,或者是涉及相關的操作非常多和複雜的時候,就得考慮方法的組織,重構,提取,建立更好的類的層次關系這樣才符合面向對象程式設計基本思想,才能夠獲得可以水準和垂直擴充的機會。同時使得代碼的耦合度降到最低,想要達到如此美好的境況,又得回到設計群組織方面去。

繼續閱讀