Re: OO, I just don't get it.

From: Daniel T. (daniel_t_at_eathlink.net)
Date: 12/16/03


Date: Tue, 16 Dec 2003 05:19:22 GMT


"Val" <valmont_gaming@hotmail.com> wrote:

> Maybe I should have been more clear. I don't have any problems at all with
> the basics of OOP. I certainy don't have problems with coding.
> No wonder after all these years I'd say. I feel it is the most perfect time
> to move on:
> It's OOD I have problems with. Wasn't this the domain of patterns/factoring?
> But the explanations I encounter on (for example) university
> sites "feel" way out of reality.
>
> Perhaps I should give and example, to illustrate my weakness. I will do that
> below this line.
> In fact, I should post this little pdf file which demonstrates the case of
> designing a sorting program. It is most likely a newbie case for you
> designers.
> http://home.planet.nl/~bosnj002/cpp/pattern-examples4.pdf
> Coding wise, it might be a nice but relatively easy task for me. I would
> combine fantasy with healthy thinking and off I go.
> But look at the pdf. From page 6 I lost them already. "Avoid the grand
> mistake of using top-level algorithmic view to structure design..." .
> Huh? "Facade, Adapters...".
> If I want a sorting program then I'll check my know-how about datastructures
> (representation) and algorithms (STL perhpas, other resources, misc
> algorithms). And
> then I'll code after sketching a UML diagram on paper (to throw it away a
> day later)
>
> Do you see now my problem? What's are "they" talking about? What will I
> achieve in then end? And once again, is there a book/tutorial like
> "Patterns and Factoring for Dummies"? Because I just can't "place" the
> things these engineers are mentioning.

I think I see your problem. Let's see what I can do to contribute...

First, write a program that can forward sort lines of text from standard
input and write them to standard output. Don't worry about any other
requirements and don't write the program to do anything more than the
sort in question. Oh, and since this is a learning exorcise, don't use
the STL... :-)

Now add in a command line option to "sort in reverse". Notice that
somewhere in your program, you have to do something different based on
whether the sort is forward or reverse. Now I want you to be creative
and arrange the code so that the if check on the reverse flag is only
done once during runtime. In other words, if you put the check inside a
loop somewhere, it will be called every pass, instead put the check
outside all the loops and use an abstract class that has a method that
tells the algorithm which which item goes first, then the if check
creates a concrete object from a sub-class of that abstract class.

Now add a command line flag to sort numerically. If this flag is set,
the program should parse input as integers and sort them. It should not
sort strings with this flag set. :-) Again, the flag should only be
examined once during runtime. Of course you must make sure that the
"sort in reverse" flag still works.

When you are done with this step, you should have a program that (1)
checks the flags and creates some objects based on how they are set, (2)
reads the input and uses one or more of the objects created above to
parse it, (3) sorts the items, again using one or more of the objects
created in step 1 to decide the sort order, then (4) outputs the items,
again using one or more of the objects created in step 1.

Now ask yourself, how hard would it be to use the sorting algorithm for
sorting doubles? What if you wanted it to sort shapes by size? Would you
have to make any changes to the actual input, sort, and output sections
of the program, or only to the "check the flags and create some objects"
part?

Extra Credit: add a command line option that allows the user to change
the sort algorithm being used...

If desired, e-mail the program to me (replace "postmaster" with
"daniel_t"), or post it on this newsgroup and let us critique it...

As you come to understand what "they" are talking about in the example,
you will find that you routinely write program units that can work in a
variety of contexts, just like the input, sort, and output units in the
above program work for many different situations, even ones you haven't
thought of yet.



Relevant Pages

  • Re: Find Previous Instance on another computer?
    ... It has been suggested you use some sort of flag and you have ... running instances. ... > check a second computer for the same reason. ...
    (microsoft.public.vb.general.discussion)
  • Re: Strange CListCtrl Sorting problem
    ... I had Sort flag set to ascending in the resource editor. ... >> If the first time I populate the CListCtrl and sort it, ... >> AliR. ...
    (microsoft.public.vc.mfc)
  • Re: Strange CListCtrl Sorting problem
    ... I had Sort flag set to ascending in the resource editor. ... > If the first time I populate the CListCtrl and sort it, ... > AliR. ...
    (microsoft.public.vc.mfc)
  • Re: CPU increasing slowly up to 100%
    ... I think you should look for examples of one process waiting for another ... Sometimes people think if this sort of wait is placed in a thread, ... On the topic of deadlocks, and again at the risk of saying the obvious, ... flag and exit on its own. ...
    (comp.lang.java.programmer)
  • Re: Optimizing An Integer Array Reverse
    ... So does it sort the array, reverse the array, or sort in reverse order? ... If the former, which algorithm does it ...
    (comp.lang.asm.x86)