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 things 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 calling dispose on ienumerable<t> faster checking whether implementation of ienumerable implements idisposable. not unconditional call faster checking idisposable , calling it--it's faster checking whether object implements idisposable , 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

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -