V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
cheava
V2EX  ›  问与答

Java 项目中的 service 分层和工程结构

  •  1
     
  •   cheava · 2018-07-05 08:51:00 +08:00 · 2350 次点击
    这是一个创建于 2114 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问下大家关于工程上的问题: service 层有没有必要分成接口和 impl ?

    (项目没有对外系统提供服务)

    我以前也是一直有分的,但是就以前项目来说,也确实感觉没有什么用处

    能举一些例子说明吗?可能做的项目不是很大型,所以没感觉

    15 条回复    2018-07-06 08:41:19 +08:00
    zjp
        1
    zjp  
       2018-07-05 09:00:52 +08:00 via Android
    如果用到了动态代理就必须分接口和实现,其他的情况下没有强制的要求
    micean
        2
    micean  
       2018-07-05 09:04:37 +08:00
    一般情况用不上
    但也就 ctrl+左键 2 次的事
    所以我不喜欢
    cheava
        3
    cheava  
    OP
       2018-07-05 09:07:21 +08:00
    @zjp #1 我能想到的也就是,aop 和事务管理需要
    VoidChen
        4
    VoidChen  
       2018-07-05 09:24:30 +08:00
    除了以上,补充一个,为了找方法的时候好看一点(直接看接口)
    ddbullfrog
        5
    ddbullfrog  
       2018-07-05 09:26:25 +08:00
    One of the main interests of using Spring is AOP. This is the technology that allows Spring to add new behaviours on top of your Beans: for instance, this is how transactions or security work.

    In order to add those behaviours, Spring needs to create a proxy on your class, and there are two ways of creating a proxy:

    If your class uses an interface, Spring will use a standard mechanism provided by Java to create a dynamic proxy.

    If your class doesn ’ t use an interface, Spring will use CGLIB to generate a new class on the fly: this is not a standard Java mechanism, but it works as well as the standard mechanism.
    ioc
        6
    ioc  
       2018-07-05 09:29:03 +08:00 via Android
    @ddbullfrog 意思就是没差咯
    misaka19000
        7
    misaka19000  
       2018-07-05 09:29:51 +08:00
    感觉没有用处就不分
    Cbdy
        8
    Cbdy  
       2018-07-05 09:30:48 +08:00
    不分,需要的时候再分。从实现抽象出接口不是一条命令的事情吗?不要过度设计
    cheava
        9
    cheava  
    OP
       2018-07-05 09:36:29 +08:00
    @Cbdy #8 我们目前内部定的规范就是,外部调用定接口,内部调用省略接口
    JRay
        10
    JRay  
       2018-07-05 09:45:31 +08:00
    我也有过这种想法,一直没理解到接口和 impl 的用处在哪儿
    swim2sun
        11
    swim2sun  
       2018-07-05 09:48:28 +08:00
    直接写成 class,如果有多个实现再抽象成接口也不晚啊,Java 重构起来很方便
    Guozi1989
        12
    Guozi1989  
       2018-07-05 09:53:57 +08:00
    我这边是强制要求写接口,虽然我个人是及不愿意。逃~
    li1215101
        13
    li1215101  
       2018-07-05 13:55:30 +08:00
    小项目不分,大项目分,方便看代码,Java service 的逻辑有时候写得比较乱,对外接口和逻辑搞一起很难看懂
    tairan2006
        14
    tairan2006  
       2018-07-05 16:30:16 +08:00 via Android
    没啥球用,真有需要再重构
    nl101531
        15
    nl101531  
       2018-07-06 08:41:19 +08:00 via Android
    重构的时候就会发现很实用了。另外接口的目的是制定协议,比较复杂的 service 我这边都会分开,比如订单流程,每一个类型订单都会有一个具体的 impl,而上层对外只需要一个接口就好了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3335 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:04 · PVG 20:04 · LAX 05:04 · JFK 08:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.