Facade Pattern 2 in Java : 门面模式 « 设计模式 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 设计模式 » 门面模式屏幕截图 
Facade Pattern 2 in Java
Facade Pattern 2 in Java

//[C] 2002 Sun Microsystems, Inc.---

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Properties;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RunFacadePattern {
    public static void main(String [] arguments){
        System.out.println("Example for the Facade pattern");
        System.out.println();
        System.out.println("This code sample uses an InternationalizatgionWizard (a Facade)");
        System.out.println(" to manage communication between the rest of the application and");
        System.out.println(" a series of other classes.");
        System.out.println();
        System.out.println("The InternationalizatgionWizard maintains a colleciton of Nation");
        System.out.println(" objects. When the setNation method is called, the wizard sets the");
        System.out.println(" default nation, updating the Currency, PhoneNumber and localized");
        System.out.println(" String resources (InternationalizedText) available.");
        System.out.println();
        System.out.println("Calls to get Strings for the GUI, the currency symbol or the dialing");
        System.out.println(" prefix are routed through the Facade, the InternationalizationWizard.");
        System.out.println();
        
        if (!(new File("data.ser").exists())){
            DataCreator.serialize("data.ser");
        }
        
        System.out.println("Creating the InternationalizationWizard and setting the nation to US.");
        System.out.println();
        InternationalizationWizard wizard = new InternationalizationWizard();
        wizard.setNation("US");
        
        System.out.println("Creating the FacadeGui.");
        System.out.println();
        FacadeGui application = new FacadeGui(wizard);
        application.createGui();
        application.setNation(wizard.getNation("US"));
    }
}
class FacadeGui implements ActionListener, ItemListener{
    private static final String GUI_TITLE = "title";
    private static final String EXIT_CAPTION = "exit";
    private static final String COUNTRY_LABEL = "country";
    private static final String CURRENCY_LABEL = "currency";
    private static final String PHONE_LABEL = "phone";
    
    private JFrame mainFrame;
    private JButton exit;
    private JComboBox countryChooser;
    private JPanel controlPanel, displayPanel;
    private JLabel countryLabel, currencyLabel, phoneLabel;
    private JTextField currencyTextField, phoneTextField;
    private InternationalizationWizard nationalityFacade;
    
    public FacadeGui(InternationalizationWizard wizard){
        nationalityFacade = wizard;
    }
    
    public void createGui(){
        mainFrame = new JFrame(nationalityFacade.getProperty(GUI_TITLE));
        Container content = mainFrame.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        
        displayPanel = new JPanel();
        displayPanel.setLayout(new GridLayout(32));
        
        countryLabel = new JLabel(nationalityFacade.getProperty(COUNTRY_LABEL));
        countryChooser = new JComboBox(nationalityFacade.getNations());
        currencyLabel = new JLabel(nationalityFacade.getProperty(CURRENCY_LABEL));
        currencyTextField = new JTextField();
        phoneLabel = new JLabel(nationalityFacade.getProperty(PHONE_LABEL));
        phoneTextField = new JTextField();
        
        currencyTextField.setEditable(false);
        phoneTextField.setEditable(false);
        
        displayPanel.add(countryLabel);
        displayPanel.add(countryChooser);
        displayPanel.add(currencyLabel);
        displayPanel.add(currencyTextField);
        displayPanel.add(phoneLabel);
        displayPanel.add(phoneTextField);
        content.add(displayPanel);
        
        controlPanel = new JPanel();
        exit = new JButton(nationalityFacade.getProperty(EXIT_CAPTION));
        controlPanel.add(exit);
        content.add(controlPanel);
        
        exit.addActionListener(this);
        countryChooser.addItemListener(this);
        
        mainFrame.addWindowListener(new WindowCloseManager());
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
    
    private void updateGui(){
        nationalityFacade.setNation(countryChooser.getSelectedItem().toString());
        mainFrame.setTitle(nationalityFacade.getProperty(GUI_TITLE));
        countryLabel.setText(nationalityFacade.getProperty(COUNTRY_LABEL));
        currencyLabel.setText(nationalityFacade.getProperty(CURRENCY_LABEL));
        phoneLabel.setText(nationalityFacade.getProperty(PHONE_LABEL));
        exit.setText(nationalityFacade.getProperty(EXIT_CAPTION));

        currencyTextField.setText(nationalityFacade.getCurrencySymbol() " " +
            nationalityFacade.getNumberFormat().format(5280.50));
        phoneTextField.setText(nationalityFacade.getPhonePrefix());

        mainFrame.invalidate();
        countryLabel.invalidate();
        currencyLabel.invalidate();
        phoneLabel.invalidate();
        exit.invalidate();
        mainFrame.validate();
    }
    
    public void actionPerformed(ActionEvent evt){
        Object originator = evt.getSource();
        if (originator == exit){
            exitApplication();
        }
    }
    public void itemStateChanged(ItemEvent evt){
        Object originator = evt.getSource();
        if (originator == countryChooser){
            updateGui();
        }
    }
    
    public void setNation(Nation nation){
        countryChooser.setSelectedItem(nation);
    }
    
    private class WindowCloseManager extends WindowAdapter{
        public void windowClosing(WindowEvent evt){
            exitApplication();
        }
    }
    
    private void exitApplication(){
        System.exit(0);
    }
}

class Nation {
    private char symbol;
    private String name;
    private String dialingPrefix;
    private String propertyFileName;
    private NumberFormat numberFormat;
    
    public Nation(String newName, char newSymbol, String newDialingPrefix,
        String newPropertyFileName, NumberFormat newNumberFormat) {
        name = newName;
        symbol = newSymbol;
        dialingPrefix = newDialingPrefix;
        propertyFileName = newPropertyFileName;
        numberFormat = newNumberFormat;
    }
    
    public String getName(){ return name; }
    public char getSymbol(){ return symbol; }
    public String getDialingPrefix(){ return dialingPrefix; }
    public String getPropertyFileName(){ return propertyFileName; }
    public NumberFormat getNumberFormat(){ return numberFormat; }
    
    public String toString(){ return name; }
}

class Currency{
    private char currencySymbol;
    private NumberFormat numberFormat;
    
    public void setCurrencySymbol(char newCurrencySymbol){ currencySymbol = newCurrencySymbol; }
    public void setNumberFormat(NumberFormat newNumberFormat){ numberFormat = newNumberFormat; }
    
    public char getCurrencySymbol(){ return currencySymbol; }
    public NumberFormat getNumberFormat(){ return numberFormat; }
}

class DataCreator{
    private static final String GUI_TITLE = "title";
    private static final String EXIT_CAPTION = "exit";
    private static final String COUNTRY_LABEL = "country";
    private static final String CURRENCY_LABEL = "currency";
    private static final String PHONE_LABEL = "phone";
    
    public static void serialize(String fileName){
        saveFrData();
        saveUsData();
        saveNlData();
    }
    
    private static void saveFrData(){
        try{
            Properties textSettings = new Properties();
            textSettings.setProperty(GUI_TITLE, "Demonstration du Pattern Facade");
            textSettings.setProperty(EXIT_CAPTION, "Sortir");
            textSettings.setProperty(COUNTRY_LABEL, "Pays");
            textSettings.setProperty(CURRENCY_LABEL, "Monnaie");
            textSettings.setProperty(PHONE_LABEL, "Numero de Telephone");
            textSettings.store(new FileOutputStream("french.properties")"French Settings");
        }
        catch (IOException exc){
            System.err.println("Error storing settings to output");
            exc.printStackTrace();
        }
    }
    private static void saveUsData(){
        try{
            Properties textSettings = new Properties();
            textSettings.setProperty(GUI_TITLE, "Facade Pattern Demonstration");
            textSettings.setProperty(EXIT_CAPTION, "Exit");
            textSettings.setProperty(COUNTRY_LABEL, "Country");
            textSettings.setProperty(CURRENCY_LABEL, "Currency");
            textSettings.setProperty(PHONE_LABEL, "Phone Number");
            textSettings.store(new FileOutputStream("us.properties")"US Settings");
        }
        catch (IOException exc){
            System.err.println("Error storing settings to output");
            exc.printStackTrace();
        }
    }
    private static void saveNlData(){
        try{
            Properties textSettings = new Properties();
            textSettings.setProperty(GUI_TITLE, "Facade Pattern voorbeeld");
            textSettings.setProperty(EXIT_CAPTION, "Exit");
            textSettings.setProperty(COUNTRY_LABEL, "Land");
            textSettings.setProperty(CURRENCY_LABEL, "Munt eenheid");
            textSettings.setProperty(PHONE_LABEL, "Telefoonnummer");
            textSettings.store(new FileOutputStream("dutch.properties")"Dutch Settings");
        }
        catch (IOException exc){
            System.err.println("Error storing settings to output");
            exc.printStackTrace();
        }
    }
}

class PhoneNumber {
    private static String selectedInterPrefix;
    private String internationalPrefix;
    private String areaNumber;
    private String netNumber;

    public PhoneNumber(String intPrefix, String areaNumber, String netNumber) {
        this.internationalPrefix = intPrefix;
        this.areaNumber = areaNumber;
        this.netNumber = netNumber;
    }
    
    public String getInternationalPrefix(){ return internationalPrefix; }
    public String getAreaNumber(){ return areaNumber; }
    public String getNetNumber(){ return netNumber; }
    public static String getSelectedInterPrefix(){ return selectedInterPrefix; }
    
    public void setInternationalPrefix(String newPrefix){ internationalPrefix = newPrefix; }
    public void setAreaNumber(String newAreaNumber){ areaNumber = newAreaNumber; }
    public void setNetNumber(String newNetNumber){ netNumber = newNetNumber; }
    public static void setSelectedInterPrefix(String prefix) { selectedInterPrefix = prefix; }
    
    public String toString(){
        return internationalPrefix + areaNumber + netNumber;
    }
}

class InternationalizedText{
    private static final String DEFAULT_FILE_NAME = "";
    private Properties textProperties = new Properties();
    
    public InternationalizedText(){
        this(DEFAULT_FILE_NAME);
    }
    public InternationalizedText(String fileName){
        loadProperties(fileName);
    }
    
    public void setFileName(String newFileName){
        if (newFileName != null){
            loadProperties(newFileName);
        }
    }
    public String getProperty(String key){
        return getProperty(key, "");
    }
    public String getProperty(String key, String defaultValue){
        return textProperties.getProperty(key, defaultValue);
    }
    
    private void loadProperties(String fileName){
        try{
            FileInputStream input = new FileInputStream(fileName);
            textProperties.load(input);
        }
        catch (IOException exc){
            textProperties = new Properties();
        }
    }
}
class InternationalizationWizard{
    private HashMap map;
    private Currency currency = new Currency();
    private InternationalizedText propertyFile = new InternationalizedText();
    
    public InternationalizationWizard() {
        map = new HashMap();
        Nation[] nations = {
            new Nation("US"'$'"+1""us.properties", NumberFormat.getInstance(Locale.US)),
            new Nation("The Netherlands"'f'"+31""dutch.properties", NumberFormat.getInstance(Locale.GERMANY)),
            new Nation("France"'f'"+33""french.properties", NumberFormat.getInstance(Locale.FRANCE))
        };
        for (int i = 0; i < nations.length; i++) {
            map.put(nations[i].getName(), nations[i]);
        }
    }
    
    public void setNation(String name) {
        Nation nation = (Nation)map.get(name);
        if (nation != null) {
            currency.setCurrencySymbol(nation.getSymbol());
            currency.setNumberFormat(nation.getNumberFormat());
            PhoneNumber.setSelectedInterPrefix(nation.getDialingPrefix());
            propertyFile.setFileName(nation.getPropertyFileName());
        }
    }
    
    public Object[] getNations(){
        return map.values().toArray();
    }
    public Nation getNation(String name){
        return (Nation)map.get(name);
    }
    public char getCurrencySymbol(){
        return currency.getCurrencySymbol();
    }
    public NumberFormat getNumberFormat(){
        return currency.getNumberFormat();
    }
    public String getPhonePrefix(){
        return PhoneNumber.getSelectedInterPrefix();
    }
    public String getProperty(String key){
        return propertyFile.getProperty(key);
    }
    public String getProperty(String key, String defaultValue){
        return propertyFile.getProperty(key, defaultValue);
    }
}

           
       
Related examples in the same category
1. Java中的Facade模式Java中的Facade模式
2. 门面模式
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.