Re: OOA?

From: Phlip (phlip_cpp_at_yahoo.com)
Date: 01/15/04


Date: 14 Jan 2004 23:19:49 EST

Michael G wrote:

>>Can you define what it means to "model the
>> problem domain"?
>
> I'll try. The following may sound like a canned answer but it isn't.
>
> It means to create a community of collaborating objects that talk to each
> (send and receive messages) in order to solve some problem. The
> types/kinds/classes of objects that exist in this community depend upon
> the problem domain.

At the risk of recidivism, here's a neat post I forwarded here a while ago:

- ---8<------------------------------

Several months ago I attended my friend Payson Hall's excellent
problem-solving class. One of his problem-solving hueristics is to
try to simplify the problem before solving it. And sometimes
simplifying requires you to introduce a bit of complexity.

To illustrate the hueristic, he told us about how he once wrote a
program to play the solitaire card game Klondike (aka Canfield). As
he wrote the program, he noticed that a great deal of code was devoted
to special cases. For example, when you're playing to the tableau
(the seven piles of cards that you deal out in the starting position),
the usual rule is that you can play a card onto any opposite-colored
card of the next-higher rank -- that is, you can play a red 8 on a
black 9, or a black jack to a red queen. There is a special case
having to do with spaces (empty piles): You can play a king to a
space. So cards ace through queen have one rule, and kings have
another.

Another special case: playing to the foundation (the four initially
empty piles to which you ultimately play the cards). The general rule
in the foundation is that you can play a card to a same-suit card of
the next-lower rank -- you can play the three of clubs onto the two of
clubs. The special case: Aces are played to an empty foundation
pile. Cards 2 through king have one rule, and aces have another.

I loved Payson's way of simplifying the code: Introduce a slightly
more complex starting position.

The foundation piles don't start out empty, instead each has a face-up
card of a new rank: the Naught -- the naught of clubs, the naught of
hearts, and so on. The rank of a naught is zero. You can play an ace
onto a naught of the same color.

Visually, the naught cards are completely transparent. Though they're
there on the screen, you can't see them. A pile with only a naught
looks exactly like an empty pile. And because Payson's program
automatically moves cards to the foundation when possible, the user
never has to see which invisible card to play to. When the ace of
clubs appears, the program automatically plays it onto the naught of
clubs.

The tableau piles also have something new: Before dealing the cards
to the tableau, place onto each pile a face-down card of a new rank
and suit: the Emperor of Emeralds. Emperors outrank kings by one.
Now, when you move the next to last card from a pile, you flip over
the remaining card, the Emperor. The rule for playing to the tableau
becomes: you can move a card onto any different-suit card of the
next-higher rank. Not the opposite suit -- there are now three suits
-- but different suit. Most cards still behave the same way. Black 8
on red nine. But you can now move a king onto any Emperor of a
different suit -- that is, onto any Emperor of Emeralds.

As with the naughts, the Emperors of Emeralds are transparent. A pile
with only an Emperor looks empty. But you can't move an Emperor --
not because there are no empty spaces, but because there are no
higher-rank cards. Same rule as for the other cards.

The special cases for playing kings to the tableau and aces to the
foundation are now gone. Those moves work just like all other moves.
Greatly simplified code, for the cost of a slightly more complex
starting position.

Dale

- -
Dale Emery -- Consultant -- Resistance as a Resource
Web: http://www.dhemery.com
Weblog: http://www.dhemery.com/cwd (Conversations with Dale)

Where there's a will, there's a won't. -- Ambrose Bierce

- ---8<------------------------------

That may or may not be the "best" OO design. Let's just say it sustains a
team's effort, and at least stays out of their way. Those interested in
better ways can use Google Groups on a sample string from the quote; this
newsgroup proposed some good alternative models. That's one of our jobs.

But efforts to discover such a design, or to debate whether it's the "best"
option for a team, are hindered, not helped, by slavish attention to
potential objects from the problem's domain.

A deck of cards has no Emerald Suit or Emperor Rank.

Suppose someone less smart than us tried to design follow the advice "write
about the problem, then scan your writing for frequently recurring nouns."
They might started their project by writing a Deck class and a Card class.
Then they might assigned these Suit and Rank properties, and then they
would write the deck behaviors - sort, stack, discard, etc. This sequence
of design activities might accidentally obscure the big picture.

If, instead, we advise to start with behaviors, they might then apportion
objects that best balance these behaviors, then merge the objects into
classes. Following that schedule discovers the classes last. The odds they
match problem domain classes get much lower. They are simply the
intersection of bearers of behavior.

If your domain expert asks for certain domain objects, simply make the
program's user interface say the program has them. Leave the Emperor and
Emerald cards invisible.

-- 
  Phlip


Relevant Pages

  • Eliminating special cases - a case study
    ... program to play the solitaire card game Klondike. ... the Naught -- the naught of clubs, ... A pile with only a naught ...
    (comp.object)
  • Re: Liar paradox, one more time
    ... God sorts the cards into three piles, ... Pile A contains those cards that are true. ... > meaningless, or whose meaning is too ambiguous to be considered definitely ... God comes upon a card upon which is written "This card ...
    (sci.logic)
  • Re: Liar paradox, one more time
    ... God sorts the cards into three piles, ... Pile A contains those cards that are true. ... >> It's a prediction about what God is going to do to a particular card, ... If He puts it in pile D, then He *doesn't* put it in pile A, ...
    (sci.logic)
  • Re: Liar paradox, one more time
    ... Daryl McCullough wrote in message ... ... God sorts the cards into three piles, ... Pile A contains those cards that are true. ... God comes upon a card upon which is written "This card ...
    (sci.logic)
  • Re: Liar paradox, one more time
    ... God sorts the cards into three piles, ... Pile A contains those cards that are true. ... God comes upon a card upon which is written "This card ... This sentence is perfectly meaningful. ...
    (sci.logic)