Re: Programming to an Interface




Sasa wrote:

As stated in the GoF:
"Program to an interface, not an implementation.


I think it's pretty clear what it means - of course, it is not clear
when to use it.

True, the GoFfers don't anywhere say anything as precise as, "Here's
when to program to an interface ..." but I'd argue that they do imply
when it should be used: whenever vaiance encapsulation is to be
employed.

Let's take a peek, for example, at Chapter Two alone, the case study,
and count how many times the variance encapsulation theme pops up:

1. Section 2.3, Encapsulating the Formatting Algorithm: We can
isolate the algorithm and make it easily replaceable at the same time
by encapsulating it in an object.
2. Section 2.3 again, Strategy Pattern: Encapsulating an algorithm
in an object is the intent of the Strategy Pattern.
3. Section 2.6, Encapsulating Implementation Dependencies: Neither
alternative is very appealing, but what else can we do? The same thing
we did for formatting and embellishment, namely, encapsulate the
concept that varies.
4. Section 2.7, Encapsulating a Request: Here we have another
example of encapsulating the concept that varies ...
5. Sectoin 2.8, Encapsulating Access and Traversal: Once again a
better solution is to encapsulate the concept that varies ...
6. Section 2.8 again, Iterator Pattern: The Iterator pattern
illustrates once more how encapsulating the concept that varies helps
us gain flexibility and reusability.
7. Section 2.8 yet again, Encapsulating the Analysis: From all
indications, we need to encapsulate the analysis in a separate object,
much like we've done many times before.

In case the notion escapes us, the authors also burried a cute, little
land-mine back in the introduction, in the form of the only table in
the entire book that lists all the book's patterns and compares their
intents. Remember the title of that table, Table 1.2?

Object-orientation concerns reuse, yet the table's not entitled,
"Design aspects that design patterns let you reuse." Object-orientation
concerns complexity management, yet the table's not entitled, "Design
aspects that design patterns let you manage."

The table is entitled, "Design aspects that design patterns let you
vary."

Certainly, if GoF don't intend variance encapsulation to be The
Opportunity to program to an interface, then I've misunderstood their
work.

..ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition

.



Relevant Pages

  • Re: Is OO a tradeable commodity/skill?
    ... In particular, "Design patterns, Elements of Reusable Object-Oriented ... Encapsulating the Formatting Algorithm: ... A programmer may write a thousand lines of code per day, ...
    (comp.object)
  • Re: ASA 5510 and NAT to Site-to-Site
    ... Outside interface address of which device, the ASA or the router? ... You are referring to changing an inner address here, but later refer to changing an outer (encapsulating) address. ... packet over the VPN tunnel encrypted it of course says the source is ...
    (comp.dcom.sys.cisco)