Java Reference
In-Depth Information
Facade
To make the PIM more functional for users, you want to give them the opportunity to customize the application.
Some examples of items to customize include font type, font size, colors, which services to start when, default
currency, etc. This example tracks a set of nationality-based settings.
In this example, the Facade class is the InternationalizationWizard . This class coordinates between a client
and a number of objects associated with a selected nationality.
Example A.163 InternationalizationWizard.java
1. import java.util.HashMap;
2. import java.text.NumberFormat;
3. import java.util.Locale;
4. public class InternationalizationWizard{
5. private HashMap map;
6. private Currency currency = new Currency();
7. private InternationalizedText propertyFile = new InternationalizedText();
8.
9. public InternationalizationWizard() {
10. map = new HashMap();
11. Nation[] nations = {
12. new Nation("US", '$', "+1", "us.properties", NumberFormat. getInstance(Locale.US)),
13. new Nation("The Netherlands", 'f', "+31", "dutch.properties",
NumberFormat.getInstance(Locale.GERMANY)),
14. new Nation("France", 'f', "+33", "french.properties", NumberFormat.
getInstance(Locale.FRANCE))
15. };
16. for (int i = 0; i < nations.length; i++) {
17. map.put(nations[i].getName(), nations[i]);
18. }
19. }
20.
21. public void setNation(String name) {
22. Nation nation = (Nation)map.get(name);
23. if (nation != null) {
24. currency.setCurrencySymbol(nation.getSymbol());
25. currency.setNumberFormat(nation.getNumberFormat());
26. PhoneNumber.setSelectedInterPrefix(nation.getDialingPrefix());
27. propertyFile.setFileName(nation.getPropertyFileName());
28. }
29. }
30.
31. public Object[] getNations(){
32. return map.values().toArray();
33. }
34. public Nation getNation(String name){
35. return (Nation)map.get(name);
36. }
37. public char getCurrencySymbol(){
38. return currency.getCurrencySymbol();
39. }
40. public NumberFormat getNumberFormat(){
41. return currency.getNumberFormat();
42. }
43. public String getPhonePrefix(){
44. return PhoneNumber.getSelectedInterPrefix();
45. }
46. public String getProperty(String key){
47. return propertyFile.getProperty(key);
48. }
49. public String getProperty(String key, String defaultValue){
50. return propertyFile.getProperty(key, defaultValue);
51. }
52. }
Note that the InternationalizationWizard has a number of get methods, which it delegates to its associated
objects. It also has a method setNation , used to change the nation used by the client.
Although the Facade manages the internationalized settings for a number of objects in this example, it is still
possible to manage each object individually. This is one of the benefits of this pattern—it allows a group of
objects to be managed collectively in some situations, but still provides the freedom to individually manage the
components as well.
 
Search WWH ::




Custom Search