Java Reference
In-Depth Information
double requestCurrentRate(String fromCurrency,
String toCurrency);
}
There could be 100 different implementations of this interface, any of which you
might be required to use. Rather than hard-code a specific implementation into
the client code, let's request an implementation from an intermediary class. That
way, you're not bound to a one specific service: the intermediary can pick and
choose what implementation to provide each time a request is made. Create a
CurrencyExchangeServiceFactory in the same package as the interface, and edit
the file to look like listing 2.2.
Listing 2.2
The factory that will retrieve a CurrencyExchangeService for you
package com.acme.conversion.currency.service;
/**
* This factory is responsible for providing a valid currency
* exchange service upon request.
*/
public class CurrencyExchangeServiceFactory {
public static CurrencyExchangeService getService() {
// The body of this method should decide on and return
// a valid CurrencyExchangeService of some sort.
// Until we either write one (or are provided one),
// we can only return null here.
return null;
}
}
Finally, let's write a simple command-line program to use this factory and inter-
face. Use the Project tool window to create a new package called com.acme.con-
version.currency.client , and then create a new class in that package called
CommandLineClient . Edit the file to look like listing 2.3.
Listing 2.3
The first implementation of your command line client
package com.acme.conversion.currency.client;
import com.acme.conversion.currency.service.CurrencyExchangeService;
import
com.acme.conversion.currency.service.CurrencyExchangeServiceFactory;
Search WWH ::




Custom Search