[img]http://p.blog.csdn.net/images/p_blog_csdn_net/iamzealotwang/426157/o_pic1.jpg[/img]
[img]http://s17.divshare.com/files/2008/07/10/4917109/pic1.jpg[/img]
图片要是看不到,请刷新尝试一下,因为实在找不到好的外链空间。。。
在业务层定义了2个Server
CatalogService
和
UserService
这两个都好理解,问题出在
servicelocator 这个server 上面
public interface ServiceLocator
Interface needs to be implemented by ServiceLocator.
ServiceLocator is used to lookup for business services.
Method Summary
getCatalogService()
Get the CatalogService.
getUserService()
Get the UserService.
我想问一下定义这个servicelocator有什么用处么?
比如
UserBean里面的loginAction()
调用还需要:
User user = this.serviceLocator.getUserService().login(this.username, this.password);
要是不要servicelocator的话 直接this.userservice.login(this.username, this.password);不是挺好的么?
你说要是为了以后的扩充用,可是里面定义的接口就两个
getCatalogService() 和 getUserService() 感觉完全没有用处啊。
[b]问题补充:[/b]
====================补充========================
您好,您的意思我明白。
我是不是可以这样理解,如果使用了loc 这个Server也就没有存在的意义了,是这个样子的么?
要是这样的话,是使用loc好一些 还是手工编写这个Server好呢?
我看的是Put_JSF_ToWork 在他里面给出了一个ServiceLocator的impl 实现了
您说的那个方法
public Object lookupService(String serviceBeanName) {
return appContext.getBean(serviceBeanName);
}
按照这个函数,加入我添加了一个ServerA ,其中有一个方法B()
那我在调用的时候
A a = this.serviceLocator.lookupService(ServerA).####
那块####应该怎么写呢?
谢谢。
其实, 想法是好的, 就是统一管理获取具体功能类的方式.
假如说: UserService的实现是RMI的, 那么, 就可以将具体查找的逻辑隐藏在ServiceLocator里面了.
还有, 你说直接的this.userservice, 这样做的前提是你依旧要去找到这个userService实例. [或者由Ioc容器帮你注入了, 不用再自己做手动的查找.] 如果,你要手动查找, 最佳实践还是会统一由一个ServiceLocator去查找的.
还有, 如果我来设计这个ServiceLocator的话. 那它应该是下面这样的:
代码随手写的
[code="java"]
public class ServiceLocator {
public static T getService(Class service) {
// ...
}
}[/code]
对外就提供一个getService方法, 而不是罗列那么多的get**方法.