Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
198
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Abstract Factory (page 6) – The Abstract Factory creates families of related objects. To simplify access to the different objects the factory has created, the factory can also create a Facade object.

Mediator (page 77) – The Mediator pattern and the Facade pattern seem very similar. The difference is in the intent and in the implementation. The Mediator helps ease the communication between components and it adds behavior. The Facade is only an abstraction of the interface of one or more subsystems.

Singleton (page 34) – The Facade uses the Singleton pattern to guarantee a single, globally accessible point of access for a subsystem.

Session Facade [CJ2EEP] – The Session Facade pattern is a Facade that encapsulates the complexities of Enterprise JavaBeans™, to simplify the interface for its clients.

Example

Note:

For a full working example of this code example, with additional supporting classes and/or a RunPattern class, see “ Facade ” on page 468 of the “ Full Code Examples ” appendix.

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 3.20 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();

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.}

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.}

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.}

122

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.

Calling the setNation method in this class sets the current nation. That makes the wizard alter the Currency setting, the PhoneNumber, and a set of localized language strings, InternationalizedText.

Example 3.21 Currency.java

1.import java.text.NumberFormat;

2.public class Currency{

3.private char currencySymbol;

4.private NumberFormat numberFormat;

6.public void setCurrencySymbol(char newCurrencySymbol){ currencySymbol =

newCurrencySymbol; }

7.public void setNumberFormat(NumberFormat newNumberFormat){ numberFormat =

newNumberFormat; }

8.

9.public char getCurrencySymbol(){ return currencySymbol; }

10.public NumberFormat getNumberFormat(){ return numberFormat; }

11.}

Example 3.22 InternationalizedText.java

1.import java.util.Properties;

2.import java.io.File;

3.import java.io.IOException;

4.import java.io.FileInputStream;

5.public class InternationalizedText{

6.private static final String DEFAULT_FILE_NAME = "";

7.private Properties textProperties = new Properties();

9.public InternationalizedText(){

10.this(DEFAULT_FILE_NAME);

11.}

12.public InternationalizedText(String fileName){

13.loadProperties(fileName);

14.}

15.

16.public void setFileName(String newFileName){

17.if (newFileName != null){

18. loadProperties(newFileName);

19.}

20.}

21.public String getProperty(String key){

22.return getProperty(key, "");

23.}

24.public String getProperty(String key, String defaultValue){

25.return textProperties.getProperty(key, defaultValue);

26.}

27.

28.private void loadProperties(String fileName){

29.try{

30. FileInputStream input = new FileInputStream(fileName);

31. textProperties.load(input);

32.}

33.catch (IOException exc){

123

34. textProperties = new Properties();

35.}

36.}

37.}

Example 3.23 PhoneNumber.java

1.public class PhoneNumber {

2.private static String selectedInterPrefix;

3.private String internationalPrefix;

4.private String areaNumber;

5.private String netNumber;

6.

7.public PhoneNumber(String intPrefix, String areaNumber, String netNumber) {

8.this.internationalPrefix = intPrefix;

9.this.areaNumber = areaNumber;

10.this.netNumber = netNumber;

11.}

12.

13.public String getInternationalPrefix(){ return internationalPrefix; }

14.public String getAreaNumber(){ return areaNumber; }

15.public String getNetNumber(){ return netNumber; }

16.public static String getSelectedInterPrefix(){ return selectedInterPrefix; }

18.public void setInternationalPrefix(String newPrefix){ internationalPrefix =

newPrefix; }

19.public void setAreaNumber(String newAreaNumber){ areaNumber = newAreaNumber; }

20.public void setNetNumber(String newNetNumber){ netNumber = newNetNumber; }

21.public static void setSelectedInterPrefix(String prefix) { selectedInterPrefix =

prefix; }

22.

23.public String toString(){

24.return internationalPrefix + areaNumber + netNumber;

25.}

26.}

General country data is stored in a helper class, Nation. The InternationalizationWizard creates a collection of nations when it is first instantiated.

Example 3.24 Nation.java

1.import java.text.NumberFormat;

2.public class Nation {

3.private char symbol;

4.private String name;

5.private String dialingPrefix;

6.private String propertyFileName;

7.private NumberFormat numberFormat;

9.public Nation(String newName, char newSymbol, String newDialingPrefix,

10.String newPropertyFileName, NumberFormat newNumberFormat) {

11.name = newName;

12.symbol = newSymbol;

13.dialingPrefix = newDialingPrefix;

14.propertyFileName = newPropertyFileName;

15.numberFormat = newNumberFormat;

16.}

17.

18.public String getName(){ return name; }

19.public char getSymbol(){ return symbol; }

20.public String getDialingPrefix(){ return dialingPrefix; }

21.public String getPropertyFileName(){ return propertyFileName; }

22.public NumberFormat getNumberFormat(){ return numberFormat; }

24.public String toString(){ return name; }

25.}

124