R
Rhino
My background in OO theory is not that great and I am struggling with the
right way to design something that I can describe fairly easily. Can someone
help me figure out what I need to do?
I am writing some methods that want to determine if a given value is within
a range of values. For example, if the range is 1 to 5, I want my method to
return true if the input parameter is 1, 2, 3, 4, or 5; otherwise, the
method should return false. By the same token, given a range of dates
starting with Jan 1 2005 and ending with Dec 31 2005, I want to return true
if a given date belongs to 2005 but return false otherwise.
That is trivial to code if I deal with each data type one at a time but I
think this requirement cries out for a general solution. It seems to me that
I have identified the need for a class that represents a Range of two values
of the same type. (I'm assuming that it makes no sense to have a Range that
has a low value of one type and a high value of a different type, such as
"cat" to Dec 31 2005). It seems to me that a Range is a very specialized
collection and my implementation of choice should be a TreeSet since it
always keeps the low value of the range before the high value, which makes
it very useful for the between() method I will need.
Therefore, am I on the right track so far in saying that Range should
subclass TreeSet or is there a better approach?
If Range should subclass TreeSet as I suspect, I'm not sure if Range should
be a concrete or abstract class. The reason for my uncertainty is that I
need to insure certain behaviour in my Range class:
- an instance of Range can only contain two objects
- the two objects within an instance of Range have to be the same type, e.g.
both ints or both dates or both Strings
- it should not be possible to remove any elements from the Range once it
has been created
- it should not be possible to add any elements to the Range once it has
been created
I'm not sure if I can do everything in one well-written Range class or if I
will need to create subclasses like IntRange, DateRange, etc.
I tried writing my Range class and came up with two constructors;
unfortunately neither or them successfully adds the Objects passed to the
constructor to the Range. Here is the code (please forgive all the
diagnostics):
public Range(Object firstObject, Object secondObject) {
super();
System.out.println("firstObject: " + firstObject);
System.out.println("secondObject: " + secondObject);
/* Verify that both objects are the same class. */
String firstObjectClass = firstObject.getClass().getName();
String secondObjectClass = secondObject.getClass().getName();
if (!firstObjectClass.equals(secondObjectClass)) {
throw new IllegalArgumentException("The two Objects passed to
this constructor must be the same class.");
}
boolean result = add(firstObject);
if (result != true) {
throw new IllegalArgumentException("The first Object was not
added to the Range.");
}
result = add(secondObject);
if (result != true) {
throw new IllegalArgumentException("The second Object was not
added to the Range.");
}
System.out.println("Range(Object, Object) size: " + size());
}
and
public Range(Object[] range) {
super();
System.out.println("range.length: " + range.length);
System.out.println("range[0]: " + range[0]);
System.out.println("range[1]: " + range[1]);
if (range.length != 2) {
throw new IllegalArgumentException(
"The range array must contain exactly two elements.");
}
/* Verify that both objects are the same class. */
String firstObjectClass = range[0].getClass().getName();
String secondObjectClass = range[1].getClass().getName();
if (!firstObjectClass.equals(secondObjectClass)) {
throw new IllegalArgumentException("The two objects passed to
this constructor must be the same class.");
}
boolean result = add(range[0]);
if (result != true) {
throw new IllegalArgumentException("The first Object was not
added to the Range.");
}
result = add(range[1]);
if (result != true) {
throw new IllegalArgumentException("The second Object was not
added to the Range.");
}
System.out.println("Range(Object[]) size: " + size());
}
For some reason, the add() methods are not adding the values to the Range
class; I don't understand WHY they aren't working or how to find out. The
values I'm passing in are Strings, specifically "cat" and "dog".
I'm trying to figure out if I have a fundamental design flaw here or if I've
just made a silly (but not obvious, at least to me) coding error.
I would also like to confirm that the best way to prevent the Range from
holding more or less than 2 objects is to override the add(), addAll() and
remove() methods so that they don't do anything.
Can anyone help me confirm that my design is sound and/or help me figure out
why my Objects are not being added to the Range? I've worked on this for the
last few hours and I'm just not seeing the problem yet....
--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
right way to design something that I can describe fairly easily. Can someone
help me figure out what I need to do?
I am writing some methods that want to determine if a given value is within
a range of values. For example, if the range is 1 to 5, I want my method to
return true if the input parameter is 1, 2, 3, 4, or 5; otherwise, the
method should return false. By the same token, given a range of dates
starting with Jan 1 2005 and ending with Dec 31 2005, I want to return true
if a given date belongs to 2005 but return false otherwise.
That is trivial to code if I deal with each data type one at a time but I
think this requirement cries out for a general solution. It seems to me that
I have identified the need for a class that represents a Range of two values
of the same type. (I'm assuming that it makes no sense to have a Range that
has a low value of one type and a high value of a different type, such as
"cat" to Dec 31 2005). It seems to me that a Range is a very specialized
collection and my implementation of choice should be a TreeSet since it
always keeps the low value of the range before the high value, which makes
it very useful for the between() method I will need.
Therefore, am I on the right track so far in saying that Range should
subclass TreeSet or is there a better approach?
If Range should subclass TreeSet as I suspect, I'm not sure if Range should
be a concrete or abstract class. The reason for my uncertainty is that I
need to insure certain behaviour in my Range class:
- an instance of Range can only contain two objects
- the two objects within an instance of Range have to be the same type, e.g.
both ints or both dates or both Strings
- it should not be possible to remove any elements from the Range once it
has been created
- it should not be possible to add any elements to the Range once it has
been created
I'm not sure if I can do everything in one well-written Range class or if I
will need to create subclasses like IntRange, DateRange, etc.
I tried writing my Range class and came up with two constructors;
unfortunately neither or them successfully adds the Objects passed to the
constructor to the Range. Here is the code (please forgive all the
diagnostics):
public Range(Object firstObject, Object secondObject) {
super();
System.out.println("firstObject: " + firstObject);
System.out.println("secondObject: " + secondObject);
/* Verify that both objects are the same class. */
String firstObjectClass = firstObject.getClass().getName();
String secondObjectClass = secondObject.getClass().getName();
if (!firstObjectClass.equals(secondObjectClass)) {
throw new IllegalArgumentException("The two Objects passed to
this constructor must be the same class.");
}
boolean result = add(firstObject);
if (result != true) {
throw new IllegalArgumentException("The first Object was not
added to the Range.");
}
result = add(secondObject);
if (result != true) {
throw new IllegalArgumentException("The second Object was not
added to the Range.");
}
System.out.println("Range(Object, Object) size: " + size());
}
and
public Range(Object[] range) {
super();
System.out.println("range.length: " + range.length);
System.out.println("range[0]: " + range[0]);
System.out.println("range[1]: " + range[1]);
if (range.length != 2) {
throw new IllegalArgumentException(
"The range array must contain exactly two elements.");
}
/* Verify that both objects are the same class. */
String firstObjectClass = range[0].getClass().getName();
String secondObjectClass = range[1].getClass().getName();
if (!firstObjectClass.equals(secondObjectClass)) {
throw new IllegalArgumentException("The two objects passed to
this constructor must be the same class.");
}
boolean result = add(range[0]);
if (result != true) {
throw new IllegalArgumentException("The first Object was not
added to the Range.");
}
result = add(range[1]);
if (result != true) {
throw new IllegalArgumentException("The second Object was not
added to the Range.");
}
System.out.println("Range(Object[]) size: " + size());
}
For some reason, the add() methods are not adding the values to the Range
class; I don't understand WHY they aren't working or how to find out. The
values I'm passing in are Strings, specifically "cat" and "dog".
I'm trying to figure out if I have a fundamental design flaw here or if I've
just made a silly (but not obvious, at least to me) coding error.
I would also like to confirm that the best way to prevent the Range from
holding more or less than 2 objects is to override the add(), addAll() and
remove() methods so that they don't do anything.
Can anyone help me confirm that my design is sound and/or help me figure out
why my Objects are not being added to the Range? I've worked on this for the
last few hours and I'm just not seeing the problem yet....
--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare