老大,Service层需要接口吗?
往期热门文章:
作者:架构思维
来源:https://www.toutiao.com/i6882356844245975563
前几天刷头条又刷到了「Service层和Dao层真的有必要每个类都加上接口吗?」这个问题,之前简单回答了一波,给出的观点是「看情况」
- 可以在尚未实现具体Service逻辑的情况下编写上层代码,如Controller对Service的调用
- Spring默认是基于动态代理实现AOP的,动态代理需要接口
- 可以对Service进行多实现
- 先在Controller层编写逻辑,遇到需要委托Service调用的地方,直接先写出调用代码。
- 优先完成Controller层的流程
- 然后使用IDE的自动补全,对刚才调用下层的代码生成对应的类和方法,在里面添加TODO
- 等所有的类和方法都补全了,再基于TODO,按照上面的流程去一个个的完善逻辑。
- 此方法可以使你对业务流程有比较好的理解。
- Controller
- Service
- Dao
Controller Service --- 接口在一个包中 impl --- 实现在另一个包里 Dao
- Controller
- Service
- ---- 接口在一个包中
- impl ---实现在另一个包里
- impl2 ---新实现在另一个包里
- Dao
- Controller
- Service
- ---- 接口在一个包中
- impl ---实现在另一个包里
- Service2
- impl2 ---新实现在另一个包里
- Dao
- Controller
- Service --- 接口模块
- ServiceImpl
- impl ---实现在另一个包里
- ServiceImpl2
- impl2 ---新实现在另一个包里
- Dao
- Controller
- Service --- 接口模块
- ServiceImpl
- impl ---实现在另一个包
- ServiceImpl2
- impl ---新实现和老实现在相同的包中
- Dao
Controller Service1 --- 老实现 Service2 --- 新实现 Dao
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。