实现多个接口是否违反单一责任原则? [英] Does implementing multiple interfaces violate Single Responsibility Principle?

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

问题描述

来自维基百科:

单一责任原则规定,每个班级应有一个 单一责任,而该责任应完全 由类封装.

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中的接口可以用来说出类具有什么属性(例如,ComparableSerializable),但是并不能真正说明类的职责.

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天全站免登陆