An instance of an RWExchangeFactory contains an exchange rate table and at least one exchange group. Exchange groups can construct the exchange objects that are used to perform currency conversions. When presented with a source/target currency pair, the exchange factory searches its list of exchange groups until it finds one that can create an exchange object with the given source/target pair and the factory's associated exchange rate table. If none of the groups in the factory can create a valid exchange object the factory returns an invalid object.
The following example demonstrates how a factory works.
Suppose you have the job of exchanging currency. You have at your disposal a currency exchange rate table, a calculator, and lots of currencies. You also have the following procedure for doing currency conversion:
First, you always check to see if both the source and target currencies are in the European Monetary Union (EMU). If so, you need to use the following rule for converting the source amount into a target:
a. | Find a conversion factor in the exchange rate table for converting Euro's into the source currency (usually in the form 1 Euro = source). |
b. | Convert the source amount into a Euro amount by dividing by this factor. |
c. | Find a conversion factor for converting Euro's to the target currency. |
d. | Use this factor to multiply the amount just computed to obtain the amount in the target currency. |
If either one or both currencies are not part of the EMU, you will look for an exchange rate that converts the source amount to the target amount. Once you find that rate, you can multiply the source amount by this exchange rate to obtain the target currency amount.
If you can't find a source-to-target rate in the exchange rate table, you look in the table for an exchange rate that converts the target into the source. If you find this rate, you divide the source amount by it to obtain the amount in the target currency.
Class RWExchangeFactory encapsulates this behavior. An RWExchangeFactory instance contains an exchange rate table and a list of one or more currency exchange groups. As mentioned in Section 5.4, currency exchange groups produce an exchange object with a particular implementation when presented with a source currency, a target currency, and an exchange rate table. Money.h++ supplies the following exchange groups:
RWEuroGroup, EuroGroup in Java, which produces an exchange object with an implementation that performs an exchange using the triangulating procedure described in step one of the example.
RWMultiplicationGroup, MultiplicationGroup in Java, which creates exchange objects that implement the exchange algorithm described in step two.
RWDivisionGroup, DivisionGroup in Java, which creates exchange objects that implement the exchange algorithm described in step three.
The C++ exchange groups listed above are derived from the abstract base class RWExchangeGroupImpl. The handle class, RWExchangeGroup presents the interface common to all exchange groups and contains a C++ pointer to a specific implementation class that must be derived from RWExchangeGroupImpl. In Java, the exchange groups inherit directly from the abstract base class ExchangeGroup. An exchange factory object, RWExchangeFactory in C++ and ExchangeFactory in Java, contains a list of exchange group objects.
When you create a factory, you are defining the overall process that will be used to do a conversion. In order to implement the scenario described at the beginning of this section in C++, your exchange rate factory needs to contain an exchange rate table and three RWExchangeGroup objects. The first RWExchangeGroup object in the factory contains an RWEuroGroup implementation, the second contains an RWMultiplicationGroup implementation, and the third contains an RWDivisionGroup implementation. The C++ code for creating such a factory looks something like this:
RWExchangeRateTable rates; // Exchange rates . . . RWTValSlist<RWCString> euroCurrencies // mnemonics of // currencies in the EMU . . . // Create the exchange rate factory that uses the exchange // rate table rates. By default it contains // a single exchange group instance, which uses an // RWMultiplicationGroup implementation. RWExchangeFactory<double> exchangeFactory(rates); // Create and prepend the Euro exchange group. This puts the // Euro group at the head of the list and the multiplication // group in the second position. RWEuroGroup<double>* euroGroupImpl = new RWEuroGroup<double>(euroCurrencies); exchangeFactory.prependExchangeGroup( RWExchangeGroup(euroGroupImpl) ); // Next create a division exchange group and append it // to the list. RWDivisionGroup<double>* divGroupImpl = new RWDivisionGroup<double>(euroCurrencies); exchangeFactory.appendExchangeGroup( RWExchangeGroup(divGroupImpl) );
You can write a similar program in Java for an exchange rate factory that implements the currency exchange strategy described at the beginning of this section. The Java ExchangeFactory contains an exchange rate table and a list of three ExchangeGroup objects. The first ExchangeGroup object in the list would contain a EuroGroup, the second a MultiplicationGroup, and the third a DivisionGroup. The Java code for creating the factory might look something like the following:
// Exchange rates ExchangeRateTable rates; . . . // mnemonics of currencies in the EMU Vector euroCurrencies; . . . // Create an exchange rate factory that uses the exchange // rate table rates. By default, it contains one group, a // MultiplicationGroup. ExchangeFactory factory = new ExchangeFactory(rates); // Create and prepend the Euro exchange group. This puts the // Euro group at the head of the list and the // multiplication group in second position. EuroGroup euroGroup = new EuroGroup(euroCurrencies); factory.prependExchangeGroup(euroGroup)); // Next create a division exchange group and append it to the list. DivisionGroup divGroup = new DivisionGroup(euroCurrencies); factory.appendExchangeGroup(divGroup));
Now, suppose that you want to create a simpler factory for an application that you know will not perform any triangular conversions, but may use either a multiplication or a division style of conversion. The following C++ code sets up that factory:
RWExchangeRateTable rates; // Exchange rates . . . // Create the exchange rate factory that uses the exchange // rate table rates. By default it contains // a single exchange group instance, which uses an // RWMultiplicationGroup implementation. RWExchangeFactory<double> exchangeFactory(rates); // Create a division exchange group and append it // to the list. RWDivisionGroup<double>* divGroupImpl = new RWDivisionGroup<double>; exchangeFactory.appendExchangeGroup( RWExchangeGroup(divGroupImpl) );
Once again, here is the simpler factory as set up through Java code:
// Exchange rates ExchangeRateTable rates; . . . // Create an exchange rate factory that uses the exchange // rate table rates. By default, it contains one group, a // MultiplicationGroup. ExchangeFactory factory = new ExchangeFactory(rates); // Create a division exchange group and append it to the list. DivisionGroup divGroup = new DivisionGroup(euroCurrencies); factory.appendExchangeGroup(divGroup));
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.