JAVA-关于泛型接口的疑惑

Hacreal 2018-12-14 09:42:22
在结构化设计中,好的设计模式需满足:高内聚,低耦合

public interface IAService {

// 与IBService接口中相似的方法
public List<A> findAll();

public A findByPrimaryKey(Integer key);

public Integer insert(A pojo);

public Integer updateByPrimaryKey(A pojo);

public Integer deleteByPrimaryKey(Integer key);

}





public interface IBService {

// 与IAService接口中相似的方法
public List<B> findAll();

public B findByPrimaryKey(Integer key);

public Integer insert(B pojo);

public Integer updateByPrimaryKey(B pojo);

public Integer deleteByPrimaryKey(Integer key);
}


A的Service接口和B的Service接口中存在相似的方法,后续会有C的Service又存在相似的方法,改进后如下:

public interface IServiceBean<T> {

public List<T> findAll();

public T findByPrimaryKey(Object key);

public Integer insert(T pojo);

public Integer updateByPrimaryKey(T pojo);

public Integer deleteByPrimaryKey(Object key);
}




public interface IAService extends IServiceBean<A>{

// 定义A的Serivce自己特有的业务方法
}




public interface IBService extends IServiceBean<B> {

// 定义B的Serivce自己特有的业务方法
}

这样写是否存在弊处? 是否符合 高内聚,低耦合?
...全文
256 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Saleayas 2018-12-14
  • 打赏
  • 举报
回复
我不建议这么写,接口是协议,如果他们是独立的, 那么就不要有牵连。 接口只是编程语言的对协议的描述。 因为这是需要向别人公开的。 但是对于你的这种接口,我认为是设计上的错误造成的。 应该把 ABC 和 IService 分开。 也就是说把 ABC 用接口统一起来,比如 使用 I。 然后,使用 I 来定义 Service。
十八道胡同 2018-12-14
  • 打赏
  • 举报
回复
如果我写, 我也会这么写的。

51,412

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧