Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Currency Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

5.5 Currency Exchange Factory

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:

  1. 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:

  2. 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.

  3. 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.

  4. If you can't find a source-to-target rate in the exchange rate table, look in the table for an exchange rate that converts the target into the source. If you find this rate, 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,", 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. The Currency Module supplies the following exchange groups:

The 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 pointer to a specific implementation class that must be derived from RWExchangeGroupImpl. An exchange factory object, RWExchangeFactory, 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, 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 code for creating such a factory looks something like this:

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 code sets up that factory:



Previous fileTop of DocumentContentsIndex pageNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.