When there is a choice between referring to an object by their interface
or a class
, the former should be preferred, but only if an appropriate type exists.
Consider String
implements
CharSequence
as an example. You should not just blindly use CharSequence
in preferrence to String
for all cases, because that would deny you simple operations like trim()
, toUpperCase()
, etc.
However, a method that takes a String
only to care about its sequence of char
values should use CharSequence
instead, because that is the appropriate type in this case. This is in fact the case with replace(CharSequence target, CharSequence replacement)
in the String
class.
Another example is java.util.regex.Pattern
and its Matcher matcher(CharSequence)
method. This lets a Matcher
be created from Pattern
for not just String
, but also for all other CharSequence
there are out there.
A great example in the library of where an interface
should've been used, but unfortunately wasn't, can also be found in Matcher
: its appendReplacement
and appendTail
methods accept only StringBuffer
. This class has largely been replaced by its faster cousin StringBuilder
since 1.5.
A StringBuilder
is not a StringBuffer
, so we can not use the former with the append…
methods in Matcher
. However, both of them implements
Appendable
(also introduced in 1.5). Ideally Matcher
's append…
method should accept any Appendable
, and we would then be able to use StringBuilder
, as well as all other Appendable
available!
So we can see how when an appropriate type exists referring to objects by their interfaces can be a powerful abstraction, but only if those types exist. If the type does not exist, then you may consider defining one of your own if it makes sense. In this Cat
example, you may define interface SelfBathable
, for example. Then instead of referring to a Cat
, you can accept any SelfBathable
object (e.g. a Parakeet
)
If it does not make sense to create a new type, then by all means you can refer to it by its class
.
See also
- Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces
If appropriate interface types exist, then parameters, return values, and fields should all be declared using interface types. If you get into the habit of using interface types, your program will be much more flexible. It is entirely appropriate to refer to an object by a class if no appropriate interface exists.
Related links
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…