为别人提供方法工具类的时候传递参数有什么特别的要求吗?
关键字: 技术疑问昨天花时间为别人写了个工具类,作用就是生成Rss文件,具体代码如下:
public class RssBuilder {
private SyndFeed feed;
private List entries;
private SyndEntry entry ;
public RssBuilder(){
feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
entries = new ArrayList();
}
public void createChannelImage(String title,String link,String url,String description) throws Exception{
SyndImage image = new SyndImageImpl();
image.setTitle(title);
image.setLink(link);
image.setUrl(url);
image.setDescription(description);
feed.setImage(image);
}
/**
* 创建一个频道
* @param title 频道标题
* @param link 频道对应的连接
* @param description 频道描述
* @param language 频道所用语言
* @param pubDate 频道发布时期
* @param copyright 版权所有
* @throws Exception
*/
public void createChannel(String title,String link,String description,String language,Date pubDate,String copyright) throws Exception{
feed.setTitle(title);
feed.setLink(link);
feed.setDescription(description);
feed.setLanguage(language);
feed.setPublishedDate(pubDate);
feed.setCopyright(copyright);
}
/**
* 添加新闻子项
* @param title 标题
* @param link 连接地址
* @param description 简单描述
* @param pubDate 发布日期
* @param category 所属范围
* @param author 发布作者
* @throws Exception
*/
public void createItems(String title,String link,String description,Date pubDate,String category,String author) throws Exception {
entry = new SyndEntryImpl();
//设置新闻标题
entry.setTitle(title);
//设置新闻的连接地址
entry.setLink(link);
//设置新闻简介
SyndContent content = new SyndContentImpl();
content.setType("text/plain");
content.setValue(description);
entry.setDescription(content);
//设置发布时间
entry.setPublishedDate(pubDate);
//设置频道所属的范围
SyndCategory cate = new SyndCategoryImpl();
cate.setName(category);
List cateList = new ArrayList();
cateList.add(cate);
entry.setCategories(cateList);
//设置作者
entry.setAuthor(author);
//将新闻项添加至数组中
entries.add(entry);
}
/**
* 添加新闻子项
* @param title 标题
* @param link 连接地址
* @param description 简单描述
* @param pubDate 发布日期
* @param category 所属范围
* @param author 发布作者
* @param enclosure 流媒体播放文件地址
* @throws Exception
*/
public void createItems(String title,String link,String description,Date pubDate,String category,String author,String enclosure) throws Exception {
entry = new SyndEntryImpl();
//设置新闻标题
entry.setTitle(title);
//设置新闻的连接地址
entry.setLink(link);
//设置新闻简介
SyndContent content = new SyndContentImpl();
content.setValue(description);
entry.setDescription(content);
//设置发布时间
entry.setPublishedDate(pubDate);
//设置频道所属的范围
SyndCategory cate = new SyndCategoryImpl();
cate.setName(category);
List cateList = new ArrayList();
cateList.add(cate);
entry.setCategories(cateList);
//设置作者
entry.setAuthor(author);
//设置流媒体播放文件
SyndEnclosure en = new SyndEnclosureImpl();
en.setUrl(enclosure);
List enList = new ArrayList();
enList.add(en);
entry.setEnclosures(enList);
//将新闻项添加至数组中
entries.add(entry);
}
/**
* 生成XML文件
* @param filePath 文件保存路径和名称
* @throws Exception
*/
public void buildChannel(String filePath) throws Exception {
feed.setEntries(entries);
SyndFeedOutput output = new SyndFeedOutput();
Writer writer;
writer = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
output.output(feed, writer);
}
}
让我们的项目负责人看了,他要我把createItems()方法中的多个参数再封装一个类,让用户传的时候只传一个包含这个类的List对象,想来想去就是不明白,为什么要这样呢?
按照我现在的写法的话,别人用的时候也不用再写什么特别的类啦,只用把自己从数据库里查询出来的结果的对象以参数的形式传递过来就可以啦,这样不就更简单啦。
为什么一定要传一个List对象呢?
写工具类的时候有什么特别的要求吗?
评论
你没有考虑到别人使用你这些方法的感受,人家要按照你给定的参数顺序,还要小心谨慎地生怕位置对错,你可以换位思考一下,我只将需要的参数塞给一个对象,然后将这个对象传给调用的方法,具体怎么处理那就是接口作者的问题了。
你想想一个对象传过来,你还用写那么多重载方法么?对己对人其实都方便,另外 如果用的是框架技术从数据库直接调出个对象出来,用你这个方法那就是更加糟糕至极了...
这个exception调用者知道怎么处理吗?
不知道的话,建议扔个RuntimeException,不要强制他去catch
这种思考方式有时候是脱裤子放屁,多此一举.
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 1624 次
- 性别:

- 来自: 郑州

- 详细资料
搜索本博客
最新评论
-
Dwr2+Struts2+Spring2.5+H ...
jackzhangyunjie 写道回复power_zl: 我看了你出现的错 ...
-- by power_zl -
Dwr2+Struts2+Spring2.5+H ...
回复power_zl: 我看了你出现的错误,这种错误一般情况下是因为你没有此 ...
-- by jackzhangyunjie -
Dwr2+Struts2+Spring2.5+H ...
严重: Context initialization failedorg.spr ...
-- by power_zl -
电子地图--MapABC研究( ...
呵呵,lz太言重了,我也是开玩笑而已。他们开放只是他们采集的城市基础数据,至于应 ...
-- by transist -
Dwr2+Struts2+Spring2.5+H ...
commons-fileupload-1.2.1.jar 这个可以不要吧!
-- by lijie250






评论排行榜