熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

設計模

2022-06-13   來源: Java高級技術 

  近段要實現一個上傳excel文件到服務器並把excel文件的數據導入到數據庫表中的功能
    excel文件有兩種格式對應數據庫兩張表我先實現了其中一種excel格式式的上傳和導入數據上傳的功能是一樣的沒什麼好說的不同的是兩種excel文件格式不一樣對應的數據庫表的字段也不一樣怎麼辦呢?
    開始的時候我先做了一個excel文件的功能做另一個很相似了運用c/p大法就搞定了:)
    代碼是這樣的
    action中:
 CityDayService cds = new CityDayServiceImpl();
  ImpExcelData impExcelData = new ImpExcelData(filePath );   //這裡從excel文件中取出數據
  try {
   List list = impExcelDatagetData();                  //放進list中用的是jxl
         List<CityDay> sameList = cdssameList(list);//這裡有不同

  //boolean isOk = cdscheckExcel(list);
   if (sameListsize()>) {
    requestsetAttribute(sameList sameList);
    request
      setAttribute(echo
        以下數據有重復是否覆蓋);
    return mappingfindForward(upload);
   } else {

  cdsadd(list);//這裡有不同
   }
   Systemoutprint();
  } catch (Exception e) {
   // TODO Autogenerated catch block
   eprintStackTrace();
  }

  另一種格式的excel文件對應的action是這樣的

  /* 導入表 */

    ZhbTCityDayForecastService cds = new ZhbTCityDayForecastServiceImpl();/////////////////
  ImpExcelData impExcelData = new ImpExcelData(filePath );
  try {
   List list = impExcelDatagetData();
         List<ZhbTCityDayForecast> sameList = cdssameList(list);////////////////////
   if (sameListsize()>) {
    requestsetAttribute(sameList sameList);
    request
      setAttribute(echo
        數據有重復是否覆蓋);
    return mappingfindForward(upload);
   } else {

  cdsadd(list);///////////////////////////////
   }
   Systemoutprint();
  } catch (Exception e) {
   // TODO Autogenerated catch block
   eprintStackTrace();
  }


  
    業務層:

     public interface CityDayService{
  public List findByCityAndDate(String cityDate date);
  public void deleteSame(List excelDataList);
  public void add(List excelDatalist);
  public List sameList(List excelDatalist);
    }
    和
     public interface ZhbTCityDayForecastService{
  public List findByCityAndDate(String cityDate date);
  public void deleteSame(List excelDataList);
  public void add(List excelDatalist);
  public List sameList(List excelDatalist);
   }

  業務層實現類:

       public class CityDayServiceImpl implements CityDayService {

  public List findByCityAndDate(String city Date date) {
  CityDayDAO cdd = new CityDayDAOImpl();
  return cddfindByCityAndDate(city date);
 }

  public void add(List excelDatalist) {
 Session session = HibernateSessionFactorygetSession();
 Transaction tran = sessionbeginTransaction();
  List<CityDay> newList = new ArrayList();
  for (int i = ; i < excelDatalistsize(); i++) {
   HashMap map = (HashMap) excelDatalistget(i);
   String strDate = mapget() + ;
   Date date;
   try {
    date = new SimpleDateFormat(yyyyMMdd)parse(strDate);
    CityDayDAO cdd = new CityDayDAOImpl();
    CityDay cd = new CityDay();
    cdsetDate(date);
    cdsetCity(mapget() + );
    cdsetPollutionIndeces(new Integer(mapget()+));
    cdsetMainPollutant(mapget()toString());
    cdsetGrade(mapget()toString());
    cdsetStatus(mapget()toString());
    cddsaveOrUpdate(cd);
   } catch (ParseException e) {
    tranrollback();
    eprintStackTrace();
   }

  }
    mit();
 }

  public void deleteSame(List excelDataList) {

  CityDayDAO cdd = new CityDayDAOImpl();
  boolean result = true;
  List<CityDay> list = new ArrayList<CityDay>();
  for (int i = ; i < excelDataListsize(); i++) {
   HashMap map = (HashMap) excelDataListget(i);
   String city = mapget() + ;
   String strDate = mapget() + ;
   Date date;
   try {
    date = new SimpleDateFormat(yyyyMMdd)parse(strDate);
    List onelist = cddfindByCityAndDate(city date);
    for(int j=;j<onelistsize();j++){
     listadd((CityDay)onelistget(j));
    }
   } catch (ParseException e) {
    // TODO Autogenerated catch block
    eprintStackTrace();
   }

  }
  for(int j=;j<listsize();j++){
   CityDay cd = (CityDay)listget(j);
   Systemoutprintln(要刪除的數據有:\n + city:+cdgetCity()+date: + cdgetDate());
  }
  cdddelete(list);
 }

  public List<CityDay> sameList(List excelDatalist) {
  CityDayDAO cdd = new CityDayDAOImpl();
  boolean result = true;
  List<CityDay> list = new ArrayList<CityDay>();
  for (int i = ; i < excelDatalistsize(); i++) {
   HashMap map = (HashMap) excelDatalistget(i);
   String city = mapget() + ;
   String strDate = mapget() + ;
   Date date;
   try {
    date = new SimpleDateFormat(yyyyMMdd)parse(strDate);
    List onelist = cddfindByCityAndDate(city date);
    for(int j=;j<onelistsize();j++){
     listadd((CityDay)onelistget(j));
    }
   } catch (ParseException e) {
    // TODO Autogenerated catch block
    eprintStackTrace();
   }

  }
  for(int j=;j<listsize();j++){
   CityDay cd = (CityDay)listget(j);
   Systemoutprintln(重復的數據有:\n + city:+cdgetCity()+date: + cdgetDate());
  }
  return list;
 }

  }
    和

  public class ZhbTCityDayForecastServiceImpl implements ZhbTCityDayForecastService {

  public List findByCityAndDate(String city Date date) {
  ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();
  return cddfindByCityAndDate(city date);
 }

  public void add(List excelDatalist) {
 Session session = HibernateSessionFactorygetSession();
 Transaction tran = sessionbeginTransaction();
  List<CityDay> newList = new ArrayList();
  for (int i = ; i < excelDatalistsize(); i++) {
   HashMap map = (HashMap) excelDatalistget(i);
   String strDate = mapget()++mapget()++mapget() + ;
   Date date;
   try {
    date = new SimpleDateFormat(yyyyMMdd)parse(strDate);
    ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();
    ZhbTCityDayForecast cd = new ZhbTCityDayForecast();
    cdsetCity(mapget() + );
    cdsetDate(date);
    cdsetPollutionMin(new Integer(mapget()+));
    cdsetPollutionMax(new Integer(mapget()+));
    cdsetMainPollutant(mapget()+);
    cdsetGrade(mapget()toString());
    cdsetStatus(mapget()toString());
    cddsaveOrUpdate(cd);
   } catch (ParseException e) {
    tranrollback();
    eprintStackTrace();
   }

  }
    mit();
 }
 public void deleteSame(List excelDataList) {

  ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();
  boolean result = true;
  List<ZhbTCityDayForecast> list = new ArrayList<ZhbTCityDayForecast>();
  for (int i = ; i < excelDataListsize(); i++) {
   HashMap map = (HashMap) excelDataListget(i);
   String city = mapget() + ;
   String strDate = mapget()++mapget()++mapget() + ;
   Date date;
   try {
    date = new SimpleDateFormat(yyyyMMdd)parse(strDate);
    List<ZhbTCityDayForecast> onelist = cddfindByCityAndDate(city date);
    for(int j=;j<onelistsize();j++){
     listadd((ZhbTCityDayForecast)onelistget(j));
    }
   } catch (ParseException e) {
    // TODO Autogenerated catch block
    eprintStackTrace();
   }

  }
  cdddelete(list);
 }

  public List<ZhbTCityDayForecast> sameList(List excelDatalist) {
  // TODO Autogenerated method stub
  ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();
  List<ZhbTCityDayForecast> list = new ArrayList<ZhbTCityDayForecast>();
  for (int i = ; i < excelDatalistsize(); i++) {
   HashMap map = (HashMap) excelDatalistget(i);
   String city = mapget() + ;
   String strDate = mapget()++mapget()++mapget() + ;
   Date date;
   try {
    date = new SimpleDateFormat(yyyyMMdd)parse(strDate);
    List onelist = cddfindByCityAndDate(city date);
       for(int j=;j<onelistsize();j++){
     listadd((ZhbTCityDayForecast)onelistget(j));
    }
   } catch (ParseException e) {
    // TODO Autogenerated catch block
    eprintStackTrace();
   }
  }
  return list;
 }
 }

  這樣做完後自己感覺很不滿意因為兩個相似的功能寫了兩遍代碼:兩個actionservice還有兩個頁面很笨
    如果以後需求增加再寫其它的excel格式豈不是很慘又要用cp大法了初步的思路是只一個action和一個頁面
    經過思索發現只有service層的
    public void add(List excelDatalist);
    public List sameList(List excelDatalist);
    public void deleteSame(List excelDataList);
    三個方法是不同的

  那麼在action中可以根據參數生成不同的service執行不同的service中的service方法可以用工廠方法生產出不同的service思路有了開干

  首先把這三個方法提到一個接口裡來

    public interface CommonUploadService {
  public void add(List excelDatalist);
  public List sameList(List excelDatalist);
  public void deleteSame(List excelDataList);
   }

  原為的service就變成這樣

  public interface CityDayService extends CommonUploadService{
  public List findByCityAndDate(String cityDate date);
   }

  public interface ZhbTCityDayForecastService extends CommonUploadService{
  public List findByCityAndDate(String cityDate date);
   }

  實現類不用變

  工廠方法

 public class UploadServiceFactory {

  private static CommonUploadService cus;

  private static final String cityDayService = comxxxdatacenteruploadserviceCityDayServiceImpl;

  private static final String foreCastService = comxxxdatacenteruploadforecastserviceCityDayServiceImpl;

  private UploadServiceFactory() {
 }

  public static CommonUploadService getInstance(String type) {
  try {
   if (dailyequals(type)) {
    Class cls = ClassforName(cityDayService);
    cus = (CommonUploadService) clsnewInstance();
   }
   if (forecastequals(type)) {
    Class cls = ClassforName(foreCastService);
    cus = (CommonUploadService) clsnewInstance();
   }
  } catch (Exception e) {
   eprintStackTrace();
  }
  return cus;
 }
  }

  改造後的action

  /* 導入表 */
 // CityDayService cds = new CityDayServiceImpl();//這裡有不同 可以用工廠方法根據參數type不同構造出
     String type = requestgetParameter(type);
     CommonUploadService cds =  UploadServiceFactorygetInstance(type);
  ImpExcelData impExcelData = new ImpExcelData(filePath );
  try {
   List list = impExcelDatagetData();
         List<CityDay> sameList = cdssameList(list);//這裡有不同

  //boolean isOk = cdscheckExcel(list);
   if (sameListsize()>) {
    requestsetAttribute(sameList sameList);
    request
      setAttribute(echo
        以下數據有重復是否覆蓋);
    return mappingfindForward(upload);
   } else {

  cdsadd(list);//這裡有不同
   }
   Systemoutprint();
  } catch (Exception e) {
   // TODO Autogenerated catch block
   eprintStackTrace();
  }

  這樣一個action就可以搞定了
    如果以為要增加新格式的excel文件只實現CommonUploadService方法改造一下工廠類就ok了

  總結:
    以後遇到功能類似的需求可以把接口相同但實現不同的方法提到共的提口來然後用工廠方法生成不同的實例
    運用面向對象的多態的性能就可以簡化開發
    設計模式關暫在於實踐光看書本是不行的有什麼不對的地方請多多指點


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27596.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.