zoukankan      html  css  js  c++  java
  • 2018.3.25 设计模式之简单工厂方法例子(汽车保险)

    先定义 汽车保险接口 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: 
    
    Bodily 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);
       }
    }
    
    
    
  • 相关阅读:
    9.12 其他样式;JS
    9.11 样式表
    9.9 样式表
    PHP延迟几秒后执行,PHP延迟执行,PHP停顿几毫秒后执行 usleep() 和 sleep()
    PHP 去除url参数中出现+、空格、=、%、&、#等字符的解决办法, PHP去除影响url路径的字符
    java第一堂课-常用的电脑快捷键操作和重要的dos命令
    常用第三方接口,支付宝支付,微信支付,软著,IOS开发者账号
    苹果开发者账号申请流程完整版 https://www.jianshu.com/p/655380201685
    thinkphp 去重查询
    将数据导【入】【excel表】格式 前端页面
  • 原文地址:https://www.cnblogs.com/qichunlin/p/8645836.html
Copyright © 2011-2022 走看看