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

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

To better illustrate the use of the Facade in a user environment, the class FacadeGui creates a simple Swing GUI which demonstrates the effect of changing the country, calling the get methods for the InternationalizationWizard to provide language, currency and phone number information.

Example A.168 FacadeGui.java

1.import java.awt.Container;

2.import java.awt.GridLayout;

3.import java.awt.event.ActionListener;

4.import java.awt.event.ActionEvent;

5.import java.awt.event.ItemListener;

6.import java.awt.event.ItemEvent;

7.import java.awt.event.WindowAdapter;

8.import java.awt.event.WindowEvent;

9.import javax.swing.BoxLayout;

10.import javax.swing.JButton;

11.import javax.swing.JComboBox;

12.import javax.swing.JFrame;

13.import javax.swing.JLabel;

14.import javax.swing.JPanel;

15.import javax.swing.JTextField;

16.public class FacadeGui implements ActionListener, ItemListener{

17.private static final String GUI_TITLE = "title";

18.private static final String EXIT_CAPTION = "exit";

19.private static final String COUNTRY_LABEL = "country";

20.private static final String CURRENCY_LABEL = "currency";

21.private static final String PHONE_LABEL = "phone";

22.

23.private JFrame mainFrame;

24.private JButton exit;

25.private JComboBox countryChooser;

26.private JPanel controlPanel, displayPanel;

27.private JLabel countryLabel, currencyLabel, phoneLabel;

28.private JTextField currencyTextField, phoneTextField;

308

29. private InternationalizationWizard nationalityFacade;

30.

31.public FacadeGui(InternationalizationWizard wizard){

32.nationalityFacade = wizard;

33.}

34.

35.public void createGui(){

36.mainFrame = new JFrame(nationalityFacade.getProperty(GUI_TITLE));

37.Container content = mainFrame.getContentPane();

38.content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

39.

40.displayPanel = new JPanel();

41.displayPanel.setLayout(new GridLayout(3, 2));

43.countryLabel = new JLabel(nationalityFacade.getProperty(COUNTRY_LABEL));

44.countryChooser = new JComboBox(nationalityFacade.getNations());

45.currencyLabel = new JLabel(nationalityFacade.getProperty(CURRENCY_LABEL));

46.currencyTextField = new JTextField();

47.phoneLabel = new JLabel(nationalityFacade.getProperty(PHONE_LABEL));

48.phoneTextField = new JTextField();

49.

50.currencyTextField.setEditable(false);

51.phoneTextField.setEditable(false);

53.displayPanel.add(countryLabel);

54.displayPanel.add(countryChooser);

55.displayPanel.add(currencyLabel);

56.displayPanel.add(currencyTextField);

57.displayPanel.add(phoneLabel);

58.displayPanel.add(phoneTextField);

59.content.add(displayPanel);

60.

61.controlPanel = new JPanel();

62.exit = new JButton(nationalityFacade.getProperty(EXIT_CAPTION));

63.controlPanel.add(exit);

64.content.add(controlPanel);

65.

66.exit.addActionListener(this);

67.countryChooser.addItemListener(this);

69.mainFrame.addWindowListener(new WindowCloseManager());

70.mainFrame.pack();

71.mainFrame.setVisible(true);

72.}

73.

74.private void updateGui(){

75.nationalityFacade.setNation(countryChooser.getSelectedItem().toString());

76.mainFrame.setTitle(nationalityFacade.getProperty(GUI_TITLE));

77.countryLabel.setText(nationalityFacade.getProperty(COUNTRY_LABEL));

78.currencyLabel.setText(nationalityFacade.getProperty(CURRENCY_LABEL));

79.phoneLabel.setText(nationalityFacade.getProperty(PHONE_LABEL));

80.exit.setText(nationalityFacade.getProperty(EXIT_CAPTION));

81.

82.currencyTextField.setText(nationalityFacade.getCurrencySymbol() + " " +

83.

nationalityFacade.getNumberFormat().format(5280.50));

84.

phoneTextField.setText(nationalityFacade.getPhonePrefix());

85.

 

86.mainFrame.invalidate();

87.countryLabel.invalidate();

88.currencyLabel.invalidate();

89.phoneLabel.invalidate();

90.exit.invalidate();

91.mainFrame.validate();

92.}

93.

94.public void actionPerformed(ActionEvent evt){

95.Object originator = evt.getSource();

96.if (originator == exit){

97. exitApplication();

98.}

99.}

100.public void itemStateChanged(ItemEvent evt){

101.Object originator = evt.getSource();

102.if (originator == countryChooser){

103.updateGui();

104.}

105.}

106.

309

107.public void setNation(Nation nation){

108.countryChooser.setSelectedItem(nation);

109.}

110.

111.private class WindowCloseManager extends WindowAdapter{

112.public void windowClosing(WindowEvent evt){

113.exitApplication();

114.}

115.}

116.

117.private void exitApplication(){

118.System.exit(0);

119.}

120.}

The class DataCreator produces a set of InternationalizedText objects to use in this example.

Example A.169 DataCreator.java

1.import java.util.Properties;

2.import java.io.IOException;

3.import java.io.FileOutputStream;

4.public class DataCreator{

5.private static final String GUI_TITLE = "title";

6.private static final String EXIT_CAPTION = "exit";

7.private static final String COUNTRY_LABEL = "country";

8.private static final String CURRENCY_LABEL = "currency";

9.private static final String PHONE_LABEL = "phone";

10.

11.public static void serialize(String fileName){

12.saveFrData();

13.saveUsData();

14.saveNlData();

15.}

16.

17.private static void saveFrData(){

18.try{

19. Properties textSettings = new Properties();

20. textSettings.setProperty(GUI_TITLE, "Demonstration du Pattern Facade");

21. textSettings.setProperty(EXIT_CAPTION, "Sortir");

22. textSettings.setProperty(COUNTRY_LABEL, "Pays");

23. textSettings.setProperty(CURRENCY_LABEL, "Monnaie");

24. textSettings.setProperty(PHONE_LABEL, "Numero de Telephone");

25. textSettings.store(new FileOutputStream("french.properties"), "French Settings");

26.}

27.catch (IOException exc){

28. System.err.println("Error storing settings to output");

29. exc.printStackTrace();

30.}

31.}

32.private static void saveUsData(){

33.try{

34. Properties textSettings = new Properties();

35. textSettings.setProperty(GUI_TITLE, "Facade Pattern Demonstration");

36. textSettings.setProperty(EXIT_CAPTION, "Exit");

37. textSettings.setProperty(COUNTRY_LABEL, "Country");

38. textSettings.setProperty(CURRENCY_LABEL, "Currency");

39. textSettings.setProperty(PHONE_LABEL, "Phone Number");

40. textSettings.store(new FileOutputStream("us.properties"), "US Settings");

41.}

42.catch (IOException exc){

43. System.err.println("Error storing settings to output");

44. exc.printStackTrace();

45.}

46.}

47.private static void saveNlData(){

48.try{

49. Properties textSettings = new Properties();

50. textSettings.setProperty(GUI_TITLE, "Facade Pattern voorbeeld");

51. textSettings.setProperty(EXIT_CAPTION, "Exit");

52. textSettings.setProperty(COUNTRY_LABEL, "Land");

53. textSettings.setProperty(CURRENCY_LABEL, "Munt eenheid");

54. textSettings.setProperty(PHONE_LABEL, "Telefoonnummer");

55. textSettings.store(new FileOutputStream("dutch.properties"), "Dutch Settings");

56.}

57.catch (IOException exc){

58. System.err.println("Error storing settings to output");

59. exc.printStackTrace();

310

60.}

61.}

62.}

The RunPattern class creates the InternationalizationWizard and associates it with the GUI; subsequently, the InternationalizationWizard (Facade) can be used to obtain information about the currently selected country.

Example A.170 RunPattern.java

1.import java.io.File;

2.public class RunPattern{

3.public static void main(String [] arguments){

4.System.out.println("Example for the Facade pattern");

5.System.out.println();

6.System.out.println("This code sample uses an InternationalizatgionWizard (a

Facade)");

7.System.out.println(" to manage communication between the rest of the application

and");

8.System.out.println(" a series of other classes.");

9.System.out.println();

10.System.out.println("The InternationalizatgionWizard maintains a colleciton of

Nation");

11.System.out.println(" objects. When the setNation method is called, the wizard sets

the");

12.System.out.println(" default nation, updating the Currency, PhoneNumber and

localized");

13.System.out.println(" String resources (InternationalizedText) available.");

14.System.out.println();

15.System.out.println("Calls to get Strings for the GUI, the currency symbol or the

dialing");

16.System.out.println(" prefix are routed through the Facade, the

InternationalizationWizard."); 17. System.out.println();

18.

19.if (!(new File("data.ser").exists())){

20.

DataCreator.serialize("data.ser");

 

21.

}

L

 

22.

System.out.println("Creating the InternationalizationWizardY

and setting the nation

23.

 

to US.");

F

 

24.System.out.println();

25.InternationalizationWizard wizard = new InternationalizationWizard();

26.wizard.setNation("US"); MA

28.System.out.println("CreatingTEthe FacadeGui.");

29.System.out.println();

30.FacadeGui application = new FacadeGui(wizard);

31.application.createGui();

32.application.setNation(wizard.getNation("US"));

33.}

34.}27.

TEAM FLY PRESENTS

311