您现在的位置是:首页 >

设计模式23种还是26种 设计模

火烧 2023-04-21 22:41:41 1056
设计模   近段要实现一个上传excel文件到服务器 并把excel文件的数据导入到数据库表中的功能 excel文件有两种格式 对应数据库两张表 我先实现了其中一种excel格式式的上传和导入数据 上

设计模  

  近段要实现一个上传excel文件到服务器 并把excel文件的数据导入到数据库表中的功能     excel文件有两种格式 对应数据库两张表 我先实现了其中一种excel格式式的上传和导入数据 上传的功能是一样的 没什么好说的 不同的是两种excel文件格式不一样 对应的数据库表的字段也不一样 怎么办呢?    开始的时候 我先做了一个excel文件的功能 做另一个很相似了 运用c/p大法就搞定了:)     代码是这样的     action中:  CityDayService cds = new CityDayServiceImpl();  ImpExcelData impExcelData = new ImpExcelData(filePath );   //这里从excel文件中取出数据  try {   List list = impExcelData getData();                  //放进list中 用的是jxl         List<CityDay> sameList = cds sameList(list);//这里有不同

  //boolean isOk = cds checkExcel(list);   if (sameList size()> ) {    request setAttribute( sameList sameList);    request      setAttribute( echo         以下数据有重复 是否覆蓋 );    return mapping findForward( upload );   } else {

  cds add(list);//这里有不同   }   System out print( );  } catch (Exception e) {   // TODO Auto generated catch block   e printStackTrace();  }

设计模式23种还是26种 设计模

  另一种格式的excel文件对应的action是这样的

  

    ZhbTCityDayForecastService cds = new ZhbTCityDayForecastServiceImpl();/////////////////  ImpExcelData impExcelData = new ImpExcelData(filePath );  try {   List list = impExcelData getData();         List<ZhbTCityDayForecast> sameList = cds sameList(list);////////////////////   if (sameList size()> ) {    request setAttribute( sameList sameList);    request      setAttribute( echo         数据有重复 是否覆蓋 );    return mapping findForward( upload );   } else {

  cds add(list);///////////////////////////////   }   System out print( );  } catch (Exception e) {   // TODO Auto generated catch block   e printStackTrace();  }

      业务层:

     public interface CityDayService{  public List findByCityAndDate(String city Date date);  public void deleteSame(List excelDataList);  public void add(List excelDatalist);  public List sameList(List excelDatalist);    }    和     public interface ZhbTCityDayForecastService{  public List findByCityAndDate(String city Date 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 cdd findByCityAndDate(city date); }

  public void add(List excelDatalist) { Session session = HibernateSessionFactory getSession(); Transaction tran = session beginTransaction();  List<CityDay> newList = new ArrayList();  for (int i = ; i < excelDatalist size(); i++) {   HashMap map = (HashMap) excelDatalist get(i);   String strDate = map get( ) + ;   Date date;   try {    date = new SimpleDateFormat( yyyy MM dd ) parse(strDate);    CityDayDAO cdd = new CityDayDAOImpl();    CityDay cd = new CityDay();    cd setDate(date);    cd setCity(map get( ) + );    cd setPollutionIndeces(new Integer(map get( )+ ));    cd setMainPollutant(map get( ) toString());    cd setGrade(map get( ) toString());    cd setStatus(map get( ) toString());    cdd saveOrUpdate(cd);   } catch (ParseException e) {    tran rollback();    e printStackTrace();   }

  }    mit(); }

  public void deleteSame(List excelDataList) {

  CityDayDAO cdd = new CityDayDAOImpl();  boolean result = true;  List<CityDay> list = new ArrayList<CityDay>();  for (int i = ; i < excelDataList size(); i++) {   HashMap map = (HashMap) excelDataList get(i);   String city = map get( ) + ;   String strDate = map get( ) + ;   Date date;   try {    date = new SimpleDateFormat( yyyy MM dd ) parse(strDate);    List onelist = cdd findByCityAndDate(city date);    for(int j= ;j<onelist size();j++){     list add((CityDay)onelist get(j));    }   } catch (ParseException e) {    // TODO Auto generated catch block    e printStackTrace();   }

  }  for(int j= ;j<list size();j++){   CityDay cd = (CityDay)list get(j);   System out println( 要删除的数据有:n + city: +cd getCity()+ date: + cd getDate());  }  cdd delete(list); }

  public List<CityDay> sameList(List excelDatalist) {  CityDayDAO cdd = new CityDayDAOImpl();  boolean result = true;  List<CityDay> list = new ArrayList<CityDay>();  for (int i = ; i < excelDatalist size(); i++) {   HashMap map = (HashMap) excelDatalist get(i);   String city = map get( ) + ;   String strDate = map get( ) + ;   Date date;   try {    date = new SimpleDateFormat( yyyy MM dd ) parse(strDate);    List onelist = cdd findByCityAndDate(city date);    for(int j= ;j<onelist size();j++){     list add((CityDay)onelist get(j));    }   } catch (ParseException e) {    // TODO Auto generated catch block    e printStackTrace();   }

  }  for(int j= ;j<list size();j++){   CityDay cd = (CityDay)list get(j);   System out println( 重复的数据有:n + city: +cd getCity()+ date: + cd getDate());  }  return list; }

  }    和

  public class ZhbTCityDayForecastServiceImpl implements ZhbTCityDayForecastService {

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

  public void add(List excelDatalist) { Session session = HibernateSessionFactory getSession(); Transaction tran = session beginTransaction();  List<CityDay> newList = new ArrayList();  for (int i = ; i < excelDatalist size(); i++) {   HashMap map = (HashMap) excelDatalist get(i);   String strDate = map get( )+ +map get( )+ +map get( ) + ;   Date date;   try {    date = new SimpleDateFormat( yyyy MM dd ) parse(strDate);    ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();    ZhbTCityDayForecast cd = new ZhbTCityDayForecast();    cd setCity(map get( ) + );    cd setDate(date);    cd setPollutionMin(new Integer(map get( )+ ));    cd setPollutionMax(new Integer(map get( )+ ));    cd setMainPollutant(map get( )+ );    cd setGrade(map get( ) toString());    cd setStatus(map get( ) toString());    cdd saveOrUpdate(cd);   } catch (ParseException e) {    tran rollback();    e printStackTrace();   }

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

  ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();  boolean result = true;  List<ZhbTCityDayForecast> list = new ArrayList<ZhbTCityDayForecast>();  for (int i = ; i < excelDataList size(); i++) {   HashMap map = (HashMap) excelDataList get(i);   String city = map get( ) + ;   String strDate = map get( )+ +map get( )+ +map get( ) + ;   Date date;   try {    date = new SimpleDateFormat( yyyy MM dd ) parse(strDate);    List<ZhbTCityDayForecast> onelist = cdd findByCityAndDate(city date);    for(int j= ;j<onelist size();j++){     list add((ZhbTCityDayForecast)onelist get(j));    }   } catch (ParseException e) {    // TODO Auto generated catch block    e printStackTrace();   }

  }  cdd delete(list); }

  public List<ZhbTCityDayForecast> sameList(List excelDatalist) {  // TODO Auto generated method stub  ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();  List<ZhbTCityDayForecast> list = new ArrayList<ZhbTCityDayForecast>();  for (int i = ; i < excelDatalist size(); i++) {   HashMap map = (HashMap) excelDatalist get(i);   String city = map get( ) + ;   String strDate = map get( )+ +map get( )+ +map get( ) + ;   Date date;   try {    date = new SimpleDateFormat( yyyy MM dd ) parse(strDate);    List onelist = cdd findByCityAndDate(city date);       for(int j= ;j<onelist size();j++){     list add((ZhbTCityDayForecast)onelist get(j));    }   } catch (ParseException e) {    // TODO Auto generated catch block    e printStackTrace();   }  }  return list; } }

  这样做完后 自己感觉很不满意 因为两个相似的功能 写了两遍代码:两个action service 还有两个页面 很笨     如果以后需求增加 再写其它的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 city Date date);   }

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

  实现类不用变

  工厂方法  public class UploadServiceFactory {

  private static CommonUploadService cus;

  private static final String cityDayService = xxx datacenter upload service CityDayServiceImpl ;

  private static final String foreCastService = xxx datacenter upload forecast service CityDayServiceImpl ;

  private UploadServiceFactory() { }

  public static CommonUploadService getInstance(String type) {  try {   if ( daily equals(type)) {    Class cls = Class forName(cityDayService);    cus = (CommonUploadService) cls newInstance();   }   if ( forecast equals(type)) {    Class cls = Class forName(foreCastService);    cus = (CommonUploadService) cls newInstance();   }  } catch (Exception e) {   e printStackTrace();  }  return cus; }  }

  改造后的action    // CityDayService cds = new CityDayServiceImpl();//这里有不同 可以用工厂方法 根据参数type不同构造出     String type = request getParameter( type );     CommonUploadService cds =  UploadServiceFactory getInstance(type);  ImpExcelData impExcelData = new ImpExcelData(filePath );  try {   List list = impExcelData getData();         List<CityDay> sameList = cds sameList(list);//这里有不同

  //boolean isOk = cds checkExcel(list);   if (sameList size()> ) {    request setAttribute( sameList sameList);    request      setAttribute( echo         以下数据有重复 是否覆蓋 );    return mapping findForward( upload );   } else {

  cds add(list);//这里有不同   }   System out print( );  } catch (Exception e) {   // TODO Auto generated catch block   e printStackTrace();  }

  这样一个action就可以搞定了     如果以为要增加新格式的excel文件 只实现CommonUploadService方法 改造一下工厂类就ok了

lishixinzhi/Article/program/Java/gj/201311/27596  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码