Programming to an Interface
- From: "GoogleEyeJoe" <crgsmrt@xxxxxxxxxxx>
- Date: 13 May 2006 03:03:26 -0700
Hi,
I have just been reading about the concept of programming to an
interface. As I understand, it is useful to program to an interface
with respect to polymorpism. I.e. If you wanted to treat books and
videos in the same way, as a product for example (something that has a
price - for simplicity )you can make those objects implement the
interface and then program to that interface. So rather than create
code that calls, lets say...
book1.GetPrice();
'book1 being an implementation of the book class'
and
video1.GetPrice()
'video1 being an implementation of the video class'
You could instead write one piece of code that would call the correct
GetPrice method by means of polymorphism.
E.g.
===
product1.GetPrice()
'this would call the GetPrice for a video or book depending on the
object being dealt with'.
That's fine. I understand that. What does confuse me however, is the
notion that programming to an interface reduces the likelihood that
changing an object's implementation will break existing code (code
created by the client of the object (that implements the interface).
Why is this...?
For example, if you had the method AddTwoNumbers(numberOne, numberTwo)
in either an interface or in an object, it doesn't matter which you
use, you cannot change that method's signature (in either the interface
or an object) as doing so would break the client of the method (whether
it is called via an interface or via an object).
So where did this notion of reducing the chance of breaking client code
come from???
Surely, rather than add an additional method to an interface every time
a new method signature is required, you could simply add a new method
to the object... I just can't see how programming to an interface will
reduce the likelihood of breaking a clients code.
I'd really appreciate if someone could tell me where I'm thinking along
the wrong lines.
Many thanks in advance.
Craig.
.
- Follow-Ups:
- Re: Programming to an Interface
- From: Robert Martin
- Re: Programming to an Interface
- From: Pennyliang
- Re: Programming to an Interface
- From: contravariance
- Re: Programming to an Interface
- From: Nick Malik [Microsoft]
- Re: Programming to an Interface
- From: Daniel T.
- Re: Programming to an Interface
- From: VisionSet
- Re: Programming to an Interface
- From: Patrick May
- Re: Programming to an Interface
- Prev by Date: Re: Searching OO Associations with RDBMS Persistence Models
- Next by Date: Re: Programming to an Interface
- Previous by thread: requirements gathering
- Next by thread: Re: Programming to an Interface
- Index(es):
Relevant Pages
|