Skip to main content
deleted 9 characters in body
Source Link
JacquesB
  • 62.4k
  • 21
  • 137
  • 190

It does not violationviolate the Liskov Substitution Principle. The LSP states that a subtype should not violate guarantees given by a supertype. But the List interface explicitly states that the methods add(), remove() etc. are optional and may throw this exception. So LSP is not broken by subtypes throwing this exception, even though the "principle of least astonishment" is obviously broken.

It is not obvious to what extent the Interface Segregation Principle can be applied to general-purpose library classes, since we don't know what subsets of an interface clients might want to use. The principle is best applied in application code when we see that different clients need different subsets of the interface of the same class. In the case of List it would require sub-typing it in your own application code in order to segregate the interface.

That said, the existence of the optional methods does indicate that the interface is not fine-grained enough. Having a specific UnmodifiableList interface would make it much clearer in client code what contract is expected and void avoid the possibility of runtime exceptions for unsupported methods.

It does not violation the Liskov Substitution Principle. The LSP states that a subtype should not violate guarantees given by a supertype. But the List interface explicitly states that the methods add(), remove() etc. are optional and may throw this exception. So LSP is not broken by subtypes throwing this exception, even though the "principle of least astonishment" is obviously broken.

It is not obvious to what extent the Interface Segregation Principle can be applied to general-purpose library classes, since we don't know what subsets of an interface clients might want to use. The principle is best applied in application code when we see that different clients need different subsets of the interface of the same class. In the case of List it would require sub-typing it in your own application code in order to segregate the interface.

That said, the existence of the optional methods does indicate that the interface is not fine-grained enough. Having a specific UnmodifiableList interface would make it much clearer in client code what contract is expected and void avoid the possibility of runtime exceptions for unsupported methods.

It does not violate the Liskov Substitution Principle. The LSP states that a subtype should not violate guarantees given by a supertype. But the List interface explicitly states that the methods add(), remove() etc. are optional and may throw this exception. So LSP is not broken by subtypes throwing this exception, even though the "principle of least astonishment" is obviously broken.

It is not obvious to what extent the Interface Segregation Principle can be applied to general-purpose library classes, since we don't know what subsets of an interface clients might want to use. The principle is best applied in application code when we see that different clients need different subsets of the interface of the same class. In the case of List it would require sub-typing it in your own application code in order to segregate the interface.

That said, the existence of the optional methods does indicate that the interface is not fine-grained enough. Having a specific UnmodifiableList interface would make it much clearer in client code what contract is expected and avoid the possibility of runtime exceptions for unsupported methods.

added 350 characters in body
Source Link
JacquesB
  • 62.4k
  • 21
  • 137
  • 190

It doesn't strictly speaking violate any of the SOLID principles. Which just goes to show the limitations ofdoes not violation the SOLID principles.

The Liskov Substitution Principle. The LSP states that a subtype should not violate guarantees given by a supertype. But the List interface explicitly states that the methods add(), remove() etc. are optional and may throw this exception. So LSP is not broken by subtypes throwing this exception, even though the "principle of least astonishment" is obviously broken.

TheIt is not obvious to what extent the Interface Segregation principlePrinciple can be applied to general-purpose library classes, since we don't know what subsets of an interface clients might seem like itwant to use. The principle is violated, butbest applied in fact there is aapplication code when we see that different clients need different subsets of the interface, Collection, which represent a collection without of the mutability operationssame class. A client which does not expectIn the case of List it would require sub-typing it in your own application code in order to mutatesegregate the list can use this interface instead. So this principle

That said, the existence of the optional methods does indicate that the interface is not broken eitherfine-grained enough. Having a specific UnmodifiableList interface would make it much clearer in client code what contract is expected and void avoid the possibility of runtime exceptions for unsupported methods.

It doesn't strictly speaking violate any of the SOLID principles. Which just goes to show the limitations of the SOLID principles.

The Liskov Substitution Principle states that a subtype should not violate guarantees given by a supertype. But the List interface explicitly states that the methods add(), remove() etc. are optional and may throw this exception. So LSP is not broken by subtypes throwing this exception, even though the "principle of least astonishment" is obviously broken.

The Interface Segregation principle might seem like it is violated, but in fact there is a different interface, Collection, which represent a collection without the mutability operations. A client which does not expect to mutate the list can use this interface instead. So this principle is not broken either.

It does not violation the Liskov Substitution Principle. The LSP states that a subtype should not violate guarantees given by a supertype. But the List interface explicitly states that the methods add(), remove() etc. are optional and may throw this exception. So LSP is not broken by subtypes throwing this exception, even though the "principle of least astonishment" is obviously broken.

It is not obvious to what extent the Interface Segregation Principle can be applied to general-purpose library classes, since we don't know what subsets of an interface clients might want to use. The principle is best applied in application code when we see that different clients need different subsets of the interface of the same class. In the case of List it would require sub-typing it in your own application code in order to segregate the interface.

That said, the existence of the optional methods does indicate that the interface is not fine-grained enough. Having a specific UnmodifiableList interface would make it much clearer in client code what contract is expected and void avoid the possibility of runtime exceptions for unsupported methods.

Source Link
JacquesB
  • 62.4k
  • 21
  • 137
  • 190

It doesn't strictly speaking violate any of the SOLID principles. Which just goes to show the limitations of the SOLID principles.

The Liskov Substitution Principle states that a subtype should not violate guarantees given by a supertype. But the List interface explicitly states that the methods add(), remove() etc. are optional and may throw this exception. So LSP is not broken by subtypes throwing this exception, even though the "principle of least astonishment" is obviously broken.

The Interface Segregation principle might seem like it is violated, but in fact there is a different interface, Collection, which represent a collection without the mutability operations. A client which does not expect to mutate the list can use this interface instead. So this principle is not broken either.