Interface Segregation Principle Concerns
Closed this issue · 1 comments
=== THIS ISSUE IS POSTED AS A COLLEGE ASSIGNMENT ON SOLID DESIGN PRINCIPLES, PLEASE CLOSE THE
ISSUE IF YOU FIND IT TO BE USELESS. ===
- src/main/java/com/amihaiemil/eoyaml/YamlLine.java
Concern:
In this case, the contents, comment, and requireNestedIndentation methods are concrete (not abstract) methods in the YamlLine interface, but they may not be necessary for all implementations.
Solution:
To improve this, you might consider splitting the interface into smaller, more specific interfaces. For example, you could have a main YamlLine interface with the common methods, and then create additional interfaces such as YamlLineContent, YamlLineComment, and YamlLineIndentation that contain specific methods.
Code:
public interface YamlLine extends Comparable<YamlLine> {
String value();
String trimmed();
int number();
int indentation();
}
public interface YamlLineContent {
String contents(int previousIndent);
}
public interface YamlLineComment {
String comment();
}
public interface YamlLineIndentation {
boolean requireNestedIndentation();
}
public class ConcreteYamlLine implements YamlLine, YamlLineContent, YamlLineComment, YamlLineIndentation {
// Implement the interface methods
}
- src/main/java/com/amihaiemil/eoyaml/YamlSequence.java
Concern:
The YamlSequence interface could be violating the interface segregation principle. Some of the methods, such as string, foldedBlockScalar, literalBlockScalar, integer, floatNumber, doubleNumber, longNumber, date, and dateTime, may not be relevant or necessary for all implementations of this interface.
Solution:
Split the interface into smaller, more specific interfaces. For example, you could have a main YamlSequence interface with the common methods, and then create additional interfaces like YamlSequenceScalar, YamlSequenceNumber, YamlSequenceDateTime, etc., that contain specific methods.
Code:
public interface YamlSequence extends YamlNode, Iterable<YamlNode> {
Collection<YamlNode> values();
int size();
YamlMapping yamlMapping(int index);
YamlSequence yamlSequence(int index);
}
public interface YamlSequenceScalar {
String string(int index);
String foldedBlockScalar(int index);
Collection<String> literalBlockScalar(int index);
}
public interface YamlSequenceNumber {
int integer(int index);
float floatNumber(int index);
double doubleNumber(int index);
long longNumber(int index);
}
public interface YamlSequenceDateTime {
LocalDate date(int index);
LocalDateTime dateTime(int index);
}
public class ConcreteYamlSequence implements YamlSequence, YamlSequenceScalar, YamlSequenceNumber, YamlSequenceDateTime {
// Implement interface methods
}
- src/main/java/com/amihaiemil/eoyaml/YamlStream.java
Concern:
By extending the Stream interface, you are inheriting a large number of methods from the Stream interface that may not be necessary or applicable to the specific YamlStream context.
The Stream interface is quite extensive and has many methods that may not make sense or be redundant in the context of a YAML stream representation. Additionally, the YamlStream interface already defines methods like values(), iterator(), and comment() that provide specific functionality for working with YAML nodes, which could make some of the methods inherited from Stream unnecessary.
Solution:
create smaller, more specific interfaces that focus on operations relevant to YAML flows. For example, you could have interfaces like YamlStreamFiltering, YamlStreamMapping, YamlStreamReducing, etc., that provide specific and relevant methods for those operations.
Code:
public interface YamlStream extends YamlNode {
Collection<YamlNode> values();
Comment comment();
}
public interface YamlStreamFiltering {
YamlStream filter(Predicate<? super YamlNode> predicate);
// Other specific methods if necessary
}
public interface YamlStreamMapping {
YamlStream map(Function<? super YamlNode, ? extends YamlNode> mapper);
// Otros métodos de mapeo específicos si es necesario
}
public interface YamlStreamReducing {
YamlNode reduce(YamlNode identity, BinaryOperator<YamlNode> accumulator);
// Otros métodos de reducción específicos si es necesario
}
// Otras interfaces específicas según las operaciones necesarias
public class ConcreteYamlStream implements YamlStream, YamlStreamFiltering, YamlStreamMapping, YamlStreamReducing {
// Implementaciones de los métodos de las interfaces
}
Diagramas UML:
It is very difficult to change the interface structure for libraries, particularly because these are the public API facing the users. Any change to the interfaces will break backwards compatibility and we try to avoid that at all costs.
YamlLine
, on the other hand, is an internal detail which could be changed, but we intend to deprecate the current implementation for reading and replace it with ANTLR - thus, we will not need the concept of YamlLine anymore.
Thank you, but I will close this issue.