Lesson 3
Value translation
Task description
Distance in a program may be represented in two units of measurement: miles and in kilometers. One set of algorithms/functions accepts value in miles while another in kilometers. In order to use the same distance value in both, value needs to be “translated” between miles and kilometers. Provide a way to ensure that correct parameter value is provided in both set of interface functions.
Solution
2.1 Type definitions
Separate types/classes need to be defined for each different units of measurement to be used as parameters in different API functions. In our case there are two of those units – kilometers and miles but in other cases may be more. So, these types are DistanceInKilometers and DistanceInMiles. These types are to be used to pass values as parameters into APIs
Then, a main class needs to be defined which will hold the
value of distance in the program – Distance. Value will be only one but there
are accessor methods (empty operators) that will return values of this distance
held by this object internally in different units.
Class UnsignedInteger
{
public:
UnsignedInteger(unsigned
int val) { Value = val; }
protected:
unsigned int Value;
};
Class DistanceInMiles : public class UnsignedInteger
{
DistanceInMiles(unsigned
int value) // constructor – takes initial value in
miles
{ Value = value; } //
assigns parameter value to a member Value
};
Class DistanceInKilometers : public class UnsignedInteger
{
DistanceInKilometers(unsigned
int value) // constructor – takes initial value in
kilometers
{ Value = value; } //
assigns parameter value to a member Value
};
Class Distance : public class UnsignedInteger
{
Distance(DistanceInKilometers Val)
// constructor – takes initial value in kilometers
{ Value = Val.Value; } // assigns parameter value to a member Value
operator DistanceInMiles
() // empty operator method to get in miles
{ return DistanceInMiles(Value
* 10 /16); }
operator DistanceInKilometers
() // empty operator method to get in kilometers
{ return DistanceInKilometers(Value);
}
};
2.2 Usage in APIs
Now we will explain the usage of those classes in the
program. Let's imagine we have two APIs – taking distance in different units.
These APIs need to use these defined types sa parameters in their APIs.
Class APIUsingKilometers
{
static Void API1Method(
DistanceInKilometers dist);
}
Class APIUsingMiles
{
static Void API2Method(
DistanceInMiles dist);
}
2.3 Usage in program
Here will be the usage of the whole thing together. Let's
say we calculated the distance in the program and then use it to pass to
different APIs.
Class123::Method123
{
Distance distanceToSomething(DistanceInKilometers(2345)); // initialize with kilometers
APIUsingKilometers::API1Method(distanceToSomething); // call API which accepts kilometers, the value will be
automatically given in kilometers
APIUsingMiles::API2Method(distanceToSomething); // call API which accepts miles, the value will be
automatically given in miles
}
As we see in the example above, all these class/type
definitions were made just in order to be able to call these two APIs with
“transparent” Distance value and so that each API will get automatically translated correct value. It is
assumed, of course, that in real life the number of times that those APIs are
called are much much bigger and there will be a lot of savings in programmer's
effort on calling those APIs, so that all this “preparation” work on defining
special classes/types is not wasted.
No comments:
Post a Comment