Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: OO vs. global variables...

by Masem (Monsignor)
on Sep 04, 2001 at 21:50 UTC ( #110102=note: print w/replies, xml ) Need Help??

in reply to OO vs. global variables...

I think what seems tricky is how a Point is going to know it's an edge point in a typical OO fashion, as you're finding out, you're trying to make objects work uphill as well as down. This typically doesn't work well.

There's three possible solutions that I can think of, two requiring massive code changes in Board and other parts of the code, and one that requires Point to be changed. Let me do that one first:

If this were C++, I would say one possible solution is to use templates (Pardon any syntax, I know what I mean, but haven't written C++ in years :-), and I just want to show the idea here...)

template <int H, int V> class SpecificBoard extends Board { Point<H,V>** array; new() { array = new Point<H,V>[H][V]; /* then set i,j on each... */ } ... } template <int H, int V> class Point { int i,j; new(x,y) { i = x; j = y } ... } Board* board = new SpecificBoard<10,10>;
What comes about here is that implicitely, you've passed the board size to the point class (which is specific to the board size), so the point doesn't know what Board it is part of, but can work out edge information from this. Can this be done in Perl? Sure, at the cost of two extra int's per point. It's not as 'invisible' as the C++ template method, but then allows you to check edge states without having a Board object to worry about.

A second way would be to avoid having points know how to check for edges, and instead have those functions built into the Board class. It makes, from an object pov, more sense to have it this way, though from your description, it sounds like you'd have to modify code throughout to make this work.

Finally, you could create a small BoardInfo class that only contains just enough info about the board itself (size, for example) that could be passed around to other objects without passing the entire Board object. The Board object itself could hold on to one copy of this to replace it's own versions of size and other details. But again, this is sort of getting away from a good OOP solution, since Points would need to know the BoardInfo to determine if they are edge points.

(A fourth solution, icky but doable, is to create subclasses of points, MiddlePoint, EdgePoint, and CornerPoint, that would be determined by the Board object at creation time, and would immediately know what their state is. But that seems like lots of extra classes...)

IMO, 2 is the *best* way to go, but 1 is the most adaptable if you are already though hours of coding.

Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
It's not what you know, but knowing how to find it if you don't know that's important

Replies are listed 'Best First'.
Re: Re: OO vs. global variables...
by Boots111 (Hermit) on Sep 06, 2001 at 03:08 UTC
    I know that this is not a C++ forum; however, it is a programming one and learning one language helps with all others...

    This is a completely incorrect use of templates. Templates take only a class (not an instance of a class), thus you would get <int, int> rather than <8,8>. What you want here is a constructor that takes two int's (or scalars) as arguments, and builds the class based on this input.

    The solution that has point as a base class and three inherited classes, middle-point, edge-point, and corner-point, is what fits best into an OO design pattern. Because the whole purpose of OOP is to have each class only worry about itself. However, you might consider making a line class to determine captures since captures are really line oriented...

      I know the original specification of C++ only allows class names as template parameters, but recent C++ developes allow a larger body of parameters to templates (see (first reasonable link I could find) here for example) -- this type of specification is commonly used to demonstrate how one can do fibonaci calculations all at compile time, rather than run time. Mind you, all these have to be specified at compile time, so that my method would not work with a board that could be dynamically allocated. But from a naming classification, you're talking about the slight difference between a Point-on-an-X-by-Y board, and a Point that knows it's on an X-by-Y board. Minor here, but could be significant in other cases.

      Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
      It's not what you know, but knowing how to find it if you don't know that's important

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://110102]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2022-01-27 04:50 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (70 votes). Check out past polls.