inheritance - is it acceptable to provide an API that is undefined a large part of the time? -
given type follows:
class thing { getinfo(); isremotething(); getremotelocation(); }
the getremotelocation()
method has defined result if isremotething()
returns true. given thing
s not remote, acceptable api? other option see provide remotething
subclass, user needs way cast thing
remotething
if necessary, seems add level of indirection problem.
having interface include members usable on objects implement interface not of them, , includes query method interface members useful, pattern in cases gained it.
examples of reasons can useful:
if it's interface member useful on objects not other instances of same type, pattern may 1 makes sense.
if it's consumer may hold references variety of objects implementing interface, of support particular member , of not, , if it's such collection want use member on instances support it, such usage more convenient if objects implement interface including member, if , don't. especially true interface members
idisposable.dispose
purpose notify implementation of may or may not care (e.g. nobody needs anymore , may abandoned without further notice), , ask whatever needs consequence (in many cases nothing). blindly callingdispose
onienumerable<t>
faster checking whether implementation ofienumerable
implementsidisposable
. not unconditional call faster checkingidisposable
, calling it--it's faster checking whether object implementsidisposable
, finding out doesn't.in cases, consumer may use field hold different kinds of things @ different times. example, may useful have field @ times hold extant reference mutable object, , @ other times hold possibly-shared reference immutable object. if type of field includes mutating methods (which may or may not work) means of creating new mutable instance data copied immutable one, code receives object , might want mutate data can store reference passed-in object. if , when wants mutate data, can overwrite field reference mutable copy; if never ends having mutate data, however, can use passed-in immutable object , never bother copying it.
the biggest disadvantage of having interfaces include members aren't useful imposes more work on implementers. thus, people writing interfaces should include members existence benefit @ least consumers of every class implementing interface.
Comments
Post a Comment