# Proper OO method for condensing code segment

This topic is 5195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well, that is a bit clearer. However I don't see why you made a direction class and a coordinate class or why you would make a coordinate a direction. I would think that coordinates can have a direction and thats a 'has a' relationships not a 'is a' relationship. Or in effect you could have it such that a Direction is a mathematical vector where it 'has a' start and 'has a' end coordinates. Therefore neither would be polymorphically coupled like you have them they would be coupled rather by a dependancy.
But then you would be allowed to derived different types of directions and even different types of coordinates.

##### Share on other sites
Zahlman, I really like the idea of having the direction be an object. Encoding the delta pairs in named constants is much better! I initially thought you meant the user specified movement as deltas. Good stuff.

As far as the "direction vs. coordinate" problem goes, it is certainly an interesting one. The humorous thing about it is that in my current engine, we have 2D and 3D Vectors. There is no differentiation between a direction and a coordinate. I've always thought it was a bit odd, but fortunately for us it hasn't been a huge issue (it's not a big leap from vector to coordinate). IMO, the peculiarity of a proper solution comes from the fact that a direction and a coordinate are the same data, just interpreted differently.

It's difficult coming up with an argument for how to actually make the coordinate and vector seperate classes. I would almost think that coordinate be the base class, storing only the x and y positions. Then, have direction extend that and add functions for dealing with vectors such as length, angle, summation, etc.

This all seems a bit overengineered to me. Perhaps a vector class to represent coordinates and directions is appropriate after all? Maybe a simple typedef of Vector to Coordinate would suffice?

##### Share on other sites
The inheritance definitely is a hack, to expose the Direction internals only to Coordinates.

There is another option: make both these classes have-a "XYPair", or some other silly-named thing like that, where the data members are public but the class itself is not. Then put XYPair, Coordinate and Direction in a package together, without anything else inside.

That's Java-specific, but the net effect is that outside classes do not know of the existance of XYPair, so they will not be able to get at any of those internals, although Coordinate and Direction are able to share them. Wow, I actually almost miss the 'friend' keyword now.

(You could also make the XYPair a public class with package-level constructor, so that only Directions and Coordinates can *make* XYPairs, which should still give you enough security. In that setup, you might also put factory methods into the XYPair class, and hide the Direction/Coordinate constructors.)

Once you've done that you can also prevent Coordinates from adding to other Coordinates (requiring a Direction), since Coordinate no longer inherits from Direction.

I agree that this is all over-engineered and I never write stuff like this, but these are the kinds of lengths you end up going to if you put too much faith in OO dogma :)

Unfortunately Java lacks 'typedefs' :\ Of course, my choice of Java to present example code is rather arbitrary.

##### Share on other sites
I may be a little confused but isn't the only purpose of data-hiding to prevent something from modifying the invariant of an object?

Why don't you just produce a datatype that holds the vector data and write the ruitines required to perform operations like comparing and adding/subracting, ..., on the vector structure.

struct vector {    datatype x;    datatype y;    datatype z;    // datatype can be any type you require or even a template parm!};

You then write classes like Coordinate and Direction which provide extended operations for working with vectors assuming they have themselves little need for making the vectors they contain part of their invariant.

Finally to maintain the Board object's invariant you simply use all of the utilities you've just written to aid in writing a few simple checks (like to make sure that the translation vector is no more than x+-1 or a y+-1 translation).

Such a solution is still as far as I know Object Oriented since I'm unaware of OO sugesting that data must be C++ primitives held in a class that protects such primatives as an invariant instead of simply producing a compound data type that is more suitable for this problem.

##### Share on other sites
Well that brings up the point of whether you really need two classes. You could just make them both one class called a VectorCoordinate which can be added to each other resulting in a new VectorCoordinate thus the direction or the movement of one tile or object (whatever) would be in terms of distance between your current location and the amount which you desire to deviate from that coordinate.

Thus the direction coordinate becomes a relative posistion to your current location.

Let me illustrate:
public class CoordinateVector { private x=0, y=0; public CoordinateVector( int x, int y ) {  this.x = x;  this.y = y; } public CoordinateVector translate( CoordinateVector dest ) {  return( new CoordinateVector( this.x + dest.x, this.y + dest.y ) ); } public void moveTo( CoordinateVector dest ) {  this.x = dest.x;  this.y = dest.y; }}

This is simple and it can be added to do do other functionality such as mirroring over x and y axis if need be and you would want to keep the Board functionality asside from this classes functionality there is a cohesion issue if you combine them. This is very similar to javas Point class and if I was using java I would have used it instead of writing my own function. But I was using javascript and I was thinking too much along the lines of Arrays than Cartisian Planes.

Now if you wanted to expand this and create a Board using either this class or java.awt.Point or even java.awt.geom.Point2D how would you do it?

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013394
×