问题描述
根据接口隔离原则
...因此我们应该定义接口以使其具有逻辑分隔.
...and so we should define interfaces to have logical separation.
但是Java 8中引入的default
方法提供了在Java接口中实现方法的灵活性.看来Java 8提供了增强接口的可行性,使其具有一些与其核心逻辑无关的方法,但是具有一些默认或空的实现.
But default
methods introduced in Java 8 have provided the flexibility to implement methods in Java interfaces. It seems Java 8 has provided the feasibility to enhance an interface to have some methods not related to its core logic, but with some default or empty implementation.
它不违反ISP吗?
推荐答案
好问题.当然,它违反了接口隔离原则,而且我个人不喜欢默认实现的概念,因为它破坏了接口设计的美感,并且破坏了精确的多态性.如果有人不了解ISP的概念,那么他们将开始设计胖接口并最终像包装在一个接口中的所有东西一样结束.在代码设计期间,人们不会逻辑地思考.
Good question. Definitely, it violates Interface Segregation Principle and I personally don't like the concept of default implementation because it spoils the beauty of interface design and a bit on exact polymorphism as well. If somebody is not aware of the concept of ISP then they will start design fat interfaces and will end up like everything packed in one interface. During code design, people will not think logically as well.
这将最终导致代码异味,而且我确信那些不了解这些概念的人将开始编写错误的代码.我认为默认实现是一个不需要的功能,因为它会使人们编写臭代码.
This will end up with code smells and I am sure those who don't know the concepts will start writing bad code. I believe default implementation is an unwanted feature as it will tend people to write smelly code.
这篇关于接口隔离原则和Java 8中的默认方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!