Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

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, 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:

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:

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:

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:

Once again, here is the simpler factory as set up through Java code:


Previous fileTop of DocumentContentsIndexNext file

©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.