Foreign Exchange layer

The ForeignExchange is the interface to the Foreign Exchange component. Client application of the component has to keep a reference to the interface to call the conversion methods, which decouples the implementation of the component with the client application.

The ForeignExchangeImpl is the implementation of currency conversion logic which is mainly related with the amount of currencies. It obtains the settings used to customize the component, such as the conversion currency, the rate cache refresh interval from the XML file. It is also responsible for passing each Foreign Exchange rate query request to its sub-layers, the Rates Provider layer.

The Foreign Exchange Layer is also responsible for caching rates, in order to reduce the number of rate retrieval requests that the ForeignExchangeImpl sends to the Rates Provider Layer. Before calling the RatesProvider to query the rates, the ForeignExchangeImpl checks the cache to see if the rates it needs are contained there.

To prevent rates in the cache from becoming stale, the cache clears the rates from time to time. It compares the time when the rates were last cached with the time of the current attempt to retrieve them from the cache. If the result of the comparison exceeds a specified time limit, which is referred as rates refresh interval, the cache is emptied. If this is the case, the ForeignExchangeImpl retrieves fresh rates from the server (through its sub-layers). If it does obtain fresh rates, it adds them to the cache.