1. 程式人生 > >2018.3.25 簡單工廠方法例子

2018.3.25 簡單工廠方法例子

hierarchy xtend bstr keyevent 簡單工廠方法 nag pos hab 例子

先定義 汽車保險接口 AutoInsurance

package com.glut.demo;

/**
 * 汽車保險
 * 接口
 * @author qichunlin
 *
 */
public interface AutoInsurance {
    abstract String getInfo();
}

定義具體類型去實現汽車保險的接口

package com.glut.demo;

/**
 * 
 * 車身噴油嘴
 * 實現類
 * @author qichunlin
 *
 */
public class BodyInjur implements AutoInsurance{
    
    private String description;//說明
    
    @Override
    public String getInfo() {
        description = " Body Injur Liability: \n\nBodily injury coverage pays for medical bills" +
                " lost wages, rehabilitation, treatment and/or" +
                " funeral costs for anyone injured or killed " +
                " by your car. Such coverage will also pay for" +
                " pain and suffering damages when a third " +
                " party successfully sues. ";
        return description;//註意這裏返回的是一個對象
    }
    
}

再定義另外一個工廠接口 PolicyProducer

package com.glut.demo;

/**
 * This is a sub class in the factory class hierarchy PolicyProducer
 * 
 * 實現接口PolicyProducer  的類
 * 
 * @author qichunlin
 *
 */
public class BodyPolicy implements PolicyProducer{

    @Override
    public AutoInsurance getInsurObj() {
        return new BodyInjur();
    }

}
package com.glut.demo;

public class LuxuryCarPolicy implements PolicyProducer{

    @Override
    public AutoInsurance getInsurObj() {
        return new LuxuryCarInjur();
    }

}

測試類 客戶端的GUI ,用戶選擇相應的保險顯示在屏幕上

package com.glut.demo;

/**
 * 
 * This is a client class that 
 * uses the factory method pattern
 * @author qichunlin
 *
 */

import java.awt.event.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.io.File;
import java.io.IOException;



public class ClientGUI extends JFrame {
    
      private JSplitPane  bigSplitPane;
      private JScrollPane showInfoPane;
      private JPanel btnPanel;
      private JComboBox cmbInsuranceType, cmbHouseType;
      private JLabel lblInsureType;
      private Dimension   minimumSize;
      private JTextArea txtForInfo;

      public static final String SHOW = "Show Info";
      public static final String EXIT = "Exit";
      public static final String BODYINJURE = "Body Injur Liability";
      public static final String COLLISION = "Collision Coverage";
      public static final String PERSONINJURE = "Personal Injury Protection";
      public static final String PROPERTY = "Property Damage Liability";
      public static final String COMPREHENSIVE = "Comprehensive Coverage";
      public static final String LuxuryCarInjur="LuxuryCarInjur";

      public ClientGUI() {
         super("Factory Method Pattern- Auto Insurance. ");
         minimumSize = new Dimension(130, 100);
         setUpChoicePanel();
         setUpScrollPanes();
       }

      private void setUpChoicePanel() {
          
          cmbInsuranceType = new JComboBox();
          cmbInsuranceType.addItem(BODYINJURE);
          cmbInsuranceType.addItem(COLLISION);
          cmbInsuranceType.addItem(PERSONINJURE);
          cmbInsuranceType.addItem(PROPERTY);
          cmbInsuranceType.addItem(COMPREHENSIVE);
          cmbInsuranceType.addItem(LuxuryCarInjur);

          lblInsureType = new JLabel("Insurance Types");

      //Create the open button
      JButton openButton = new JButton(SHOW);
      openButton.setMnemonic(KeyEvent.VK_S);
      JButton exitButton = new JButton(EXIT);
      exitButton.setMnemonic(KeyEvent.VK_X);

      ButtonListener btnListener = new ButtonListener();

      // add action Listener
      openButton.addActionListener(btnListener);
      exitButton.addActionListener(btnListener);

      btnPanel = new JPanel();

      //------------------------------------------------
      GridBagLayout gridbag = new GridBagLayout();
      btnPanel.setLayout(gridbag);
      GridBagConstraints gbc = new GridBagConstraints();

      btnPanel.add(lblInsureType);
      btnPanel.add(cmbInsuranceType);
      btnPanel.add(openButton);
      btnPanel.add(exitButton);

      gbc.insets.top = 5;
      gbc.insets.bottom = 5;
      gbc.insets.left = 5;
      gbc.insets.right = 5;

      gbc.gridx = 0;
      gbc.gridy = 0;
      gridbag.setConstraints(lblInsureType, gbc);
      gbc.gridx = 1;
      gbc.gridy = 0;
      gridbag.setConstraints(cmbInsuranceType, gbc);

      gbc.insets.left = 2;
      gbc.insets.right = 2;
      gbc.insets.top = 15;
      gbc.gridx = 0;
      gbc.gridy = 5;
      gridbag.setConstraints(openButton, gbc);
      gbc.anchor = GridBagConstraints.WEST;
      gbc.gridx = 1;
      gbc.gridy = 5;
      gridbag.setConstraints(exitButton, gbc);
      //-----------------------------------------------
   }

   private void setUpScrollPanes() {
      Border raisedbevel = BorderFactory.createRaisedBevelBorder();

      txtForInfo = new JTextArea("Auto insurance information will be shown here.", 20, 30);
      txtForInfo.setFont(new Font("Helvetica", Font.BOLD, 15));

      txtForInfo.setLineWrap(true);
      txtForInfo.setBackground(Color.pink);

      showInfoPane = new JScrollPane(txtForInfo);

      bigSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, showInfoPane, btnPanel);
      bigSplitPane.setDividerLocation(220);

      getContentPane().add(bigSplitPane);
      setSize(new Dimension(500, 300));
      setVisible(true);
   }

   class ButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent ae) {

        if (ae.getActionCommand().equals(EXIT)) {
            System.exit(1);
        }

        if (ae.getActionCommand().equals(SHOW)) {

            String type = (String) cmbInsuranceType.getSelectedItem();
            PolicyProducer pp=null;

            if (type.equals(BODYINJURE)) {
                pp=new BodyPolicy();
            }
            else if (type.equals(COLLISION)) {
                pp=new CollPolicy();
            }
            else if (type.equals(PERSONINJURE)) {
                pp= new PersonPolicy();
            }
            else if (type.equals(PROPERTY)) {
                pp = new PropPolicy();
            }
            else if (type.equals(COMPREHENSIVE)) {
                pp= new ComPolicy();
            }else if(type.equals(LuxuryCarInjur)){
                pp =new LuxuryCarPolicy();
            }

            AutoInsurance ai = pp.getInsurObj();
            String desc = ai.getInfo();
            txtForInfo.setText(desc);
          }
      }
   }

   public static void main(String args[])
   {
      try {
         UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      }
      catch (Exception evt) {}

      ClientGUI frame = new ClientGUI();
      frame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e)
         {
            System.exit(0);
         }
      }
      );
      frame.setSize(500, 420);
      frame.setVisible(true);
   }
}

2018.3.25 簡單工廠方法例子