Currency Conversions
Introduction
In order to perform a currency conversion, you need to know which currency you’re converting from, which currency you’re converting to, what exchange rate you should use, and how you should apply that exchange rate. The Currency Module ties all these pieces of the conversion process together.
First, the Currency Module provides a way to define exchange rates and to build a table of those exchange rates. These objects, and some examples of the processes used to create them, are described in Exchange Rates and Exchange Rate Tables.
After you determine the exchange rates, you need to tie them to the processes that will be used to perform the conversions from one currency to another. The classes that let you convert money are called currency exchange classes. The Currency Module uses a handle/body design for these classes so that all of them provide a common API, even though they may implement different conversion methods. Currency exchange classes can be categorized into currency exchange groups according to the type of conversion process they employ. Currency exchange groups bind a particular conversion process to a set of exchange rates. For example, the Currency Module provides a multiplication exchange group that binds exchange rates to a process that multiplies a source currency by an exchange rate to result in a target currency. Currency Exchange Classes describes the design of the exchange classes and Currency Exchange Groups describes the exchange groups supplied by the Currency Module.
A currency exchange factory lets your programs automatically create an exchange object with which to perform a currency conversion. By setting up an appropriately-designed exchange factory, you don’t need to think about what type of exchange object you’re using to perform a conversion--if an exchange rate for the currencies you’ve named exists, the exchange object will be created. Currency Exchange Factory provides examples of setting up an exchange factory.
Performing Currency Exchanges provides code for some example conversions, and Creating Custom Exchange Objects and Groups describes how to customize exchange objects. Finally, The Exchange Class Hierarchies provides diagrams of the class hierarchies described in this chapter.