Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Violation of single responsibility principle in Iterator from Java core

Why java.util.Iterator interface has method remove()?

Certainly sometimes this method is necessary and all have become accustomed to its presence. But in fact the main and only objective of the iterator is just to provide access container elements. And when someone wants to create his own implementation for this interface, and cannot or does not want for any reason to provide the ability to remove an element, then he is forced to throw UnsupportedOperationException. And throwing of that exception usually indicates a not too well thought out architecture or some flaws in design.

Really I don't understand the reasons for such a decision. And I guess it would be more correctly separate a specific subinterface to support the optional method:

diagram

Any reasoned versions why remove() is part of the Iterator? Is not this example of a direct violation of the single responsibility principle from SOLID?

like image 943
kapandron Avatar asked Oct 15 '15 23:10

kapandron


People also ask

What is single responsibility principle in Java?

As the name suggests, this principle states that each class should have one responsibility, one single purpose. This means that a class will do only one job, which leads us to conclude it should have only one reason to change.

What is the issue with the single responsibility principle?

A class should have only one reason to change. A common misconception about this principle is that people think it means that a class should do only one thing. This is not the case: a class can do multiple things. If not, you'd end up with classes that only have a single public method.

Could you provide an example of the single responsibility principle?

A class should have only one reason to change. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one responsibility and if in the future we need to make one change we are going to make it in the class which handles it.

Which of the following is the valid reason for the class change under the single responsibility principle?

The Principle The Single Responsibility Principle (SRP) states that there should never be more than one reason for a class to change. This means that every class, or similar structure, in your code should have only one job to do. Everything in the class should be related to that single purpose.


3 Answers

In addition to fancy technical answers ... please consider the timeline too. The "single responsibility principle" was coined by Robert Martin at some point in the middle/late 90es.

The Java iterator interface came into existence with Java 1.2; so around 1998.

It is very much possible that the folks at Sun had never heard of this concept while working on the early releases of Java.

Of course, many smart people have the same ideas without reading a book about it ... so a good designer might have implemented "SRP" like without knowing about "SRP" - but it also requires a high degree of awareness to unveil all the big and small violations of this rule ...

like image 107
GhostCat Avatar answered Nov 09 '22 01:11

GhostCat


This design decision is explained in the Java Collections API Design FAQ. Specifically, see the first question on why collections don't support immutability and instead require optional operations. The short answer is that they didn't want "an explosion" in the number of interfaces.

like image 42
jaco0646 Avatar answered Nov 09 '22 02:11

jaco0646


There seems to be a mix-up of semantics here. Robert C. Martin defines Single Responsibility as a "single reason to change" (SRP.pdf), not as "doing only a single thing". SRP is very much related with cohesion: a software module should only contain things that are functionally related to each other.

With these things in mind, I don't think that having the remove method included in Iterator violates the SRP. Removing an element is often something you might want to do while iterating over the elements; the operations are essentially cohesive. Also, enabling the removal of elements through Iterator makes the Iterable interface (which was added in Java 5) much more powerful. This feature is utilized in e.g. many of the methods in Guava's Iterables utility class.

More info on the history of the term in this excellent article by Uncle Bob himself.

like image 28
Mick Mnemonic Avatar answered Nov 09 '22 03:11

Mick Mnemonic