实施多重接口违反单一责任原则 [英] Does implementing multiple interface violate Single Responsibility Principle

查看:92
本文介绍了实施多重接口违反单一责任原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

维基百科

单一责任原则声明每个班级都应该有一个责任,这个责任应该完全由班级封装。

"Single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class."

这是否意味着实现多个接口违反了这一原则?

Does that mean implementing multiple interfaces violates this principle?

推荐答案

我不会自己说。一个课程可以有一个责任,但在这个过程中可以做多个事情,并为每一件事情实现一个接口,以履行其责任。

I would say not by itself. A class can have one responsibility, but do multiple things in the process, and implement one interface for each set of things it needs to do to fulfill its responsibility.

另外, Java中的接口可用于说明该类具有什么属性(例如, Comparable 可序列化)的内容,但是没有真正地说任何类的责任。

Also, interfaces in Java can be used to say things about what properties the class has (for example, Comparable and Serializable), but not really say anything the class's responsibility.

但是,如果一个类实现多个接口,每个接口对应一个责任,那么那么将违反这一原则。

However, if a class implements multiple interfaces, each of which corresponds to one responsibility, then that would be a violation of that principle.

这篇关于实施多重接口违反单一责任原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆