Making a variable an object  
Author Message
-





PostPosted: 2005-6-8 10:45:00 Top

java-programmer, Making a variable an object Inside a class, amongst other variables, I have a variable 'orientation'
which can hold three valid values: horizontal, vertical, and no orientation.

In true oo design, should i bother creating a Orientation nested class
just for the variable no matter how small the class is?
 
Michael Preminger





PostPosted: 2005-6-8 15:40:00 Top

java-programmer >> Making a variable an object On 08.06.2005 04:44, - wrote:
> Inside a class, amongst other variables, I have a variable 'orientation'
> which can hold three valid values: horizontal, vertical, and no
> orientation.
>
> In true oo design, should i bother creating a Orientation nested class
> just for the variable no matter how small the class is?

Have you considered using the enum type (that is, if you are using Java 1.5)

M.
 
Wibble





PostPosted: 2005-6-8 19:03:00 Top

java-programmer >> Making a variable an object - wrote:
> Inside a class, amongst other variables, I have a variable 'orientation'
> which can hold three valid values: horizontal, vertical, and no
> orientation.
>
> In true oo design, should i bother creating a Orientation nested class
> just for the variable no matter how small the class is?
Orientation is an int (1,0,-1), not an Object.
You want to be able to use > & >= etc.
You want to be able to multiply vectors by orientation to change velocity.

Use 3 static final contants.
An enum is overkill.
 
 
-





PostPosted: 2005-6-8 20:31:00 Top

java-programmer >> Making a variable an object Wibble wrote:

> Orientation is an int (1,0,-1), not an Object.
> You want to be able to use > & >= etc.
> You want to be able to multiply vectors by orientation to change velocity.
>
> Use 3 static final contants.
> An enum is overkill.

In this context, I'm using.

public class SomeClass {

public static final String HORIZONTAL_ORIENTATION =
"horizontalOrientation";

...
...
}


Now i'm considering making

public class SomeClass {

private Orientation orientation = new
Orientation(Orientation.HORIZONTAL);

...
}

public class Orientation {

public static final String HORIZONTAL = "horizontal";
...
...

// Or use enum type?
}

comments?

 
 
asb





PostPosted: 2005-6-8 20:50:00 Top

java-programmer >> Making a variable an object email***@***.com wrote in comp.lang.java.programmer:
> - wrote:
>> Inside a class, amongst other variables, I have a variable 'orientation'
>> which can hold three valid values: horizontal, vertical, and no
>> orientation.
>>
>> In true oo design, should i bother creating a Orientation nested class
>> just for the variable no matter how small the class is?

In true OO design you _always_ use an object.

But in this case, when the state of your object does not change
after it has been created, you can use the same object wherever
you need horizontal orientation.

The interesting question is "how do you get a reference to the
instance?"

> Orientation is an int (1,0,-1), not an Object.
> You want to be able to use > & >= etc.

Equality comparison most likely, but definitely none of the
others. Or is horizontal less or greater than vertical?

> You want to be able to multiply vectors by orientation to change velocity.

You are thinking about a very narrow application domain. A
game involving space ships and aliens, maybe?

--
Antti S. Brax Rullalautailu pitæ»— lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/

[1385 messages expunged from folder "Spam"]