Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Design Pattern & UML  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 DAO设计,测试的问题
gaoxt1983





发贴: 67
积分: 0
于 2006-11-24 08:52 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
DAO真的有必要设计成接口吗?还有Service,为什么啊?别告诉我是为了以后换实现方便,有多少直接换实现的情况……
java 代码
public interface LoginService
{
public User getUser(String userName,String password) throws LoginException;
public setLoginDao(LoginDao arg);
public setIpBannerDao(IpBannerDao arg);
}



比如:

java 代码
public class LoginServiceImpl implements LoginService
{
public User login(String userName,String password) throws LoginException
{
User user=userDao.getUser(userName,password);
if(user==null)
{
throw new LoginException("你丫不是输错用户名就是记错密码了");
}
if(ipBannerDao.checkUser(user)==false)
{
throw new LoginException("靠,被banner了还敢来,不想活了?");
}
return user;
}

}

那么,把DAO,Service写成接口的好处是什么?

现在,我想测试LoginService,那么我需要模拟实现两个DAO,写两个对应的Mock,还有一个testCase,可是,如果DAO多了的话,那么,我们就需要为一个Service些5,6个DAO,真的有这个必要吗?



还有,如何写这些Mock Dao?测试数据怎么注入DAO中?



作者 Re:DAO设计,测试的问题 [Re:gaoxt1983]
zhangzhaofeng





发贴: 1
积分: 0
于 2007-01-26 15:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
建议接口和抽象类结合使用


作者 Re:DAO设计,测试的问题 [Re:gaoxt1983]
Jacob53





发贴: 6
积分: 0
于 2008-02-12 18:00 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在项目前期,可以先把接口写出来,然后用这些接口为程序建框架,最后用实类把内容填进去。

当然这是二十世纪的应用程序式设计方法,虽然现在还有很多顽固分子用这种方法,但现在的MVC式的设计方法已经不大需要接口。



作者 Re:DAO设计,测试的问题 [Re:gaoxt1983]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2008-04-19 09:30 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
depends on your service.

For login service, my experience is yes, you need that, Smile, if you have tens of ways to verify them, user/password, nt login, digital certs, finger prints, face recognition, etc...

I have a dao interface so that users can specify in the runtime to output the result to console, database, file, special cache, or downstream targets.

Most important thing is that you have to think hard about the interface, the data you send across this interface. Using a Map on the interface is almost for sure is a bad idea because it makes the interface almost invisible at all.

For example,
User load(int userId);

or load(User user);

which one is better?

insertUser(int userId, String password, String name);

or

insert(User);

which one is better?

int getUserId();

should this be on the interface?

How about this:

User.load()

and how about

load(User[] users)



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2018 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号
客服电话 0592-8750026    客服信箱 714923@qq.com    客服QQ 714923