Thursday, 12 September 2013

Method Of Retrieving Random Value Depending on Type

Method Of Retrieving Random Value Depending on Type

I was wondering what would be the most appealing way of achieving this goal.
I have a blackbox. I give it a supported type and it returns a random
value base off of the type.
For my supported types, I have an enum defined as follows:
public enum Types
{
INTEGER,
DOUBLE,
BIGINTEGER,
BIGDECIMAL,
STRING,
BOOLEAN,
CHAR
}
I thought the easiest way would be to create a static function which
returns an Object based off of the Type
public static Object getRandomValue(Types type)
{
switch(type)
{
case INTEGER:
return randomInt();
case DOUBLE:
return randomDouble();
case BIGINTEGER:
return randomBigInt();
case BIGDECIMAL:
return randomBigDecimal();
case STRING:
return randomString();
case BOOLEAN:
return randomBool();
case CHAR:
return randomChar();
}
}
The problem is that an additional cast would have to be made each time
that I want to retrieve a random value based off of this method.
I've looked into the design patterns abstract factory and factory, and I
can't decide if there's an advantage into implementing either of those. Or
if there's a different design pattern that seems to be more appropriate.
Assume that all my random methods are defined.

No comments:

Post a Comment