Re: Program to an Interface not an implemetation



Responding to Will...

I am looking at the Gang of Four
Design Patterns and wondering
about the advice 'Program to
an Interface, not an implementation'.
If, for example, I have many similar classes
all needing, say tax and list price then
I should use an interface and factories
so that I decouple and am cohesive. I then
pass the Inteface object to one method
which polymorphically gets the right
implementation from my concrete classes.

Note that almost all of the GoF patterns are addressing a single class of problems where a simple association between client and service cannot capture complex dynamics in context that can lead to substitution of implementations _for the same behavior_. IOW, for the sorts of problems that the GoF patterns address, the fact that a common <abstract> behavior exists is a given. So...




But in my experience, most classes are
not similar enough to do this. Rarely a
couple of classes maybe. Even, say 2 types
of vehicle may not both have a list price
and tax (one exported for example) so is an
interface suitable across many of these absractions?
Most real world abstracted classes are riddled with
their own particular foibles which makes
finding commonality a nightmare.

Delegation and inclusion polymorphism are just tools for solving problems. If the tool doesn't fit, then don't use it.


However, I think there is a more fundamental issue here. The distinguishing thing about the OO paradigm is the militant use of abstraction. We can use abstraction to find commonality in almost anything. It is that ability that make inclusion polymorphism a powerful tool. For example:

           1         attacks 1
[Predator] ------------------- [Prey]
                                 A
                                 |
                  +--------------+----------------+
                  |              |                |
             [Gazelle]        [Pheasant]     [Brontosaurus]
              + run()         + takeFlight()   + stomp()

When a Predator attacks a Prey each type of Prey may have a unique response to that attack and, as in the example, those responses can be very different. However, through the magic of abstraction we can view the response of [Prey] at a higher level of abstraction:

           1         attacks 1
[Predator] ------------------- [Prey]
                         + RespondToAttack()
                                 A
                                 |
                  +--------------+----------------+
                  |              |                |
             [Gazelle]        [Pheasant]     [Brontosaurus]

Instead of thinking in terms of specific responses we step back and think in more general terms about what is actually going on. Now we can accommodate all three responses through a single interface method. The substitution of actual response in the subclass implementation is transparent to the Predator.

Given we are doing things in an OO manner, the responses will likely subsequently collaborate with the Predator during the chase. But that can be abstracted as well so that the collaboration is independent of the specific response. For example, the [Prey] can simply update [Predator] with position data so that [Predator] can react in its own way.


************* There is nothing wrong with me that could not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions  -- Put MDA to Work
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
(888)OOA-PATH



.



Relevant Pages

  • Re: object system...
    ... for that you need machine language. ... there are many other cases where "abstraction" can be leveraged to the ... never have an interface to access not doing something. ... based on class systems rather than type systems. ...
    (comp.object)
  • Re: object system...
    ... in order to use abstraction as self-target. ... Note that an object is defined by its responsibilities. ... never have an interface to access not doing something. ... The 3GL type systems are a compromise with the hardware computational models. ...
    (comp.object)
  • Re: Adding abstract class...
    ... What is the difference between abstract and interface class? ... It depends on what camp you are in as to how you approach OO. ... The same tends to happen with an abstraction. ... For example I might use a pencil for writing or for digging the dirt ...
    (comp.object)
  • Re: Adding abstract class...
    ... What is the difference between abstract and interface class? ... The technical camp based on the approach developed ... The same tends to happen with an abstraction. ... > For example I might use a pencil for writing or for digging the dirt ...
    (comp.object)
  • Re: Modeling Infra Interfaces in UML
    ... It's clear what's going on at that level of abstraction. ... Lollipop to represent the offering of an interface is what I've seen in the ... in stereotypes to represent non-concrete interfaces. ... I would use notation as close as possible to 'singleton' class like ...
    (comp.object)