language agnostic - What does it mean to "program to an interface"? -
i have seen mentioned few times , not totally clear on means. when , why this?
i know interfaces do, fact not clear on makes me think missing out on using them correctly.
is if do:
iinterface classref = new objectwhatever()
you utilize class implements iinterface
? when need that? thing can think of if have method , unsure of object passed expect implementing iinterface
. cannot think how need that... (also, how write method takes in object implements interface? possible?)
sorry if missed point.
additional questions:
does using interface nail performance? if how much? how can avoid without having maintain 2 bits of code?
there wonderful answers on here questions sorts of great detail interfaces , loosely coupling code, inversion of command , on. there heady discussions, i'd take chance break things downwards bit understanding why interface useful.
when first started getting exposed interfaces, confused relevance. didn't understand why needed them. if we're using language java or c#, have inheritance , viewed interfaces weaker form of inheritance , thought, "why bother?" in sense right, can think of interfaces sort of weak form of inheritance, beyond understood utilize language build thinking of them means of classifying mutual traits or behaviors exhibited potentially many non-related classes of objects.
for illustration -- have sim game , have next classes:
class="lang-java prettyprint-override">
class housefly inherits insect { void flyaroundyourhead(); void landonthings(); } class telemarketer inherits person { void callduringdinner(); void continuetalkingwhenyousayno(); }
clearly, these 2 objects have nil in mutual in terms of direct inheritance. but, both annoying.
let's our game needs have sort of random thing annoys game player when eat dinner. housefly
or telemarketer
or both -- how allow both single function? , how inquire each different type of object "do annoying thing" in same way?
the key realize both telemarketer
, housefly
share mutual loosely interpreted behavior though nil alike in terms of modeling them. so, let's create interface both can implement:
class="lang-java prettyprint-override">
interface ipest { void beannoying(); } class housefly inherits insect implements ipest { void flyaroundyourhead(); void landonthings(); void beannoying() { flyaroundyourhead(); landonthings(); } } class telemarketer inherits person implements ipest { void callduringdinner(); void continuetalkingwhenyousayno(); void beannoying() { callduringdinner(); continuetalkingwhenyousayno(); } }
we have 2 classes can each annoying in own way. , not need derive same base of operations class , share mutual inherent characteristics -- need satisfy contract of ipest
-- contract simple. have beannoying
. in regard, can model following:
class="lang-java prettyprint-override">
class diningroom { diningroom(person[] diningpeople, ipest[] pests) { ... } void servedinner() { when diningpeople eating, foreach pest in pests pest.beannoying(); } }
here have dining room accepts number of diners , number of pests -- note utilize of interface. means in our little world, fellow member of pests
array telemarketer
object or housefly
object.
the servedinner
method called when dinner served , our people in dining room supposed eat. in our little game, that's when our pests work -- each pest instructed annoying way of ipest
interface. in way, can have both telemarketers
, houseflys
annoying in each of own ways -- care have in diningroom
object pest, don't care , have nil in mutual other.
this contrived pseudo-code illustration (that dragged on lot longer anticipated) meant illustrate kind of thing turned lite on me in terms of when might utilize interface. apologize in advance silliness of example, hope helps in understanding. and, sure, other posted answers you've received here cover gamut of utilize of interfaces today in design patterns , development methodologies.
language-agnostic oop interface