快捷搜索:

设计模式一些思索

近段要实现一个上传excel文件到办事器,并把excel文件的数据导入到数据库表中的功能。

excel文件有两种款式,对应数据库两张表。我先实现了此中一种excel款式式的上传和导入数据。上传的功能是一样的,没什么好说的。不合的是两种excel文件款式不一样,对应的数据库表的字段也不一样。怎么办呢?

开始的时刻,我先做了一个excel文件的功能。做另一个很相似了,运用c/p大年夜法就搞定了:)。

代码是这样的.

action中:

CityDayService cds = new CityDayServiceImpl();

ImpExcelData impExcelData = new ImpExcelData(filePath, 0);//这里从excel文件中掏出数据

try {

List list = impExcelData.getData();//放进list中.用的是jxl

List sameList = cds.sameList(list);//这里有不合

//boolean isOk = cds.checkExcel(list);

if (sameList.size()>0) {

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();

}

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

/* 导入表 */

ZhbTCityDayForecastService cds = new ZhbTCityDayForecastServiceImpl();/////////////////

ImpExcelData impExcelData = new ImpExcelData(filePath, 0);

try {

List list = impExcelData.getData();

List sameList = cds.sameList(list);////////////////////

if (sameList.size()>0) {

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 newList = new ArrayList();

for (int i = 1; ilist = new ArrayList();

for (int i = 1; isameList(List excelDatalist) {

CityDayDAO cdd = new CityDayDAOImpl();

boolean result = true;

List list = new ArrayList();

for (int i = 1; inewList = new ArrayList();

for (int i = 1; ilist = new ArrayList();

for (int i = 1; ionelist = cdd.findByCityAndDate(city, date);

for(int j=0;j sameList(List excelDatalist) {

// TODO Auto-generated method stub

ZhbTCityDayForecastDAO cdd = new ZhbTCityDayForecastDAOImpl();

List list = new ArrayList();

for (int i = 1; i

这样做完后,自己感到很不知足.由于两个相似的功能,写了两遍代码:两个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 = "com.xxx.datacenter.upload.service.CityDayServiceImpl";

private static final String foreCastService = "com.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, 0);

try {

List list = impExcelData.getData();

List sameList = cds.sameList(list);//这里有不合

//boolean isOk = cds.checkExcel(list);

if (sameList.size()>0) {

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了.

总结:

今后碰到功能类似的需求,可以把接口相同,但实现不合的措施提到共的提口来.然后用工厂措施天生不合的实例.

运用面向工具的多态的机能.就可以简化开拓.

设计模式关暂在于实践.光看书籍是不可的.有什么纰谬的地方,请多多辅导.

您可能还会对下面的文章感兴趣: