Substitution Principle
←Older revision | Revision as of 05:45, 24 September 2022 | ||
Line 35: | Line 35: | ||
=== Substitution Principle === | === Substitution Principle === | ||
- | |||
When writing an [[API]], the term ''subclass'' shouldn’t be used for someone who can ''participate | When writing an [[API]], the term ''subclass'' shouldn’t be used for someone who can ''participate | ||
in a switch'', but for someone who ''behaves exactly like me'', plus adds some additional behavior. | in a switch'', but for someone who ''behaves exactly like me'', plus adds some additional behavior. | ||
- | True, many would agree that a '''Human''' is a subclass (a specialization) of a '''Mammal'''. | + | True, many would agree that a '''Human''' is a subclass (a specialization) of a '''Mammal'''. |
- | + | Nice, but simply exposing a [[DeepHierarchy]] of classes is unlikely to improve an [[API]]’s usability magically. | |
- | Especially [[GUI]] toolkits got this all wrong. | + | Especially [[GUI]] toolkits (full of widgets inheriting from each other) got this all wrong. |
They like to claim that a {{JDK|javax.swing|JFrame}} is a specialization of {{JDK|javax.swing|JComponent}}, | They like to claim that a {{JDK|javax.swing|JFrame}} is a specialization of {{JDK|javax.swing|JComponent}}, | ||
but try to use {{JDK|javax.swing|JFrame}} where you can use {{JDK|javax.swing|JComponent}}! It is going to fail. | but try to use {{JDK|javax.swing|JFrame}} where you can use {{JDK|javax.swing|JComponent}}! It is going to fail. | ||
- | That is not a failure of the usage, but a failure of the | + | That is not a failure of the usage, but a failure of the [[API]] designer! |
== Do Not Expose Deep Hierarchies! == | == Do Not Expose Deep Hierarchies! == |